9

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?

Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
jbssm
  • 6,861
  • 13
  • 54
  • 81

1 Answers1

4

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))
askewchan
  • 45,161
  • 17
  • 118
  • 134
  • 1
    Happy to help, @jbssm. By the way, when using `np.polyfit`,`np.polyval`, `np.poly1d`, etc., don't combine them with any of the `np.polynomial` module functions, as they [follow different conventions, specifically the return ordering](http://stackoverflow.com/a/18767992/1730674). Normally it's recommended to use the [`np.polynomial` package](http://docs.scipy.org/doc/numpy/reference/routines.polynomials.polynomial.html) exclusively, but for some reason [it doesn't provide the `covariance`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.polynomial.polynomial.polyfit.html) – askewchan Oct 30 '13 at 01:11