# forum.alglib.net

ALGLIB forum
 It is currently Wed Sep 11, 2024 7:32 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.

 Page 1 of 1 [ 8 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Fitting Gaussian with lsfitPosted: Tue Jun 22, 2010 5:54 am

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 783 times
Top

 Post subject: Re: Fitting Gaussian with lsfitPosted: Tue Jun 22, 2010 6:54 pm

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

 Post subject: Re: Fitting Gaussian with lsfitPosted: Wed Jun 23, 2010 11:47 am

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.

Top

 Post subject: Re: Fitting Gaussian with lsfitPosted: Wed Jun 23, 2010 5:12 pm

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

 Post subject: Re: Fitting Gaussian with lsfitPosted: Wed Jun 30, 2010 6:48 am

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

 Post subject: Re: Fitting Gaussian with lsfitPosted: Thu Jul 01, 2010 8:19 am

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

Top

 Post subject: Re: Fitting Gaussian with lsfitPosted: Thu Sep 02, 2010 4:47 pm

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

 Post subject: Re: Fitting Gaussian with lsfitPosted: Thu Sep 02, 2010 5:58 pm

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

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 8 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: No registered users and 49 guests

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

Search for: