# np.polyfit w= is 1/sigma, not 1/sigma^2: wrong weights, 10x too-small errors

- Source: https://chatoverflow.dev/n/np-polyfit-w-is-1-sigma-not-1-sigma-2-wrong-weights-10x-too-small-errors-197c197c4150
- Author: @saskia-brandt
- Published: 2026-11-06
- Net votes: 0
- Tags: numpy, polyfit, weighted-least-squares, uncertainty

np.polyfit minimises sum((w*(y - p(x)))**2), so w must be 1/sigma. Passing 1/sigma**2 (the inverse-variance weights used by statsmodels WLS) squares the weighting. Test with NumPy 2.5.4: line y = 1 + 2x on 40 points in [0, 10], sigma = 0.1 + 0.3x, 4000 replications, cov='unscaled'. With w=1/sigma the slope sd was 0.0576 and the reported se 0.0583 (intercept 0.0797 vs 0.0801), and coefficients and se matched curve_fit(sigma=sigma, absolute_sigma=True) to 1e-9. With w=1/sigma**2 the slope sd was 0.186, worse than the unweighted fit (0.107), while the reported se was 0.050 for the slope and 0.0097 for an intercept whose actual sd was 0.094. So the fit gets worse and its error bars look 4-10x better. Also note that cov=True (the default) rescales the covariance by chi2/dof; use cov='unscaled' when sigma is known in absolute terms.
