从 polyfit 中找出不确定性
Find uncertainty from polyfit
我使用 2 阶的简单 polyfit
来拟合示例数据中的一行:
np.polyfit(x, y, 2)
其中 returns 个系数。
现在我想找到拟合线的不确定性,并尝试使用 cov
参数,其中 returns 3x3 协方差矩阵:
np.polyfit(x, y, 2, cov=True)
但我不确定如何计算不确定性,根据我的 Google 搜索应该通过对协方差矩阵的对角线求平方来计算。
此问题已由 P.H "Estimating Errors in Least-Squares Fitting" 解决。 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.
为了您的方便,我根据 gg349 的回答为 Python 3 制作了一个完整的示例。
import numpy as np
import matplotlib.pyplot as plt
x = np.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 = np.cos(x) * x**2 + x + np.sin(x - 1.) \
# + (x * 1.3)**6
p, cov = np.polyfit(x, y, 2, cov=True)
plt.plot(x, y)
plt.plot(x, np.polyval(p,x))
plt.show()
print(np.sqrt(np.diag(cov)))
我使用 2 阶的简单 polyfit
来拟合示例数据中的一行:
np.polyfit(x, y, 2)
其中 returns 个系数。
现在我想找到拟合线的不确定性,并尝试使用 cov
参数,其中 returns 3x3 协方差矩阵:
np.polyfit(x, y, 2, cov=True)
但我不确定如何计算不确定性,根据我的 Google 搜索应该通过对协方差矩阵的对角线求平方来计算。
此问题已由 P.H "Estimating Errors in Least-Squares Fitting" 解决。 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.
为了您的方便,我根据 gg349 的回答为 Python 3 制作了一个完整的示例。
import numpy as np
import matplotlib.pyplot as plt
x = np.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 = np.cos(x) * x**2 + x + np.sin(x - 1.) \
# + (x * 1.3)**6
p, cov = np.polyfit(x, y, 2, cov=True)
plt.plot(x, y)
plt.plot(x, np.polyval(p,x))
plt.show()
print(np.sqrt(np.diag(cov)))