【问题标题】:how can I calculate in curve fit the 95% confidence interval from the pcov values?我如何计算曲线拟合 pcov 值的 95% 置信区间?
【发布时间】:2022-02-21 15:11:10
【问题描述】:

我现在用 sigma_ab 打印的值是多少?如何计算 95 处的置信区间?

for g in all:
    c0 = 5
    c2 = 0.2
    c3 = 0.7
    start = g['y'].iloc[0]
    
    p0 = np.array([c0, c2, c3]), # Construct initial guess array

    popt, pcov = curve_fit(
         model, g['x'], g['y'],
         absolute_sigma=True, maxfev=100000
    )
    
    sigma_ab = np.sqrt(np.diagonal(pcov))
    n = g.name
    print(n+' Estimated parameters: \n', popt)
    print(n + ' Approximated errors: \n', sigma_ab)

这些是估计的参数

[0.24803625 0.06072472 0.46449578]

这是 sigma_ab,但我不知道它到底是什么。我想用 95% 的置信区间计算平均值的上限和下限。

[1.32778766 0.64261562 1.47915215]

【问题讨论】:

  • 这能回答你的问题吗? How to get confidence intervals from curve_fit
  • @mkrieger1 我看到了这篇文章并使用了以下代码:sigma_ab = np.sqrt(np.diagonal(pcov)),但我不确定这个输出的真正含义。这也是我在上面的代码中显示的:我希望有一个上限和下限。我无法下载不确定性
  • 我认为这更像是一个统计问题而不是编程问题。

标签: python curve-fitting confidence-interval


【解决方案1】:

您的sigma_ab(协方差对角元素的平方)将是 1-sigma (68.3%) 不确定性。如果您的不确定性分布是严格的高斯分布(通常是一个好的但不完美的假设,因此可能是“一个不错的初始估计”),那么 2-sigma (95.5%) 的不确定性将是这些值的两倍。

如果您想要更详细的度量(并且不假设对称不确定性),您可能会发现lmfit 及其Model 类很有帮助。默认情况下(并且在可能的情况下),它将从协方差中报告 1-sigma 不确定性,这很快,而且通常非常好。它还可以显式括起来并分别找出正负的 1-、2-、3-sigma 不确定性。 您没有给出一个非常完整的示例,因此很难说出您的模型功能在做什么。如果你有这样的模型函数:

def modelfunc(x, amp, cen, sigma):
    return amp * np.exp(-(x-cen)*(x-cen)/sigma**2)

你可以使用

import numpy as np
import lmfit

def modelfunc(x, amp, cen, sigma):
    return amp * np.exp(-(x-cen)*(x-cen)/sigma**2)

x = np.linspace(-10.0, 10.0, 201)
y = modelfunc(x, 3.0, 0.5, 1.1) + np.random.normal(scale=0.1, size=len(x))

model = lmfit.Model(modelfunc)
params = model.make_params(amp=5., cen=0.2, sigma=1)

result = model.fit(y, params, x=x)
print(result.fit_report())

# now calculate explicit 1-, 2, and 3-sigma uncertainties:
ci = result.conf_interval(sigmas=[1,2,3])
lmfit.printfuncs.report_ci(ci)

会打印出来

[[Model]]
    Model(modelfunc)
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 21
    # data points      = 201
    # variables        = 3
    chi-square         = 1.93360112
    reduced chi-square = 0.00976566
    Akaike info crit   = -927.428077
    Bayesian info crit = -917.518162
[[Variables]]
    amp:    2.97351225 +/- 0.03245896 (1.09%) (init = 5)
    cen:    0.48792611 +/- 0.00988753 (2.03%) (init = 0.2)
    sigma:  1.10931408 +/- 0.01398308 (1.26%) (init = 1)
[[Correlations]] (unreported correlations are < 0.100)
    C(amp, sigma) = -0.577
          99.73%    95.45%    68.27%    _BEST_    68.27%    95.45%    99.73%
 amp  :  -0.09790  -0.06496  -0.03243   2.97351  +0.03255  +0.06543  +0.09901
 cen  :  -0.03007  -0.01991  -0.00992   0.48793  +0.00991  +0.01990  +0.03004
 sigma:  -0.04151  -0.02766  -0.01387   1.10931  +0.01404  +0.02834  +0.04309

它给出了明确计算的不确定性,并表明 - 对于这种情况 - 1-sigma 不确定性的非常快速的估计非常好,并且 2-sigma 非常接近 1-sigma 值的 2 倍。就像,你不应该真的相信超过第二个有效数字......

最后,在您的示例中,您实际上并没有传递初始值,这说明了curve_fit 中的一个非常严重的缺陷。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多