Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find error on slope and intercept using numpy.polyfit

Tags:

python

numpy

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?

like image 384
ylangylang Avatar asked Dec 24 '14 09:12

ylangylang


People also ask

How do I use Polyfit in NumPy?

In this program, also, first, import the libraries matplotlib and numpy. Set the values of x and y. Then, calculate the polynomial and set new values of x and y. Once this is done, fit the polynomial using the function polyfit().

What does Polyfit return Python?

polyfit will only return an equation that goes through all the points (say you have N) if the degree of the polynomial is at least N-1. Otherwise, it will return a best fit that minimises the squared error.

Is Polyfit same as linear regression?

Both models uses Least Squares, but the equation on which these Least Squares are used is completely different. polyfit applies it on the vandemonde matrix while the linear regression does not.

How do you fit a polynomial to data in Python?

To get the least-squares fit of a polynomial to data, use the polynomial. polyfit() in Python Numpy. The method returns the Polynomial coefficients ordered from low to high. If y was 2-D, the coefficients in column k of coef represent the polynomial fit to the data in y's k-th column.


1 Answers

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.

like image 197
Greg Avatar answered Oct 18 '22 22:10

Greg