Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the weight values to use in numpy polyfit and what is the error of the fit

I'm trying to do a linear fit to some data in numpy.

Ex (where w is the number of samples I have for that value, i.e. for the point (x=0, y=0) I only have 1 measurement and the value of that measurement is 2.2, but for the point (1,1) I have 2 measurements with a value of 3.5.

x = np.array([0, 1, 2, 3])
y = np.array([2.2, 3.5, 4.6, 5.2])
w = np.array([1, 2, 2, 1])

z = np.polyfit(x, y, 1, w = w)

So, now the question is: is it correct to use w=w in polyfit for these cases or should I use w = sqrt(w) of what should I use?

Also, how can I get the fit error from polyfit?

like image 292
jbssm Avatar asked Oct 29 '13 19:10

jbssm


People also ask

What is Numpy Polyfit in Python?

In python, Numpy polyfit() is a method that fits the data within a polynomial function. That is, it least squares the function polynomial fit. For example, a polynomial p(X) of deg degree fits the coordinate points (X, Y).

What is Numpy Polyfit return?

By the way, numpy. 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.

What does the Polyfit function do?

Polyfit is a Matlab function that computes a least squares polynomial for a given set of data. Polyfit generates the coefficients of the polynomial, which can be used to model a curve to fit the data. Polyval evaluates a polynomial for a given set of x values.

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

If you have normally distributed measurements, then your uncertainty in each value would be proportional to 1/sqrt(n) where n is the number of measurements. You want to weigh your fit by the inverse of your uncertainty, so your second guess is best: w=np.sqrt(n)

To get the covariance on your parameters, also give cov=True.

x = np.array([0, 1, 2, 3])
y = np.array([2.2, 3.5, 4.6, 5.2])
n = np.array([1, 2, 2, 1])

p, c = np.polyfit(x, y, 1, w=np.sqrt(n), cov=True)

The diagonals of your cov matrix are the individual variances on each parameter, and of course the off-diagonals are the covariances. So most likely what you want for "fit error" is the square root of these diagonals:

e = np.sqrt(np.diag(c))
like image 167
askewchan Avatar answered Sep 17 '22 17:09

askewchan