forum.alglib.net

ALGLIB forum
It is currently Mon Jul 04, 2022 4:10 pm

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Fitting Gaussian with lsfit
PostPosted: Tue Jun 22, 2010 5:54 am 
Offline

Joined: Tue Jun 22, 2010 5:44 am
Posts: 3
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 688 times
Top
 Profile  
 
 Post subject: Re: Fitting Gaussian with lsfit
PostPosted: Tue Jun 22, 2010 6:54 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 874
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.


Top
 Profile  
 
 Post subject: Re: Fitting Gaussian with lsfit
PostPosted: Wed Jun 23, 2010 11:47 am 
Offline

Joined: Tue Jun 22, 2010 5:44 am
Posts: 3
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 639 times
Top
 Profile  
 
 Post subject: Re: Fitting Gaussian with lsfit
PostPosted: Wed Jun 23, 2010 5:12 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 874
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.


Top
 Profile  
 
 Post subject: Re: Fitting Gaussian with lsfit
PostPosted: Wed Jun 30, 2010 6:48 am 
Offline

Joined: Tue Jun 22, 2010 5:44 am
Posts: 3
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?


Top
 Profile  
 
 Post subject: Re: Fitting Gaussian with lsfit
PostPosted: Thu Jul 01, 2010 8:19 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 874
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.


Top
 Profile  
 
 Post subject: Re: Fitting Gaussian with lsfit
PostPosted: Thu Sep 02, 2010 4:47 pm 
Offline

Joined: Thu Sep 02, 2010 4:14 pm
Posts: 1
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


Last edited by jamespipers on Tue Sep 14, 2010 1:39 pm, edited 4 times in total.

Top
 Profile  
 
 Post subject: Re: Fitting Gaussian with lsfit
PostPosted: Thu Sep 02, 2010 5:58 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 874
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group