我有一些来自实验室的数据,我正在用它来适应这个功能

F(S,参数)=AMP*(S/SP)/(1+S/SP+1.041)+BG(不知如何打字设置)

我将absolute_sigma=True设置为curve_fit,以获得参数(SP、AMP、BG)的绝对不确定度,但np.sqrt(np.diag(pcov))的计算误差似乎不合理.请看下面的图表.蓝点是数据.红线是f(s, *popt).绿线将最优sp值替换为从np.sqrt(np.diag(pcov))计算得出的sp-it误差.橙色的线是SP+相同的值.

enter image description here

我预计+/-线将更接近红线.

下面是一个最小的例子:

# Function for fitting
def scattering_rate(s, sp, amp, bg):
    return amp * (s/sp) / (1 + s/sp + (-20/19.6)**2) + bg

# data
s = np.array([0.6, 1.2, 2.3, 4.3, 8.1, 15.2, 28.5, 53.4])
y = np.array([8.6, 8.5, 8.9, 9.5, 10.6, 12.6, 15.5, 18.3])

# Fit data to saturated scattering rate
popt, pcov = curve_fit(scattering_rate, s, y, absolute_sigma=True)
print('Fit parameters', popt)
print('Fit uncertainties', np.sqrt(np.diag(pcov)))

# Construct fit from optimized parameters
fit_s = np.linspace(np.min(s), np.max(s), 100)
fit = scattering_rate(fit_s, *popt)

# Consider one error difference in sp value
fit_plus_err = saturation_power(fit_s, popt[0] + np.sqrt(np.diag(pcov))[0], popt[1], popt[2])
fit_minus_err = saturation_power(fit_s, popt[0] - np.sqrt(np.diag(pcov))[0], popt[1], popt[2])

# Plot
plt.plot(s, y, '-o', markeredgecolor='k', label='data')
plt.plot(fit_s, fit_plus_err, label='sp + err')
plt.plot(fit_s, fit_minus_err, label='sp - err')
plt.plot(fit_s, fit, '-r', label='opt sp')
plt.xlabel('s')
plt.ylabel('y')
plt.legend()
plt.grid()
plt.show()

编辑

Following @jlandercy's answer, we need the error bars of the original data which are y_err = array([0.242, 0.231, 0.282, 0.31 , 0.373]). Including that in curve_fit's sigma argument, the results look much better though still a bit distance enter image description here

推荐答案

为什么

当使用absolute_sigma=True时,建议您也给sigmaswitch 供电,如果不是,则将它们替换为1.,使X平方损失函数表现为RSS,如果您的不确定性不是单一的,则pcov变得不知何故变得没有意义.

修整

提供不确定性或对其的估计,以获得有意义的pcov矩阵.

将您的数据设置为sigma0.15将显著改善pcov矩阵,同时保持卡方统计显著:

sy = np.full(y.size, 0.15)
popt, pcov = curve_fit(scattering_rate, s, y, sigma=sy, absolute_sigma=True)
np.sqrt(pcov)

# array([[3.25479068, 2.07084837, 0.45391942],
#       [2.07084837, 1.35773667, 0.2563505 ],
#       [0.45391942, 0.2563505 , 0.09865549]]) 

enter image description here enter image description here

更新

您有两个直接选项来改善错误:

  • 收集更多的分数(3个参数得到5分,得到dof=2分,这是非常不对称的);
  • 减少y上的不确定性.

下图显示了对具有您的等效参数的合成数据集的影响:

enter image description here enter image description here enter image description here

Python相关问答推荐

在函数内部使用eval(),将函数的输入作为字符串的一部分

ModuleNotFound错误:没有名为Crypto Windows 11、Python 3.11.6的模块

PywinAuto在Windows 11上引发了Memory错误,但在Windows 10上未引发

django禁止直接分配到多对多集合的前端.使用user.set()

从groupby执行计算后创建新的子框架

Python,Fitting into a System of Equations

将tdqm与cx.Oracle查询集成

在np数组上实现无重叠的二维滑动窗口

为什么抓取的HTML与浏览器判断的元素不同?

如何在Polars中从列表中的所有 struct 中 Select 字段?

如何根据一列的值有条件地 Select 前N组?

调用decorator返回原始函数的输出

isinstance()在使用dill.dump和dill.load后,对列表中包含的对象失败

在pandas/python中计数嵌套类别

Numpyro AR(1)均值切换模型抽样不一致性

Gekko中基于时间的间隔约束

如何使用matplotlib查看并列直方图

如何在信号的FFT中获得正确的频率幅值

如何训练每一个pandaprame行的线性回归并生成斜率

使用xlsxWriter在EXCEL中为数据帧的各行上色