physics2u
Tier
⌕ Search ⌘K
Derivation

Linear Least Squares and the Normal Equations

D-121 Home PU-106 Threads chance Depends on Least Squares from Gaussian Likelihood, matrix-inverse-and-transpose
Statement

For a model that is linear in its parameters, y = Ap + noise, the weighted least-squares estimate that minimises χ2 = (yAp)TW(yAp) satisfies the normal equations (ATWA) = 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
The model is linear in the parameters p.If y depends non-linearly on p (e.g. an exponential decay rate), no fixed matrix A exists; the objective is no longer quadratic and has no closed-form minimiser — you must iterate (Gauss–Newton / Levenberg–Marquardt). The weight matrix W is symmetric and positive-definite.If W is not symmetric, the cross-term identity used to combine the two linear pieces fails and the gradient picks up an extra antisymmetric part. If it is not positive-definite, χ2 is not bounded below and the stationary point need not be a minimum. The columns of A are linearly independent (rank A = M, so NM).If dropped, ATWA is singular and non-invertible: the fit is degenerate (e.g. two identical basis functions), the solution is not unique, and a pseudo-inverse or regularisation is required to pick one. W is known independently of the data y.If the weights are estimated from the same residuals being fitted, the estimator becomes biased and the tidy covariance (ATWA)−1 understates the true variance.
Derivation
1
χ2(p) = (yAp)T W (yAp)
Objective: the weighted sum of squared residuals. For Gaussian noise with covariance C and W = C−1 this is −2 ln (likelihood) up to a constant, so minimising it maximises the likelihood. A
2
χ2 = yTWyyTWAppTATWy + pTATWAp
Multiply out the quadratic form, using (Ap)T = pTAT. No approximation — pure distribution of the bilinear form. A
3
yTWAp = pTATWy  ⇒  χ2 = yTWy − 2pTATWy + pTATWAp
Each middle term is a 1×1 scalar, so it equals its own transpose: (yTWAp)T = pTATWTy, and WT = W by symmetry. The two cross-terms are therefore identical and combine. B
4
∂(aTp)/∂p = a,  ∂(pTMp)/∂p = (M + MT)p
Standard vector-calculus gradients. Here a = ATWy and M = ATWA. Because W is symmetric, MT = ATWTA = M, so (M + MT) = 2M. B
5
∂χ2/∂p = −2ATWy + 2ATWAp
Apply the two gradient rules from step 4 term by term; the constant yTWy vanishes. A
6
∂χ2/∂p = 0  at  p =
Stationarity: at a minimum the gradient vanishes. Since χ2 is a quadratic with Hessian 2ATWA, which is positive-definite when A has full column rank and W > 0, the stationary point is the unique global minimum. B
7
−2ATWy + 2ATWA = 0  ⇒  (ATWA) = ATWy
Divide by 2 and move the data term to the right-hand side. These are the normal equations. A
8
= (ATWA)−1 ATWy
Left-multiply by the inverse of ATWA, which exists precisely under the full-rank assumption. A
Result
(ATWA)  = ATWy  ⇒   = (ATWA)−1ATWy,  Cov() = (ATWA)−1 (when W = C−1)

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 j comes out in [pj]. Consistent.

Limiting cases
  • Uniform weights, W = I: reduces to ordinary least squares, ATA = 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 = Σi wiyi / Σi wi.
  • Square, invertible A (N = M): the fit interpolates exactly, = 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 WC−1 (e.g. correlated errors treated as independent), 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 = (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 WC−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 = yA is orthogonal to every column of A in the metric defined by W: ATW = 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 = 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 , 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 ; 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/(NM) ≈ 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.

1
A = [[1,0],[1,1],[1,2],[1,3]],  p = (cm)T,  y = (1,3,2,5)T
First column of ones for the intercept, second column the x-values. A
2
ATA = [[N, Σx], [Σx, Σx²]] = [[4, 6], [6, 14]]
Σx = 0+1+2+3 = 6, Σx² = 0+1+4+9 = 14, N = 4. A
3
ATy = [Σy, Σxy]T = [11, 22]T
Σy = 1+3+2+5 = 11, Σxy = 0+3+4+15 = 22. A
4
det = 4·14 − 6·6 = 20,  (ATA)−1 = (1/20)[[14, −6], [−6, 4]]
2×2 inverse: swap diagonal, negate off-diagonal, divide by determinant. A
5
c = (14·11 − 6·22)/20 = 22/20 = 1.10,  m = (−6·11 + 4·22)/20 = 22/20 = 1.10
Apply = (ATA)−1ATy. A
y = 1.10 + 1.10 x

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

1
wi = 1/σi²:  w1 = 1/0.25 = 4.00, w2 = 1/0.09 = 11.11, w3 = 1/0.16 = 6.25
Weights are inverse variances, not inverse standard deviations. A
2
ATWA = Σwi = 4.00 + 11.11 + 6.25 = 21.36
With A a single ones-column, ATWA is the scalar sum of weights. A
3
ATWy = Σwiyi = 4.00·10.2 + 11.11·9.8 + 6.25·10.5 = 215.3
Weighted projection of the data onto the constant model. A
4
μ̂ = ATWy / ATWA = 215.3 / 21.36 = 10.08
Normal equation in one dimension. A
5
σμ = (ATWA)−1/2 = 1/√21.36 = 0.216
Covariance is (ATWA)−1 = 1/21.36 = 0.0468, and σ is its square root. B
μ̂ = 10.08 ± 0.22

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
  1. (A) Solve a given system. With ATWA = [[10, 4], [4, 6]] and ATWy = [20, 12]T, find .
    Solutiondet = 10·6 − 4·4 = 60 − 16 = 44. Inverse = (1/44)[[6, −4], [−4, 10]]. 1 = (6·20 − 4·12)/44 = (120 − 48)/44 = 72/44 = 1.636. 2 = (−4·20 + 10·12)/44 = (−80 + 120)/44 = 40/44 = 0.909. So = (1.64, 0.91)T.
  2. (B) Reduce to the weighted mean. Show directly from (ATWA) = ATWy that a one-parameter constant model gives = Σwiyiwi, and give its variance.
    SolutionTake A = (1,…,1)T (N×1) and W = diag(wi). Then ATWA = Σi wi (scalar) and ATWy = Σi wiyi. The normal equation (Σwi) = Σwiyi gives = Σwiyiwi. Variance = (ATWA)−1 = 1/Σwi = 1/Σ(1/σi²).
  3. (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.
    SolutionN = 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.
  4. (C) Covariance identity. Assuming = (ATWA)−1ATWy with y = Aptrue + n, Cov(n) = C, prove that when W = C−1 the covariance of equals (ATC−1A)−1.
    SolutionWrite L = (ATWA)−1ATW. Then = Ly = LAptrue + Ln. Since LA = (ATWA)−1(ATWA) = I, the estimator is unbiased: = ptrue + Ln. Cov() = 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. ∎
  5. (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.
    SolutionATWA 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 NM). 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).