How to properly get the errors in lmfit

本秂侑毒 提交于 2021-02-11 17:42:21

问题


Hello I am trying to learn how to properly use lmfit and I think I am calculating the fit errors wrong. I have some data with errors on y and when I do the fit I call this (I tried for a simple linear fit):

weight = 1/err
out = line_fit.fit(y, pars, x=x, weights = weight)

I assumed that this would calculate the chi square and use the errors in the denominator. However it seems to not work properly. The fit looks good and I am getting a reasonable value for the errors, but if I increase the errors on purpose like err = 50*err, I am getting the exactly some fit parameters. But obviously the errors on the parameters now should be much bigger (by the propagation of error formula), but they are exactly the same. What am I doing wrong?

A second questions is, if I have errors on the x axis, how can I include that in the fit? There is just one weight parameter in the function call.

Thank you!


回答1:


It is a deliberate feature of lmfit.Model (and lmfit in general) to rescale the uncertainties so that they reflect a "good fit". Instead of parameter uncertainties that increase chi-square by 1, it reports parameter uncertainties that increase chi-square by reduced chi-square. This means that changing the scale of the uncertainties or fit weights will change the value of the fit statistics but not the reported uncertainties on the parameter values.

If you do want the parameter uncertainties to be those that increase chi-square by 1, use Model.fit(ydata, params, ..., scale_covar=False)

Uncertainties in x or any independent data are challenging to include in any automated way. The fit does not actually use the values of the independent data except to inform the model function about how to calculate the model (the y values) to compare to the provided data. You might consider

  • increase the uncertainty of each y value based on the change in that y value that would happen in response to the uncertainty in x, but there is not an automated way to this with the Model framework -- you would need to do this within your model function.

  • look into scipy.ODR which can handle uncertainties in both the dependent and independent data. This is not supported in lmfit, but you might find it useful.



来源:https://stackoverflow.com/questions/59296990/how-to-properly-get-the-errors-in-lmfit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!