估计 scipy.odr 中拟合参数的标准差?

Estimate the standard deviation of fitted parameters in scipy.odr?

(与这个问题有些相关Linear fit including all errors with NumPy/SciPy, and borrowing code from this one Linear fitting in python with uncertainty in both x and y coordinates)

我使用 scipy.odrx,y 中使用固定误差拟合线性模型 (y=a*x+b)(代码如下),我得到:

Parameters (a, b): [ 5.21806759 -4.08019995]
Standard errors: [ 0.83897588  2.33472161]
Squared diagonal covariance: [ 1.06304228  2.9582588 ]

拟合的 a, b 参数的正确标准偏差值是多少?我假设这些必须从 Squared diagonal covariance 值中获得,但是这些值与 Standard errors 有什么关系?

添加

by ali_m, this is apparently related to a bug in scipy.odr 的回答中所述。如果一个人使用

np.sqrt(np.diag(out.cov_beta * out.res_var))

(即:将协方差乘以残差)而不是

np.sqrt(np.diag(out.cov_beta))

结果现在与 out.sd_beta 一致。

所以现在我的问题是:拟合参数 (a, b) 的正确标准偏差是多少?是out.sd_beta(相当于:np.sqrt(np.diag(out.cov_beta * out.res_var)))还是np.sqrt(np.diag(out.cov_beta))





import numpy as np
from scipy.odr import Model, RealData, ODR
import random

random.seed(9001)
np.random.seed(117)

def getData(c):
    """Initiate random data."""
    x = np.array([0, 1, 2, 3, 4, 5])
    y = np.array([i**2 + random.random() for i in x])
    xerr = c * np.array([random.random() for i in x])
    yerr = c * np.array([random.random() for i in x])
    return x, y, xerr, yerr

def linear_func(p, x):
    """Linear model."""
    a, b = p
    return a * x + b

def fitModel(x, y, xerr, yerr):
    # Create a model for fitting.
    linear_model = Model(linear_func)
    # Create a RealData object using our initiated data from above.
    data = RealData(x, y, sx=xerr, sy=yerr)
    # Set up ODR with the model and data.
    odr = ODR(data, linear_model, beta0=[0., 1.])
    # Run the regression.
    out = odr.run()

    # Estimated parameter values
    beta = out.beta
    print("Parameters (a, b): {}".format(beta))
    # Standard errors of the estimated parameters
    std = out.sd_beta
    print("Standard errors: {}".format(std))
    # Covariance matrix of the estimated parameters
    cov = out.cov_beta
    stddev = np.sqrt(np.diag(cov))
    print("Squared diagonal covariance: {}".format(stddev))


# Generate data and fit the model.
x, y, xerr, yerr = getData(1.)
fitModel(x, y, xerr, yerr)

是的,out.sd_beta包含估计参数的标准差,相当于参数协方差矩阵中对角线项的平方根。

正如您在上面已经提到的,scipy.odr 中存在一个错误,这意味着您必须将 out.cov_beta 乘以残差方差 out.res_var 才能得出实际的协方差矩阵对于参数。