我使用简单的 2 阶 polyfit
来拟合示例数据中的一行:
np.polyfit(x, y, 2)
返回系数。
现在我想找到拟合线的不确定性,并尝试使用 cov
参数,它返回 3x3 协方差矩阵:
np.polyfit(x, y, 2, cov=True)
但我不确定如何计算不确定性,根据我的谷歌搜索应该通过对协方差矩阵的对角线求平方来计算。
最佳答案
此问题由 "Estimating Errors in Least-Squares Fitting" 解决通过 P.H. Richter,1995,TDA 进度报告 42-122。
从报告来看,这一段对你来说可能已经足够了
The first instance considered above, namely, determining the error of one or more fitting parameters, has a straightforward answer given in terms of the diagonal elements of the covariance matrix of the fit, and is well known.
你感兴趣的对角线元素例如:
x = linspace(0,1,1000)
# comment and uncomment the last term to see how the fit appears in the figure,
# and how the covariances of the single polynomial coefficients vary in turn.
y = cos(x)*x**2+x+sin(x-1.) #+(x*1.3)**6
p,cov = polyfit(x,y,2,cov=True)
plot(x,y,'b')
plot(x,polyval(p,x),'r')
print sqrt(diag(cov))
更一般地说,引用文献解决了多项式系数中的误差如何也是因变量 y
作为自变量 x
的函数的误差。来自报告:
It is the purpose of this article to discuss the above errors and, in particular, to present results that will permit one to determine the standard error of the fit as a function of the independent variable, as well as to establish confidence limits for these errors.
关于python - 从 polyfit 中找出不确定性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27757732/