Linear Least Squares and the Normal Equations
Statement
For a model that is linear in its parameters, y = Ap + noise, the weighted least-squares estimate p̂ that minimises χ2 = (y − Ap)TW(y − Ap) satisfies the normal equations (ATWA)p̂ = ATWy, where A is the N×M design matrix, y is the vector of N observations, and W is a symmetric positive-definite weight matrix.
Why it matters
Fitting a straight line, a polynomial, a spectral baseline, or a sum of basis functions to data are all the same computation once written in matrix form. The normal equations turn "find the best curve" into "solve one linear system," making the whole of linear regression a single closed-form result rather than an iterative search.
The weight matrix W is where the physics of measurement enters. Taking W = C−1, the inverse of the data covariance, is exactly what maximising a Gaussian likelihood demands, so the estimator is not an ad-hoc recipe but the statistically optimal (minimum-variance unbiased) linear estimator. It also delivers the parameter uncertainties for free as (ATWA)−1.
Assumptions
Derivation
Result
Reading. The best-fit parameters are a linear map applied to the data. The matrix ATWA (the "curvature" or Fisher information matrix) measures how sharply χ2 rises away from the minimum; its inverse is the parameter covariance, so directions the data constrain poorly show up as large eigenvalues of (ATWA)−1. The right-hand side ATWy is the weighted projection of the data onto each basis function.
Units check. Let observation i have units [y] and column j of A have units [y]/[pj], and W = C−1 have units [y]−2. Then a diagonal entry (ATWA)jj has units ([y]/[pj])²·[y]−2 = [pj]−2, so (ATWA)−1jj carries [pj]² — correctly a variance in the parameter. And ATWy has units ([y]/[pj])·[y]−2·[y] = [pj]−1, so p̂j comes out in [pj]. Consistent.
Limiting cases
- Uniform weights, W = I: reduces to ordinary least squares, ATAp̂ = ATy.
- Diagonal W = diag(1/σi²): independent measurements; each equation is weighted by inverse variance.
- Single column of ones, A = (1,…,1)T: the estimate collapses to the weighted mean p̂ = Σi wiyi / Σi wi.
- Square, invertible A (N = M): the fit interpolates exactly, p̂ = A−1y, and the weights drop out.
- One dominant weight wk → ∞: the fit is forced through point k, an exact linear constraint on p.
Breaks when
- Rank-deficient design (collinearity). If two columns of A are equal or nearly proportional, ATWA is singular or ill-conditioned; the inverse blows up and the fitted parameters swing wildly on tiny data changes. Forming the normal equations squares the condition number, so even a moderately collinear problem loses precision — QR or SVD on A directly is safer.
- Non-linear parameter dependence. If the model is y = f(p) with f non-linear (decay rates, frequencies, peak positions), there is no design matrix and χ2 is not quadratic; the single-shot normal equations do not apply and one must linearise and iterate.
- Outliers / non-Gaussian noise. The L2 (squared) penalty assumes Gaussian errors; a single gross outlier can dominate ATWy and drag the whole fit. Heavy-tailed data need robust (e.g. Huber, L1) losses, which do not reduce to linear normal equations.
- Wrong or misspecified weights. If W ≠ C−1 (e.g. correlated errors treated as independent), p̂ stays unbiased but is no longer minimum-variance, and the reported covariance (ATWA)−1 is simply wrong.
Failure modes
- Forgetting W is symmetric and keeping the two cross-terms separate, producing a spurious factor or an asymmetric gradient in step 3–4.
- Solving by explicit inverse p̂ = (ATWA)−1ATWy numerically instead of Cholesky-solving the linear system — wastes precision and can fail on ill-conditioned problems.
- Putting the intercept in the wrong place: omitting the column of ones in A, which silently forces the fit through the origin.
- Using wi = 1/σi instead of 1/σi² — the weight is the inverse variance, not inverse standard deviation.
- Reading (ATWA)−1 as the covariance when W ≠ C−1; in general the covariance is the sandwich (ATWA)−1(ATWCWA)(ATWA)−1.
- Transposing the design matrix the wrong way so that A is M×N — then ATWA has the wrong shape and the code errors or, worse, runs on a transposed problem.
Discussion
Geometrically the normal equations say the residual r̂ = y − Ap̂ is orthogonal to every column of A in the metric defined by W: ATWr̂ = 0. The fit is the W-orthogonal projection of the data vector onto the column space of A. Best-fit does not mean the residual is zero; it means the residual has no component left inside the model subspace, so no adjustment of p could reduce it further. This projection picture is the reason least squares is so robust and universal — it is linear algebra, not statistics, once W is fixed.
The matrix ATWA is the Fisher information for a Gaussian linear model. Its inverse saturates the Cramér–Rao bound, so with W = C−1 the least-squares estimator is the best possible unbiased estimator — no cleverer method can do better with the same data. This connects three threads that look unrelated at first: minimising a sum of squares (numerical), maximising a Gaussian likelihood (statistical), and projecting orthogonally in a weighted inner product (geometric). They are one theorem seen from three sides.
The choice W = C−1 is singled out by the Gauss–Markov theorem: among all linear unbiased estimators p̂ = Ly, the one with weight C−1 has the smallest covariance in the positive-definite (Loewner) ordering, independently of whether the noise is actually Gaussian — Gaussianity is only needed to make it the maximum-likelihood and the minimum-variance estimator among all estimators. Any other symmetric positive-definite W still yields an unbiased p̂, but its covariance (ATWA)−1(ATWCWA)(ATWA)−1 is larger, degrading gracefully rather than becoming wrong.
Common misconceptions. "Least squares assumes Gaussian noise" — not to compute p̂; the estimator is defined by the geometry and is unbiased for any zero-mean noise. Gaussianity is what makes it maximum-likelihood and lets you read off exact confidence regions. Also: a large final χ2 does not mean the algebra failed; it means the model or the assumed error bars are wrong, and reduced χ2 = χ2/(N−M) ≈ 1 is the target.
Worked examples
Example 1 — Unweighted straight-line fit. Fit y = c + mx to the four points (0, 1), (1, 3), (2, 2), (3, 5) with equal errors, so W = I.
Reading. Intercept and slope both 1.10 (dimensionless here). Residuals are (−0.1, 0.8, −1.3, 0.6), summing to 0 as the ones-column orthogonality guarantees.
Example 2 — Weighted mean of three measurements. Combine y = 10.2 ± 0.5, 9.8 ± 0.3, 10.5 ± 0.4 (independent). Here A = (1,1,1)T and W = diag(1/σi²).
Reading. The combined estimate is pulled toward the most precise measurement (9.8 ± 0.3, weight 11.1) and its uncertainty 0.22 is smaller than any single error bar — the payoff of optimal weighting.
Problems
- (A) Solve a given system. With ATWA = [[10, 4], [4, 6]] and ATWy = [20, 12]T, find p̂.
Solution
det = 10·6 − 4·4 = 60 − 16 = 44. Inverse = (1/44)[[6, −4], [−4, 10]]. p̂1 = (6·20 − 4·12)/44 = (120 − 48)/44 = 72/44 = 1.636. p̂2 = (−4·20 + 10·12)/44 = (−80 + 120)/44 = 40/44 = 0.909. So p̂ = (1.64, 0.91)T. - (B) Reduce to the weighted mean. Show directly from (ATWA)p̂ = ATWy that a one-parameter constant model gives p̂ = Σwiyi/Σwi, and give its variance.
Solution
Take A = (1,…,1)T (N×1) and W = diag(wi). Then ATWA = Σi wi (scalar) and ATWy = Σi wiyi. The normal equation (Σwi)p̂ = Σwiyi gives p̂ = Σwiyi/Σwi. Variance = (ATWA)−1 = 1/Σwi = 1/Σ(1/σi²). - (B) Straight-line fit with data. Fit y = c + mx (equal weights) to (1, 2.1), (2, 3.9), (3, 6.2), (4, 7.8). Report m and c.
Solution
N = 4, Σx = 10, Σx² = 1+4+9+16 = 30, Σy = 2.1+3.9+6.2+7.8 = 20.0, Σxy = 2.1+7.8+18.6+31.2 = 59.7. ATA = [[4,10],[10,30]], det = 120 − 100 = 20. m = (NΣxy − ΣxΣy)/det = (4·59.7 − 10·20.0)/20 = (238.8 − 200)/20 = 38.8/20 = 1.94. c = (Σx²Σy − ΣxΣxy)/det = (30·20.0 − 10·59.7)/20 = (600 − 597)/20 = 3/20 = 0.15. So y = 0.15 + 1.94x. - (C) Covariance identity. Assuming p̂ = (ATWA)−1ATWy with y = Aptrue + n, Cov(n) = C, prove that when W = C−1 the covariance of p̂ equals (ATC−1A)−1.
Solution
Write L = (ATWA)−1ATW. Then p̂ = Ly = LAptrue + Ln. Since LA = (ATWA)−1(ATWA) = I, the estimator is unbiased: p̂ = ptrue + Ln. Cov(p̂) = LCLT = (ATWA)−1ATW C WA(ATWA)−1. Set W = C−1: the middle WCW = C−1CC−1 = C−1 = W, so the expression becomes (ATWA)−1(ATWA)(ATWA)−1 = (ATWA)−1 = (ATC−1A)−1. ∎ - (C) When is the solution unique? State and justify the condition on A and W for ATWA to be invertible, and give a concrete rank-deficient example.
Solution
ATWA is invertible iff it is positive-definite, i.e. iff vTATWAv > 0 for all v ≠ 0. Writing u = Av, this is uTWu > 0, which holds when W is positive-definite and u = Av ≠ 0 for every v ≠ 0 — that is, A must have full column rank M (linearly independent columns, requiring N ≥ M). Example: fitting y = p1 + p2 + p3x gives columns (1,…), (1,…), (x,…); the first two are identical, so v = (1, −1, 0)T satisfies Av = 0, ATWA is singular, and p1, p2 are individually undetermined (only their sum is fixed).