问题描述
我用numpy.polyfit将一条直线拟合到一些数据.数据本身不带有任何错误栏.这是我的代码的简化版本:
I'm fitting a straight line to some data with numpy.polyfit. The data themselves do not come with any error bars. Here's a simplified version of my code:
from numpy import polyfit
data = loadtxt("data.txt")
x,y = data[:,0],data[:,1]
fit = polyfit(x,y,1)
这当然给了我斜率和截距的值,但是如何找到最佳拟合值的不确定性呢?
Of course that gives me the values for the slope and intercept, but how to I find the uncertainty on the best-fit values?
推荐答案
我回答这个问题有点晚了,但是我认为这个问题仍然没有得到解答,对我来说,这是Google上的热门话题.因此,我认为以下是正确的方法
I'm a bit late to answer this, but I think that this question remains unanswered and was the top hit on Google for me. Therefore, I think the following is the correct method
x = np.linspace(0, 1, 100)
y = 10 * x + 2 + np.random.normal(0, 1, 100)
p, V = np.polyfit(x, y, 1, cov=True)
print "x_1: {} +/- {}".format(p[0], np.sqrt(V[0][0]))
print "x_2: {} +/- {}".format(p[1], np.sqrt(V[1][1]))
输出
x_1: 10.2069326441 +/- 0.368862837662
x_2: 1.82929420943 +/- 0.213500166807
因此,您需要返回协方差矩阵V
,对于该矩阵,对角线的平方根是每个拟合系数的估计标准偏差.当然,这可以推广到更高的维度.
So you need to return the covariance matrix, V
, for which the square root of the diagonals are the estimated standard-deviation for each of the fitted coefficients. This of course generalised to higher dimensions.
这篇关于如何使用numpy.polyfit查找坡度上的错误并进行拦截的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!