如何正确获取lmfit中的错误 [英] How to properly get the errors in lmfit

查看:98
本文介绍了如何正确获取lmfit中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试学习如何正确使用lmfit,并且我认为我计算出的拟合误差是错误的.我有一些在y上有错误的数据,当我进行拟合时,我称之为(我尝试进行简单的线性拟合):

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)

我假设这将计算卡方并使用分母中的误差.但是,它似乎无法正常工作.拟合看起来不错,并且为错误提供了一个合理的值,但是如果我故意增加错误,例如 err = 50 * err ,则可以得到一些完全适合的参数.但是显然,现在参数上的误差应该更大(通过误差公式的传播),但是它们是完全相同的.我究竟做错了什么?

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?

第二个问题是,如果我在x轴上有错误,如何将其包含在拟合中?函数调用中只有一个权重参数.

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.

谢谢!

推荐答案

这是 lmfit.Model (通常是 lmfit )的故意功能,可以重新确定不确定性使它们体现出良好的契合度".报告的参数不确定性不是将卡方增加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.

如果您确实希望参数不确定性是那些将卡方增加1的不确定性,请使用 Model.fit(ydata,params,...,scale_covar = False)

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

x 中的不确定性或任何独立数据很难以任何自动化方式包括在内.拟合实际上不使用独立数据的值,只是告知模型函数如何计算模型( y 值)以与提供的数据进行比较.您可能会考虑

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

  • 根据每个 y 值的变化来增加每个 y 值的不确定性,而该变化将响应 x 中的不确定性而发生,但是使用Model框架并没有自动的方法-您需要在模型函数中执行此操作.

  • 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.

查看 scipy.ODR ,它可以处理相关数据和独立数据中的不确定性.lmfit 不支持此功能,但是您可能会发现它很有用.

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.

这篇关于如何正确获取lmfit中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆