Extracting value from fit_report

3,903 views
Skip to first unread message

Nathaniel Brochu

unread,
Jul 4, 2016, 10:31:56 AM7/4/16
to lmfit-py
Hi,

I'm currently working on a fit and I'd like to know if it is possible to extract the incertitude value from de fit_report function. The goal would be to have three fits on my graph; one of the best fit, and two from the "worst" fit given so that I can show the range within the fit is good.

Thank you for your time and have a nice day.
Question.PNG

Matt Newville

unread,
Jul 4, 2016, 2:20:59 PM7/4/16
to lmfit-py
Nathaniel,

I'm not sure I understand the question, but I think you're asking how to get the uncertainties in the parameters after a fit.  Unfortunately, you attached an image instead of text of code, so it's a bit hard to guess what you are actually doing.   Please do not expect anyone to read code from an image. 

The fit result from either Model.fit() or minimize() has a `params` attribute that is an ordered dictionary of Parameters.   Each Parameter will have attributes `value` (the best fit value) and `stderr` (the standard error). See https://lmfit.github.io/lmfit-py/parameters.html  for more details.

If I misunderstood the question, please clarify.

--Matt

Nathaniel Brochu

unread,
Jul 5, 2016, 4:58:59 AM7/5/16
to lmfit-py, newv...@cars.uchicago.edu
Sorry for attach
Nathaniel,



What you said did help me a lot, altho right now my code uses the Parameters class and not Parameter. From the Parameters class, it is possible to easily get the valuesdict() function to have the value of each variable. However, it does not give the standard error and I can't seem to find a way to do so.

x1, x2, x3, y1, y2, y3 = seperate(x,y,rad,y_mW)

def lorentzian (params, x, y):
amp = params['amp'].value
cen = params['cen'].value
wid = params['wid'].value
model = (amp/np.pi)*(wid/((x-cen)**2+wid**2))
return model - y


params1=Parameters()
params1.add('amp', value = 3.39600134e-08)
params1.add('cen', value = 3.61278628e+09)
params1.add('wid', value = 1.26835853e+06)

params2=Parameters()
params2.add('amp', value = 9.4943e-08)
params2.add('cen', value = 7.17278628e+09)
params2.add('wid', value = 4.26835853e+06)

params3=Parameters()
params3.add('amp', value = 5.39600134e-07)
params3.add('cen', value = 10.05278628e+09)
params3.add('wid', value = 3.26835853e+07)

result1 = minimize(lorentzian, params1, args=(x1, y1))
result2 = minimize(lorentzian, params2, args=(x2, y2))
result3 = minimize(lorentzian, params3, args=(x3, y3))

final1= y1 + result1.residual
final2= y2 + result2.residual
final3= y3 + result3.residual

report_fit(result1.params)
report_fit(result2.params)
report_fit(result3.params)

l=params1.valuesdict()
print(l)

***Need someway to get the standard error of 'amp', 'cen' and 'wid'.

plt.plot(x, y_mW, 'bo')
plt.plot(x1, final1, 'r-')
plt.plot(x2, final2, 'r-')
plt.plot(x3, final3, 'r-')

plt.show()

Message has been deleted

Matt Newville

unread,
Jul 5, 2016, 7:57:29 AM7/5/16
to lmfit-py
With your

    result1 = minimize(lorentzian, params1, args=(x1, y1))

you can access everything you need:

   result1.params   :  the final Parameters
   result1.params['amp']  : the 'amp' Parameter 
   result1.params['amp'].value   : best fit value for 'amp'
   result1.params['amp'].stderr  : the uncertainty for 'amp'
   result1.params['amp'].correl  : dict of correlation values with other the other parameters

--Matt

Nathaniel Brochu

unread,
Jul 5, 2016, 10:24:21 AM7/5/16
to lmfit-py, newv...@cars.uchicago.edu
That's exactly it! Thank you very much for your time and fast answers!
Reply all
Reply to author
Forward
0 new messages