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

Fitting Gaussian with lsfit
http://forum.alglib.net/viewtopic.php?f=2&t=31
Page 1 of 1

Author:  codudi [ Tue Jun 22, 2010 5:54 am ]
Post subject:  Fitting Gaussian with lsfit

First, I'd like to thank you so much for the great effort of putting something like this!

I am trying to do nonlinear fitting of a simple gaussian function:
f= d - ae(-(x-b)^2/c^2)
The function has 4 parameters. I have tried both fg and fgh and in all cases I am not getting the optimal fitting parameters. Same fitting done under Matlab provide the corrected parameters:

a= -0.150688
b= 4.13677
c= 2.44339
d= -0.0037984

See attached file
Many Thanks

Attachments:
File comment: Visual 2010 file
Program.txt [6.72 KiB]
Downloaded 888 times

Author:  Sergey.Bochkanov [ Tue Jun 22, 2010 6:54 pm ]
Post subject:  Re: Fitting Gaussian with lsfit

I see at least one error in your derivative calculation code:
Code:
state.g[0] = -1 / (Math.Exp(AP.Math.Sqr((state.x[0] - state.c[1]))/state.c[2]));


I think that c[2] should be squared. Similar errors may exists in other components of gradient/Hessian. Try to fix them. If it won't converge, we can see what's wrong, but most time ALGLIB users have problems with incorrectly calculated derivatives.

Author:  codudi [ Wed Jun 23, 2010 11:47 am ]
Post subject:  Re: Fitting Gaussian with lsfit

I rechecked the derivatives and indeed fixed one mistake, but still it can not find the min. BTW, to verify that my gradiant and Hessian are correct, I plug the parameters I have obtained from Matlab and the fits stopped at those value. See attached corrected code.

Attachments:
Program.txt [6.79 KiB]
Downloaded 877 times

Author:  Sergey.Bochkanov [ Wed Jun 23, 2010 5:12 pm ]
Post subject:  Re: Fitting Gaussian with lsfit

I've checked your code, you still have several errors in the derivatives calculation. You've lost several "minus" signs in the exponents. Correct code for gradient calculation will be:

Code:
if (state.needfg | state.needfgh)
{
    state.g[0] = -1 / Math.Exp(AP.Math.Sqr(state.x[0] - state.c[1])/AP.Math.Sqr(state.c[2]));
    state.g[1] = 2 * state.c[0] * (state.c[1] -state.x[0]) / AP.Math.Sqr(state.c[2]) * Math.Exp(-AP.Math.Sqr(state.c[1] - state.x[0]) / AP.Math.Sqr(state.c[2]));
    state.g[2] = -2 * state.c[0] * AP.Math.Sqr(state.c[1] - state.x[0]) / Math.Pow(state.c[2], 3) * Math.Exp(-AP.Math.Sqr(state.c[1] - state.x[0]) / AP.Math.Sqr(state.c[2]));
    state.g[3] = 1.0;
}


I've not checked Hessian calculation because it means too much analysis. Just moved to FG scheme and tried it - it didn't work. Then fixed gradients - and it worked with starting parameters not too far away from correct ones.

Author:  codudi [ Wed Jun 30, 2010 6:48 am ]
Post subject:  Re: Fitting Gaussian with lsfit

This is really interesting since I have derived my derivatives using Wolfram alpha (Mathematica). However, your modification fixed the issue. Do you think your procedures can be improved using parallelism?

Author:  Sergey.Bochkanov [ Thu Jul 01, 2010 8:19 am ]
Post subject:  Re: Fitting Gaussian with lsfit

codudi wrote:
This is really interesting since I have derived my derivatives using Wolfram alpha (Mathematica).

Maybe you've made mistake when coding them in C#. It is possible with such complex expressions. This is one of the reasons why I want to add automatic differentiation in one of the next releases. It will make possible to replace subsequent calls to MinLBFGSIteration with just one call like MinLBFGSOptimize("F(a,b,c,d)=d-a*exp(-(x-b)^2/c^2)") which will automatically and efficiently calculate and differentiate F.


codudi wrote:
Do you think your procedures can be improved using parallelism?

it can benefit from parallelism in two cases:
* when attempting to find global minimum using multistart technique (you can execute multiple optimizations in parallel)
* when you have very complex function whose calculation takes so much time that it is beneficial to calculate it in parallel
In your case (simple function, easy-to-find minimum), I think, parallelism won't help you.

Author:  jamespipers [ Thu Sep 02, 2010 4:47 pm ]
Post subject:  Re: Fitting Gaussian with lsfit

So all you need is to modify the proper fittings to avoid the problem. It just needs a bit of time and effort to modify it. You can actually do it by your own.


los angeles plumbing

Author:  Sergey.Bochkanov [ Thu Sep 02, 2010 5:58 pm ]
Post subject:  Re: Fitting Gaussian with lsfit

Yes, you are right. It was planned for 3.0, but 3.0 release took more time than expected, so I think that I will add it to one of the next release candidates scheduled for September along with other easy to implement functionality.

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