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