python - 从 polyfit 中找出不确定性

标签 python numpy

我使用简单的 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/

相关文章:

python - 使用 numpy 滚动最大值

python - 如何移动 Pandas 数据框列的单个值

python - keras 中用于视频的 tensorflow 后端

python - 如何解析和保存多部分/相关类型=图像/jpeg 响应? (迪科姆·瓦多回应)

python - 使用python将特殊格式的文件解析为字典列表

python - Python3 重写字典行为

python - 高性能阵列意味着

python - 如何找到我拥有的提交版本?

python - 使用 python 和 matplotlib 获取箱线图中使用的值

python - 如何使用一个 numpy bool 数组修改另一个 numpy 数组?