问题
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 thaty
value that would happen in response to the uncertainty inx
, 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 inlmfit
, but you might find it useful.
来源:https://stackoverflow.com/questions/59296990/how-to-properly-get-the-errors-in-lmfit