我使用简单polyfit的2阶来拟合样本数据中的一行:
np.polyfit(x, y, 2)
Run Code Online (Sandbox Code Playgroud)
返回系数.
现在我想找到拟合线的不确定性,并尝试使用cov返回3x3协方差矩阵的参数:
np.polyfit(x, y, 2, cov=True)
Run Code Online (Sandbox Code Playgroud)
但我不知道如何计算不确定性,根据我的Google搜索应该通过平方协方差矩阵的对角线来计算.
PH Richter 的“估计最小二乘拟合中的错误”(1995年,TDA进度报告42-122 )解决了这个问题。
从报告中,此段可能已经足够了
上面考虑的第一实例,即确定一个或多个拟合参数的误差,具有根据拟合的协方差矩阵的对角线元素给出的简单答案,并且是众所周知的。
您感兴趣的对角线元素例如:
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))
Run Code Online (Sandbox Code Playgroud)
更一般而言,该参考文献解决了多项式系数中的该误差如何同时也是因变量的误差,该误差是自变量y的函数x。从报告中:
本文的目的是讨论上述误差,尤其是提出结果,使人们能够确定拟合的标准误差作为自变量的函数,并为这些误差建立置信限。