Home › Linear Functions › Lesson 8

Least Squares and Orthogonal Projection

Lesson 8 of 12

Key idea of this section

minimise ‖Ax − b‖²   ⇔   AᵀA x̂ = Aᵀb      (normal equations)
A x̂ = Pb is the orthogonal projection of b onto the column space of A
If A has full column rank:  x̂ = (AᵀA)⁻¹Aᵀb,   P = A(AᵀA)⁻¹Aᵀ

Overdetermined systems

A system Ax = b with A of size m × n and m > n typically has no solution: there are more equations than unknowns, and the column space of A is a proper subspace of ℝᵐ that b usually misses. This is the normal situation when fitting a model to data. If we measure points (xᵢ, yᵢ), i = 1, …, m, and seek a line y = c + mx through all of them, we obtain m equations in the two unknowns c, m, and the data are almost never exactly collinear. The method of least squares, published by Adrien-Marie Legendre in 1805 and justified probabilistically by Carl Friedrich Gauss in 1809 (who claimed to have used it since 1795, notably to predict the return of the asteroid Ceres in 1801), replaces the unsolvable system by the problem of minimising the sum of squared residuals ‖Ax − b‖².

Theorem 8.1 (Normal equations)

A vector x̂ minimises ‖Ax − b‖² over ℝⁿ if and only if AᵀAx̂ = Aᵀb. Such an x̂ always exists. It is unique if and only if the columns of A are linearly independent, in which case x̂ = (AᵀA)⁻¹Aᵀb.

Proof. Suppose AᵀAx̂ = Aᵀb, so the residual r = b − Ax̂ satisfies Aᵀr = 0, meaning r is orthogonal to every column of A and hence to the column space. For any x, the vector A(x̂ − x) lies in the column space, so by Pythagoras

‖Ax − b‖² = ‖A(x − x̂) − r‖² = ‖A(x − x̂)‖² + ‖r‖² ≥ ‖r‖² = ‖Ax̂ − b‖²,

with equality exactly when A(x − x̂) = 0. So x̂ is a minimiser, and the minimisers are precisely x̂ + ker A, which all satisfy the normal equations. Existence: the column space of AᵀA equals the column space of Aᵀ (Problem 37 shows rank AᵀA = rank A, and im AᵀA ⊆ im Aᵀ always), so Aᵀb lies in the image of AᵀA. Finally, ker AᵀA = ker A: if AᵀAx = 0 then ‖Ax‖² = xᵀAᵀAx = 0. Hence AᵀA is invertible exactly when ker A = {0}. ∎

The projection interpretation

Geometrically, Ax̂ is the point of the column space closest to b, and b − Ax̂ is perpendicular to that subspace. When A has full column rank the map b ↦ Ax̂ is the linear map P = A(AᵀA)⁻¹Aᵀ, which statisticians call the hat matrix because it "puts the hat on y". It satisfies P² = P and Pᵀ = P, and conversely every symmetric idempotent matrix is the orthogonal projection onto its image (Problem 39). Its trace equals its rank, n. The residual map I − P projects onto the orthogonal complement of the column space.

Simple linear regression

For the model y ≈ c + mx with data (x₁, y₁), …, (xN, yN), the columns of A are (1, …, 1) and (x₁, …, xN), and the normal equations are

N c   + (Σxᵢ) m  = Σyᵢ
(Σxᵢ) c + (Σxᵢ²) m = Σxᵢyᵢ

Eliminating c gives the familiar formulas m = Sxy/Sxx and c = ȳ − m x̄, where x̄, ȳ are the means, Sxx = Σ(xᵢ − x̄)² and Sxy = Σ(xᵢ − x̄)(yᵢ − ȳ). The first normal equation says the residuals sum to zero, so the fitted line passes through the centroid (x̄, ȳ) of the data (Problem 40).

Worked example. Fit a line to (1, 6), (2, 5), (3, 7), (4, 10), the data shown in the picture accompanying this course. Here x̄ = 2.5, ȳ = 7, Sxx = 2.25 + 0.25 + 0.25 + 2.25 = 5 and Sxy = (−1.5)(−1) + (−0.5)(−2) + (0.5)(0) + (1.5)(3) = 1.5 + 1 + 0 + 4.5 = 7. So m = 7/5 = 1.4 and c = 7 − 1.4·2.5 = 3.5: the least-squares line is y = 3.5 + 1.4x. As a check against the normal equations: Σxᵢ = 10, Σxᵢ² = 30, Σyᵢ = 28, Σxᵢyᵢ = 6 + 10 + 21 + 40 = 77, and indeed 4(3.5) + 10(1.4) = 28 and 10(3.5) + 30(1.4) = 77. The residuals are 1.1, −1.3, −0.7, 0.9; they sum to 0, their x-weighted sum 1.1 − 2.6 − 2.1 + 3.6 is 0, and the residual sum of squares is 1.21 + 1.69 + 0.49 + 0.81 = 4.2.

Numerical practice

Forming AᵀA squares the condition number of the problem: if A has singular values σ₁ ≥ ⋯ ≥ σₙ > 0, then κ(AᵀA) = (σ₁/σₙ)² = κ(A)². Numerically stable algorithms therefore avoid the normal equations. With a QR factorisation A = QR (Q with orthonormal columns, R upper triangular and invertible), the normal equations become Rx̂ = Qᵀb, solved by back substitution. With the singular value decomposition A = UΣVᵀ, the minimum-norm least-squares solution, valid even when A is rank-deficient, is x̂ = A⁺b, where A⁺ = VΣ⁺Uᵀ is the Moore–Penrose pseudoinverse and Σ⁺ inverts the nonzero singular values.

Least squares is the special case p = 2 of fitting in the ℓᵖ norm. The ℓ¹ problem (least absolute deviations) and the ℓ∞ problem (Chebyshev or minimax fitting) are not solved by linear equations but are linear programmes (Lesson 10 and Problem 56); for a constant model, ℓ² fitting gives the mean and ℓ¹ fitting gives a median.

Exercise

  • (a)
    Fit the line y = mx through the origin to (1, 2), (2, 3), (3, 7).
  • (b)
    Project b = (1, 2, 3) orthogonally onto the line spanned by (1, 1, 1).
  • (c)
    Find the least-squares line for (0, 0), (1, 1), (2, 3).
Show solution

Answers: (a) Here A is the single column (1, 2, 3), so m = Σxᵢyᵢ/Σxᵢ² = (2 + 6 + 21)/14 = 29/14. (b) The projection onto span{u} is (uᵀb/uᵀu)u = (6/3)(1, 1, 1) = (2, 2, 2). (c) x̄ = 1, ȳ = 4/3, Sxx = 2, Sxy = (−1)(−4/3) + 0 + (1)(5/3) = 3, so m = 3/2 and c = 4/3 − 3/2 = −1/6: y = −1/6 + (3/2)x.

V.3.1