估计scipy.odr中拟合参数的标准偏差? [英] Estimate the standard deviation of fitted parameters in scipy.odr?
问题描述
(与这个问题有些相关 线性拟合包括所有NumPy/SciPy 的错误,并从这个python 中的线性拟合,x 和 y 坐标都不确定)
(Somewhat related to this question 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.odr(代码如下),我得到:
I fit a linear model (y=a*x+b
) using fixed errors in x,y
using scipy.odr (code is below), and I get:
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
有什么关系?
What is the correct standard deviation values for the fitted a, b
parameters? I'm assuming these must be obtained from the Squared diagonal covariance
values, but then how are these values related to the Standard errors
?
添加
如 如何从 ODR 计算标准误差的答案中所述结果? ali_m
,这显然与 ascipy.odr 中的错误.如果使用
As mentioned in the answer to How to compute standard error from ODR results? by ali_m
, this is apparently related to a bug in scipy.odr. If one uses
np.sqrt(np.diag(out.cov_beta * out.res_var))
(即:将协方差乘以残差方差)而不仅仅是
(i.e: multiply the covariance by the residual variance) instead of just
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))
?
So now my question is: which is the proper standard deviation for the fitted parameters (a, b)
? Is it out.sd_beta
(equivalently: np.sqrt(np.diag(out.cov_beta * out.res_var))
) or 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
包含估计参数的标准偏差,相当于参数协方差矩阵.
Yes, out.sd_beta
contains the standard deviations for the estimated parameters, which are equivalent to the square roots of the diagonal terms in the parameter covariance matrix.
正如您在上面已经提到的,scipy.odr
中存在一个错误,这意味着您必须将 out.cov_beta
乘以剩余方差 out.res_var
以便推导出参数的实际协方差矩阵.
As you've already mentioned above, there's a bug in scipy.odr
that means you have to multiply out.cov_beta
by the residual variance out.res_var
in order to derive the actual covariance matrix for the parameters.
这篇关于估计scipy.odr中拟合参数的标准偏差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!