Skip to content Skip to sidebar Skip to footer

How To Find Error On Slope And Intercept Using 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

Solution 1:

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])))

which outputs

x_1: 10.2069326441 +/- 0.368862837662
x_2: 1.82929420943 +/- 0.213500166807

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.

Post a Comment for "How To Find Error On Slope And Intercept Using Numpy.polyfit"