b_lm2 = lmfit.Parameter('b', value=b_est)
x0_core_lm2 = lmfit.Parameter('x0_core', value=gaus1['x0_core'])
x0_1_lm2 = lmfit.Parameter('x0_1', value=gaus1['x0_1'])
x0_2_lm2 = lmfit.Parameter('x0_2', value=gaus1['x0_2'])
x0_3_lm2 = lmfit.Parameter('x0_3', value=gaus1['x0_3'])
x0_4_lm2 = lmfit.Parameter('x0_4', value=gaus1['x0_4'])
sig_core_lm2 = lmfit.Parameter('sig_core', value=gaus1['sig_core'])
sig_1_lm2 = lmfit.Parameter('sig_1', value=gaus1['sig_1'])
sig_2_lm2 = lmfit.Parameter('sig_2', value=gaus1['sig_2'])
sig_3_lm2 = lmfit.Parameter('sig_3', value=gaus1['sig_3'])
sig_4_lm2 = lmfit.Parameter('sig_4', value=gaus1['sig_4'])
m_lm2 = lmfit.Parameter('m', value=m, vary=False)
c_lm2 = lmfit.Parameter('c', value=c, vary=False)
gausfit2 = mod.fit(y, x=x, a=a_lm2, b=b_lm2, x0_core=x0_core_lm2, x0_1=x0_1_lm2, x0_2=x0_2_lm2,
x0_3=x0_3_lm2, x0_4=x0_4_lm2, sig_core=sig_core_lm2, sig_1=sig_1_lm2, sig_2=sig_2_lm2,
sig_3=sig_3_lm2, sig_4=sig_4_lm2, m=m_lm2, c=c_lm2,weights=None, scale_covar=False)
I'm looking for the easiest way of outputting the uncertainty in the fitted parameters. With spo.curve_fit, we just get the covariance matrix when we fit and we can take the diagonal and square root to find the uncertainties. With lmfit it doesn't seem to be so simple.
My fitting looks like this:
**************************
a_lm2 = lmfit.Parameter('a', value=a_est)b_lm2 = lmfit.Parameter('b', value=b_est)
x0_core_lm2 = lmfit.Parameter('x0_core', value=gaus1['x0_core'])
x0_1_lm2 = lmfit.Parameter('x0_1', value=gaus1['x0_1'])
x0_2_lm2 = lmfit.Parameter('x0_2', value=gaus1['x0_2'])
x0_3_lm2 = lmfit.Parameter('x0_3', value=gaus1['x0_3'])
x0_4_lm2 = lmfit.Parameter('x0_4', value=gaus1['x0_4'])
sig_core_lm2 = lmfit.Parameter('sig_core', value=gaus1['sig_core'])
sig_1_lm2 = lmfit.Parameter('sig_1', value=gaus1['sig_1'])
sig_2_lm2 = lmfit.Parameter('sig_2', value=gaus1['sig_2'])
sig_3_lm2 = lmfit.Parameter('sig_3', value=gaus1['sig_3'])
sig_4_lm2 = lmfit.Parameter('sig_4', value=gaus1['sig_4'])
m_lm2 = lmfit.Parameter('m', value=m, vary=False)
c_lm2 = lmfit.Parameter('c', value=c, vary=False)
gausfit2 = mod.fit(y, x=x, a=a_lm2, b=b_lm2, x0_core=x0_core_lm2, x0_1=x0_1_lm2, x0_2=x0_2_lm2,
x0_3=x0_3_lm2, x0_4=x0_4_lm2, sig_core=sig_core_lm2, sig_1=sig_1_lm2, sig_2=sig_2_lm2,
sig_3=sig_3_lm2, sig_4=sig_4_lm2, m=m_lm2, c=c_lm2,weights=None, scale_covar=False)
Thanks a lot Matt!
I see how to obtain the standard error values now. On your first point that my method is not at all the recommended approach, the reason I wasn’t just passing in the initial values is because I want to implement bounds on my parameters later. How would you recommend doing this?
Thanks again,
Daniel
Thanks a lot Matt!
I see how to obtain the standard error values now. On your first point that my method is not at all the recommended approach, the reason I wasn’t just passing in the initial values is because I want to implement bounds on my parameters later. How would you recommend doing this?
Thanks Matt. So I managed to get this working using the first method.
However, when I implement bounds I sometimes find that the stderr is given as 0. This seems to be the case when I have more restrictive bounds and the parameters end up equal to the bounded values. Do you know why this is?
From: matt.n...@gmail.com [mailto:matt.n...@gmail.com] On Behalf Of Matt Newville
Sent: samedi 9 mai 2015 22:54
To: Daniel Marx
Cc: lmfit-py
Subject: Re: Uncertainty on fitted parameters
On Sat, May 9, 2015 at 1:45 PM, Daniel Marx <natura...@gmail.com> wrote:
Thanks Matt. So I managed to get this working using the first method.
However, when I implement bounds I sometimes find that the stderr is given as 0. This seems to be the case when I have more restrictive bounds and the parameters end up equal to the bounded values. Do you know why this is?
I've frequently run into this lack of uncertainties when the fitted values are near a boundary. In my case it often happens when(1) I've set any boundary at all(2) my initial guess is not good enoughUnder these circumstances it seems like the fitting method is wandering away from the true minimum, until it encounters a downward slope that passes through the boundary, and gets stuck.
Is there a robust way to determine when this situation has been encountered? Often in these situations I can identify the true minimum with a better guess, but I'd like to know when that occurs. At the moment I am simply checking to see if any of the .stderr values are zero, but this doesn't seem entirely reliable. Is there (or could there be) some boolean flag indicating that uncertainties could not be calculated?
Btw, when we provide weights in Model.fit (which I assume should be provided as 1/sigma^2), do the magnitude of these affect the outputted standard errors from the fit? In spo.curve_fit, we have the parameter absolute_sigma that means that sigma describes one standard deviation so errors are assumed to be absolute not relative. Can I get the same thing in lmfit?
--
You received this message because you are subscribed to the Google Groups "lmfit-py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
To post to this group, send email to lmfi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/08beb955-0cde-4dbb-94cd-564f1ba62c43%40googlegroups.com.
Hi Daniel,
On Jun 15, 2015 8:33 AM, "Daniel Marx" <natura...@gmail.com> wrote:
>
> Hi Matt,
>
> I was just looking again at this. For some reason, the uncertainties I'm getting from fits with lmfit and spo.curve_fit are quite different. I've set them so both should be taking absolute errors as the input. Any ideas why this might be?
A simple example that shows the difference would be very helpful. Can you provide such an example?
--Matt
> Le jeudi 21 mai 2015 18:02:35 UTC+2, Matt Newville a écrit :
>>
>> Hi Daniel,
>>
>>
>> On Thu, May 21, 2015 at 3:01 AM, Daniel Marx <natura...@gmail.com> wrote:
>>>
>>> Btw, when we provide weights in Model.fit (which I assume should be provided as 1/sigma^2), do the magnitude of these affect the outputted standard errors from the fit? In spo.curve_fit, we have the parameter absolute_sigma that means that sigma describes one standard deviation so errors are assumed to be absolute not relative. Can I get the same thing in lmfit?
>>>
>>
>> The weights should normally be 1/sigma, not 1/sigma^2. I think the scipy.optimize.curve_fit documentation may be mistaken about that, but I haven't looked in detail at that. In lmfit, the array "weight*(model-data)" is minimized in the least squares sense.
>>
>> By default, lmfit does rescale the covariance matrix so that the weights are "relative", not "absolute". In lmfit, this is optional but called "scale_covar" (defaulting to True). You can set this to False (akin to absolute_sigma=True) in "minimize()" or "Model.fit()".
>>
>> --Matt
>>
>>
>>
>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups "lmfit-py" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
>>> To post to this group, send email to lmfi...@googlegroups.com.
>>> To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/08beb955-0cde-4dbb-94cd-564f1ba62c43%40googlegroups.com.
>>>
>>> For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>>
>> --
>> --Matt Newville <newville at cars.uchicago.edu> 630-252-0431
>
> --
> You received this message because you are subscribed to the Google Groups "lmfit-py" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
> To post to this group, send email to lmfi...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/2ed22c09-775b-4778-88aa-cf58acc4ef26%40googlegroups.com.
Don’t worry, I think I found my mistake. spo.curve_fit seems to take the sigma value itself whereas lmfit seems to take the weight (=1/sigma). If I do this, then they seem to agree to a few decimal places at least. Can you confirm that this is correct?
From: matt.n...@gmail.com [mailto:matt.n...@gmail.com] On Behalf Of Matt Newville
Sent: lundi 15 juin 2015 20:16
To: Daniel Marx
Cc: lmfit-py
Subject: Re: Uncertainty on fitted parameters
Hi Daniel,
Don’t worry, I think I found my mistake. spo.curve_fit seems to take the sigma value itself whereas lmfit seems to take the weight (=1/sigma). If I do this, then they seem to agree to a few decimal places at least. Can you confirm that this is correct?