lmfit中拟合参数的不确定性

4
我希望能够找到最简单的方法来输出拟合参数的不确定性。在使用 spo.curve_fit 进行拟合时,我们只需获得协方差矩阵,然后取对角线并开平方即可找到不确定性。但是在使用 lmfit 进行拟合时似乎并没有那么简单。
我的拟合代码如下:
import lmfit 


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)


print 'a_lm2_unc =', a_lm2.stderr

当我生成拟合报告时,会得到不确定性值,因此它们显然已经被计算出来了。我的问题是如何调用和使用它们。我尝试使用上面代码的最后一行中的stderr仅打印参数的不确定性,但这只返回“None”。我可以获得协方差矩阵,但我不知道显示的顺序是什么。我的终极目标是简单地获得数值及其相关的不确定度,然后将它们放入数组中以便在我的代码中进一步使用。
2个回答

4
不确定性隐藏在模型的.stderr属性中。例如:
gmodel = Model(function_tofit)
fit_params = Parameters()
fit_params.add('parameter1',value=guess1)
fit_params.add('parameter2',value=guess2)
...
result = gmodel.fit(y_to_fit, fit_params, x = x_to_fit)

#get the output value of the fit
mean_fit_value = result.params['parameter1'].value
std_fit_value = result.params['parameter1'].stderr

在您的情况下,您打印了fit_params['your_parameter'].stderr,显然这是None,因为您没有指定参数的先验信息。请记住,fit_params是您的输入,而params是您想要的输出。


0

因为我没有你的数据,所以无法测试,但看起来你已经接近成功了。你的 "gausfit2" 应该是一个 ModelFit 对象 (http://cars9.uchicago.edu/software/python/lmfit/model.html#model.ModelFit)。因此,你只需要执行以下操作即可生成报告:

print gausfit2.fit_report #will print you a fit report from your model

#you should also be able to access the best fit parameters and other associated attributes. For example, you can use gausfit2.best_values to obtain a dictionary of the best fit values for your params, or gausfit2.covar to obtain the covariance matrix you are interested in. 

print gausfit2.covar

#one suggestion to shorten your writing is to just create a parameters class with a shorter name and add your params to that.

params = lmfit.Parameters()
params.add('a', value=a_est) #and so on...

干杯!


我忘了提醒你也要检查lmfit中的F统计选项(http://cars9.uchicago.edu/software/python/lmfit/confidence.html),因为你的置信区间不总是对称的(尽管在这个高斯示例中可能是)。然而,我建议选择一个更合适的F统计量,因为内置的会导致相对较紧的置信区间。这里有一个链接解释为什么这很重要: http://stackoverflow.com/questions/30276163/how-to-understand-f-test-based-lmfit-confidence-intervals/30282340#30282340 - XtremeJake

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接