forum.alglib.net
http://forum.alglib.net/

logisticfit4 not giving expected results
http://forum.alglib.net/viewtopic.php?f=2&t=3871
Page 1 of 1

Author:  ericlinner [ Thu Nov 01, 2018 2:46 pm ]
Post subject:  logisticfit4 not giving expected results

I'm evaluating alglib (C++) for possible use in an embedded application that needs to do some 4PL curve fit calculations. The OS is QNX (Posix) and the CPU is x86-64. I have successfully built the library and run the test_c.cpp checks so I think it is functional. I also ran the example attached to logisticfit4 and it gave the expected results.

However, when I input some sample project data, I'm not getting the expected results. I will attach the test program and the csv file containing the input data. For this data, MATLAB and the ZunZun online 4PL calculator give almost identical results very different from Alglib:

Param Alglib MATLAB ZunZun
a 0.0850 0.0988 0.0986
b 10 31.9422 31.9155
c 22.4659 17.535 17.535
d 5.42989 1.0139 1.0140

Here is the output I get when I run my test with Alglib:

a = 0.0850456
b = 10
c = 22.4659
d = 5.42989
iterations = 118
RMS error = 0.0469343
Avg error = 0.0303365
Avg relative error = 0.192896
Max error = 0.1581
R squared = 0.960512

I tried to use logisticfit45x() to affect the results but I don't understand how to use the controls and the results didn't improve. Can you tell me what I'm doing wrong?

Attachments:
File comment: Source file to read the CSV file and calculate the 4PL fit parameters
main.cpp [2.45 KiB]
Downloaded 716 times
File comment: CSV file with x in the first column and y in the second column
sensor8lowedit.csv [2.88 KiB]
Downloaded 678 times

Author:  Sergey.Bochkanov [ Thu Nov 01, 2018 5:38 pm ]
Post subject:  Re: logisticfit4 not giving expected results

Hi!

I confirm the bug, and I think that I know the reason behind it (unable to test it at my work PC, already out of office, but I am pretty sure).

For stability reasons ALGLIB puts constraints on parameters being fitted and relaxes them several times. However, when logistic fitting functionality was developed it was thought that constraining B to be in [0.1,10] range is good enough option. It is exponent, after all - no one imagined that actual data will need an exponent equal to 30! So, relaxing this constraint should help to solve the problem.

You can do so yourself as follows (I do not give you line numbers because your trial is customized with license boilerplate, line numbers are different from that of the publicly available ALGLIB):
1) find a line with comment "Relax constraints, run optimization one more time" in the interpolation.cpp. It is the only such line in the file.
2) below this line find bndl.ptr.p_double[1] = 0.1; and replace 0.1 by 0.01
3) below this line find bndu.ptr.p_double[1] = 10.0; and replace 10.0 by 100.0

Does it help?

Author:  ericlinner [ Thu Nov 01, 2018 5:59 pm ]
Post subject:  Re: logisticfit4 not giving expected results

Yes, that worked. Thank you for the quick response. The output is now as expected:

a = 0.0985619
b = 31.9155
c = 17.535
d = 1.01404
iterations = 140
RMS error = 0.0112002
Avg error = 0.00489951
Avg relative error = 0.019207
Max error = 0.0658847
R squared = 0.997751

I do have a comment about limiting b, even to 0.01 to 100. According to https://www.myassays.com/four-parameter-logistic-regression.html, b is the slope of the curve at c (the inflection point). I can imagine cases where the slope could be outside of 0.01-100, although it is a matter of units (e.g. using kg vs. g). But I would think to be on the safe side you would allow 0.001 to 1000.

Author:  Sergey.Bochkanov [ Fri Nov 02, 2018 10:06 am ]
Post subject:  Re: logisticfit4 not giving expected results

1) I am a bit concerned about robustness here. It is easy to ruin entire optimization session by making step in a wrong place (too large/small b and/or c). In your case relaxing bounds worked fine, but I still have to think about how to make it robust in the general case.

2) is it possible to use your data as a part of our test suite? :-) of course, I can generate something synthetic like that... but generally it is a matter of good taste to include cases which failed in the tests :)

Author:  ericlinner [ Fri Nov 02, 2018 1:38 pm ]
Post subject:  Re: logisticfit4 not giving expected results

Yes, that is fine

Author:  Sergey.Bochkanov [ Fri Nov 02, 2018 2:40 pm ]
Post subject:  Re: logisticfit4 not giving expected results

Thank you, Eric!

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/