Skip to content
VidiMaster it, module by module
Module 4/Projections & Least Squares

Least-Squares Approximation & the Normal Equations

When Ax=bA\mathbf{x}=\mathbf{b} is inconsistent — b∉im⁡(A)\mathbf{b}\notin\operatorname{im}(A) — there is no exact solution, so we settle for the next best thing: the vector x^\hat{\mathbf{x}} that makes Ax^A\hat{\mathbf{x}} as close to b\mathbf{b} as possible. This lesson shows that the closest point in a subspace is the orthogonal projection (Theorem 8.12), that the minimiser x^\hat{\mathbf{x}} is found by solving the normal equation ATAx^=ATbA^{\mathsf{T}}A\hat{\mathbf{x}}=A^{\mathsf{T}}\mathbf{b} (Theorem 8.13), and how this turns messy, over-determined data into a single clean line of best fit.

Before you start — give these a try

Attempting first primes your brain for the lesson — even if you miss. Nothing is graded or saved; it's just a warm-up.

Solve the normal equation ATA x^=ATbA^{\mathsf{T}}A\,\hat{\mathbf{x}}=A^{\mathsf{T}}\mathbf{b} where ATA=[4223]A^{\mathsf{T}}A=\begin{bmatrix}4&2\\2&3\end{bmatrix} and ATb=[107]A^{\mathsf{T}}\mathbf{b}=\begin{bmatrix}10\\7\end{bmatrix}. Give x^=[x^1,x^2]\hat{\mathbf{x}}=[\hat{x}_1,\hat{x}_2].

Find the least-squares line y=mx+cy=mx+c through the points (1,1),(2,1),(3,2),(4,2),(5,3)(1,1),(2,1),(3,2),(4,2),(5,3). Give your answer as the vector [m,c][m,c] (to 3 decimals).

What you’ll be able to do

  • State Definition 8.9: a least-squares solution of Ax=bA\mathbf{x}=\mathbf{b} is a vector x^\hat{\mathbf{x}} minimising the error ∥b−Ax∥\|\mathbf{b}-A\mathbf{x}\|, and explain why it coincides with an exact solution when the system is consistent.
  • Use Theorem 8.12 to identify the point of a subspace VV closest to b\mathbf{b} as the orthogonal projection proj⁡Vb\operatorname{proj}_V\mathbf{b}.
  • Derive and solve the normal equation ATAx^=ATbA^{\mathsf{T}}A\hat{\mathbf{x}}=A^{\mathsf{T}}\mathbf{b} (Theorem 8.13) from the orthogonality condition b−Ax^∈im⁡(A)⊥=ker⁡(AT)\mathbf{b}-A\hat{\mathbf{x}}\in\operatorname{im}(A)^{\perp}=\ker(A^{\mathsf{T}}).
  • Decide when the least-squares solution is unique (ker⁡(A)={0}\ker(A)=\{\mathbf{0}\}) and compute it as x^=(ATA)−1ATb\hat{\mathbf{x}}=(A^{\mathsf{T}}A)^{-1}A^{\mathsf{T}}\mathbf{b}, using Corollary 8.1 to justify that ATAA^{\mathsf{T}}A is invertible.
  • Set up the design matrix for fitting a line y=mx+cy=mx+c (or a curve) to data and recover the projection formula proj⁡im⁡(A)b=A(ATA)−1ATb\operatorname{proj}_{\operatorname{im}(A)}\mathbf{b}=A(A^{\mathsf{T}}A)^{-1}A^{\mathsf{T}}\mathbf{b} (Theorem 8.14).

In your course

· MATH2015 · Linear Algebra & Probability
§8.4 Least squares approximation§8.3 Orthogonal projections
  • Theorem 8.12Best approximation by the orthogonal projection
    For b∈Rn\mathbf{b}\in\mathbb{R}^n and a subspace V⊆RnV\subseteq\mathbb{R}^n, proj⁡Vb\operatorname{proj}_V\mathbf{b} is the vector of VV closest to b\mathbf{b}: ∥b−proj⁡Vb∥<∥b−v∥\|\mathbf{b}-\operatorname{proj}_V\mathbf{b}\|<\|\mathbf{b}-\mathbf{v}\| for all v∈V, v≠proj⁡Vb\mathbf{v}\in V,\ \mathbf{v}\neq\operatorname{proj}_V\mathbf{b}.
  • Definition 8.9Least-squares solution
    A least-squares solution of Ax=bA\mathbf{x}=\mathbf{b} (with AA an n×mn\times m matrix) is a vector x^∈Rm\hat{\mathbf{x}}\in\mathbb{R}^m with ∥b−Ax^∥≤∥b−Ax∥\|\mathbf{b}-A\hat{\mathbf{x}}\|\le\|\mathbf{b}-A\mathbf{x}\| for all x∈Rm\mathbf{x}\in\mathbb{R}^m.
  • Theorem 8.13The normal equation for least-squares solutions
    The least-squares solutions of Ax=bA\mathbf{x}=\mathbf{b} are the exact solutions of ATAx^=ATbA^{\mathsf{T}}A\hat{\mathbf{x}}=A^{\mathsf{T}}\mathbf{b}. The solution is unique iff ker⁡(A)={0}\ker(A)=\{\mathbf{0}\}, and then x^=(ATA)−1ATb\hat{\mathbf{x}}=(A^{\mathsf{T}}A)^{-1}A^{\mathsf{T}}\mathbf{b}.
  • Theorem 8.14Orthogonal projection in terms of a basis
    If ker⁡(A)={0}\ker(A)=\{\mathbf{0}\}, then proj⁡im⁡(A)b=A(ATA)−1ATb\operatorname{proj}_{\operatorname{im}(A)}\mathbf{b}=A(A^{\mathsf{T}}A)^{-1}A^{\mathsf{T}}\mathbf{b}, so P=A(ATA)−1ATP=A(A^{\mathsf{T}}A)^{-1}A^{\mathsf{T}} is the matrix of the orthogonal projection onto im⁡(A)\operatorname{im}(A).
  • Corollary 8.1ATAA^{\mathsf{T}}A is invertible when ker⁡(A)={0}\ker(A)=\{\mathbf{0}\}
    If AA is n×mn\times m with ker⁡(A)={0}\ker(A)=\{\mathbf{0}\}, then the m×mm\times m matrix ATAA^{\mathsf{T}}A is invertible.
  • Theorem 8.11ker⁡(A)=ker⁡(ATA)\ker(A)=\ker(A^{\mathsf{T}}A)
  • Theorem 8.10im⁡(A)⊥=ker⁡(AT)\operatorname{im}(A)^{\perp}=\ker(A^{\mathsf{T}}) (orthogonal complement of the column space, from §8.3)
This lesson follows §8.4: Theorem 8.12 (closest point), Definition 8.9, and Theorem 8.13 (normal equation) are the headline results. Theorem 8.14 is the projection formula A(ATA)−1ATA(A^{\mathsf{T}}A)^{-1}A^{\mathsf{T}} recorded at the end of §8.4, generalising the orthonormal-basis matrix P=QQTP=QQ^{\mathsf{T}} of §8.3 (Theorem 8.8). The invertibility of ATAA^{\mathsf{T}}A rests on Theorem 8.11 and Corollary 8.1, and the identity im⁡(A)⊥=ker⁡(AT)\operatorname{im}(A)^{\perp}=\ker(A^{\mathsf{T}}) is Theorem 8.10.
1

Inconsistent systems and the least-squares idea

Many real problems lead to a system Ax=bA\mathbf{x}=\mathbf{b} with more equations than unknowns (an n×mn\times m matrix AA with n>mn>m). Measurement noise then usually pushes b\mathbf{b} outside the column space, b∉im⁡(A)\mathbf{b}\notin\operatorname{im}(A), so no x\mathbf{x} satisfies every equation exactly — the system is inconsistent. Rather than give up, we look for the best approximate solution. A least-squares solution (Definition 8.9) is a vector x^∈Rm\hat{\mathbf{x}}\in\mathbb{R}^m that minimises the Euclidean error ∥b−Ax^∥≤∥b−Ax∥for all x∈Rm.\|\mathbf{b}-A\hat{\mathbf{x}}\|\le\|\mathbf{b}-A\mathbf{x}\|\qquad\text{for all }\mathbf{x}\in\mathbb{R}^m. The name comes from minimising ∥b−Ax∥2\|\mathbf{b}-A\mathbf{x}\|^2, a sum of squares of the residual entries. If the system happens to be consistent, the minimum error is 00, and the least-squares solutions are exactly the ordinary solutions.

2

Best approximation is orthogonal projection

Minimising distance to a subspace has a clean geometric answer. Given b∈Rn\mathbf{b}\in\mathbb{R}^n and a subspace V⊆RnV\subseteq\mathbb{R}^n, Theorem 8.12 says the point of VV closest to b\mathbf{b} is the orthogonal projection proj⁡Vb\operatorname{proj}_V\mathbf{b}: ∥b−proj⁡Vb∥<∥b−v∥for every v∈V, v≠proj⁡Vb.\|\mathbf{b}-\operatorname{proj}_V\mathbf{b}\|<\|\mathbf{b}-\mathbf{v}\|\qquad\text{for every }\mathbf{v}\in V,\ \mathbf{v}\neq\operatorname{proj}_V\mathbf{b}. The proof is one application of Pythagoras: write b−v=(b−proj⁡Vb)+(proj⁡Vb−v)\mathbf{b}-\mathbf{v}=(\mathbf{b}-\operatorname{proj}_V\mathbf{b})+(\operatorname{proj}_V\mathbf{b}-\mathbf{v}). The first piece lies in V⊥V^{\perp} and the second in VV, so they are orthogonal and ∥b−v∥2=∥b−proj⁡Vb∥2+∥proj⁡Vb−v∥2\|\mathbf{b}-\mathbf{v}\|^2=\|\mathbf{b}-\operatorname{proj}_V\mathbf{b}\|^2+\|\operatorname{proj}_V\mathbf{b}-\mathbf{v}\|^2, which is smallest exactly when the last term is 00. Taking V=im⁡(A)V=\operatorname{im}(A), the best Ax^A\hat{\mathbf{x}} is therefore proj⁡im⁡(A)b\operatorname{proj}_{\operatorname{im}(A)}\mathbf{b}, and the residual b−Ax^\mathbf{b}-A\hat{\mathbf{x}} is orthogonal to the column space.

3

The normal equation

Orthogonality of the residual is what we can compute with. Since im⁡(A)⊥=ker⁡(AT)\operatorname{im}(A)^{\perp}=\ker(A^{\mathsf{T}}) (Theorem 8.10), the statement 'the residual is orthogonal to every column of AA' becomes Ax^=proj⁡im⁡(A)b  ⟺  b−Ax^∈ker⁡(AT)  ⟺  AT(b−Ax^)=0  ⟺  ATAx^=ATb.A\hat{\mathbf{x}}=\operatorname{proj}_{\operatorname{im}(A)}\mathbf{b}\iff\mathbf{b}-A\hat{\mathbf{x}}\in\ker(A^{\mathsf{T}})\iff A^{\mathsf{T}}(\mathbf{b}-A\hat{\mathbf{x}})=\mathbf{0}\iff A^{\mathsf{T}}A\hat{\mathbf{x}}=A^{\mathsf{T}}\mathbf{b}. This last system is the normal equation, and Theorem 8.13 says its exact solutions are precisely the least-squares solutions of Ax=bA\mathbf{x}=\mathbf{b} — and it is always consistent, even when the original system is not. The solution is unique if and only if ker⁡(A)={0}\ker(A)=\{\mathbf{0}\} (that is, AA has linearly independent columns); then ATAA^{\mathsf{T}}A is invertible (Corollary 8.1, via ker⁡(ATA)=ker⁡(A)\ker(A^{\mathsf{T}}A)=\ker(A) from Theorem 8.11) and x^=(ATA)−1ATb.\hat{\mathbf{x}}=(A^{\mathsf{T}}A)^{-1}A^{\mathsf{T}}\mathbf{b}.

4

Fitting a line or curve to data

The classic application is a line of best fit. To fit y=mx+cy=mx+c to data (x1,y1),…,(xn,yn)(x_1,y_1),\dots,(x_n,y_n), we would like mxi+c=yimx_i+c=y_i for every ii — the system [x11⋮⋮xn1]⏟A[mc]=[y1⋮yn]⏟b.\underbrace{\begin{bmatrix}x_1&1\\\vdots&\vdots\\x_n&1\end{bmatrix}}_{A}\begin{bmatrix}m\\c\end{bmatrix}=\underbrace{\begin{bmatrix}y_1\\\vdots\\y_n\end{bmatrix}}_{\mathbf{b}}. With more than two non-identical points this is over-determined and inconsistent, so we solve the normal equation ATA [ m  c ]T=ATbA^{\mathsf{T}}A\,[\,m\ \ c\,]^{\mathsf{T}}=A^{\mathsf{T}}\mathbf{b} instead; the unique pair (m,c)(m,c) minimises the total squared vertical error ∑i(yi−mxi−c)2\sum_i(y_i-mx_i-c)^2. The same recipe fits any model linear in its parameters — a parabola y=a+bx+cx2y=a+bx+cx^2 just uses columns 1, x, x21,\,x,\,x^2. Finally, when AA has independent columns, Theorem 8.14 packages the fitted values as a projection, proj⁡im⁡(A)b=A(ATA)−1ATb,\operatorname{proj}_{\operatorname{im}(A)}\mathbf{b}=A(A^{\mathsf{T}}A)^{-1}A^{\mathsf{T}}\mathbf{b}, a projection formula valid for any basis (the columns of AA), generalising P=QQTP=QQ^{\mathsf{T}} for an orthonormal basis.

Theorem 8.12 — Best approximation by the orthogonal projection

Let b∈Rn\mathbf{b}\in\mathbb{R}^n and let VV be a subspace of Rn\mathbb{R}^n. Then the orthogonal projection proj⁡Vb\operatorname{proj}_V\mathbf{b} is the unique vector of VV closest to b\mathbf{b}: ∥b−proj⁡Vb∥<∥b−v∥\|\mathbf{b}-\operatorname{proj}_V\mathbf{b}\|<\|\mathbf{b}-\mathbf{v}\| for all v∈V\mathbf{v}\in V with v≠proj⁡Vb\mathbf{v}\neq\operatorname{proj}_V\mathbf{b}.

Intuition. Drop a perpendicular from b\mathbf{b} to the 'floor' VV: the foot of that perpendicular is nearer than any other point of the floor. Since b−proj⁡Vb⊥V\mathbf{b}-\operatorname{proj}_V\mathbf{b}\perp V, for any other v∈V\mathbf{v}\in V the triangle with legs b−proj⁡Vb\mathbf{b}-\operatorname{proj}_V\mathbf{b} and proj⁡Vb−v\operatorname{proj}_V\mathbf{b}-\mathbf{v} is right-angled, and Pythagoras makes the hypotenuse b−v\mathbf{b}-\mathbf{v} strictly longer.
Theorem 8.13 — The normal equation for least-squares solutions

The least-squares solutions of Ax=bA\mathbf{x}=\mathbf{b} are exactly the (exact) solutions of the normal equation ATA x^=ATb.A^{\mathsf{T}}A\,\hat{\mathbf{x}}=A^{\mathsf{T}}\mathbf{b}. This solution is unique if and only if ker⁡(A)={0}\ker(A)=\{\mathbf{0}\}, and in that case x^=(ATA)−1ATb\hat{\mathbf{x}}=(A^{\mathsf{T}}A)^{-1}A^{\mathsf{T}}\mathbf{b}.

Intuition. The best fit Ax^A\hat{\mathbf{x}} is the projection of b\mathbf{b} onto im⁡(A)\operatorname{im}(A), so the residual b−Ax^\mathbf{b}-A\hat{\mathbf{x}} must be orthogonal to every column of AA. Stacking those orthogonality conditions is exactly AT(b−Ax^)=0A^{\mathsf{T}}(\mathbf{b}-A\hat{\mathbf{x}})=\mathbf{0}, which rearranges to ATAx^=ATbA^{\mathsf{T}}A\hat{\mathbf{x}}=A^{\mathsf{T}}\mathbf{b}. An inconsistent Ax=bA\mathbf{x}=\mathbf{b} is traded for a square system that is always solvable.
Theorem 8.14 — Orthogonal projection in terms of a basis

If the columns of AA form a basis of V=im⁡(A)V=\operatorname{im}(A) (equivalently ker⁡(A)={0}\ker(A)=\{\mathbf{0}\}), then the orthogonal projection of b\mathbf{b} onto VV is proj⁡Vb=Ax^=A(ATA)−1ATb,\operatorname{proj}_V\mathbf{b}=A\hat{\mathbf{x}}=A(A^{\mathsf{T}}A)^{-1}A^{\mathsf{T}}\mathbf{b}, so the projection matrix onto VV is P=A(ATA)−1ATP=A(A^{\mathsf{T}}A)^{-1}A^{\mathsf{T}}.

Intuition. Once x^=(ATA)−1ATb\hat{\mathbf{x}}=(A^{\mathsf{T}}A)^{-1}A^{\mathsf{T}}\mathbf{b} is the least-squares solution, the fitted vector Ax^A\hat{\mathbf{x}} is the closest point in im⁡(A)\operatorname{im}(A) — the projection. This frees you from first orthonormalising a basis: the earlier formula P=QQTP=QQ^{\mathsf{T}} (Theorem 8.8) needs an orthonormal QQ, whereas A(ATA)−1ATA(A^{\mathsf{T}}A)^{-1}A^{\mathsf{T}} works for any basis of VV, with the (ATA)−1(A^{\mathsf{T}}A)^{-1} factor correcting for non-orthogonality.
Corollary 8.1 — $A^{\mathsf{T}}A$ is invertible when $\ker(A)=\{\mathbf{0}\}$

If AA is an n×mn\times m matrix with ker⁡(A)={0}\ker(A)=\{\mathbf{0}\}, then the m×mm\times m matrix ATAA^{\mathsf{T}}A is invertible.

Intuition. By Theorem 8.11, ker⁡(ATA)=ker⁡(A)\ker(A^{\mathsf{T}}A)=\ker(A). If AA has a trivial kernel, so does the square matrix ATAA^{\mathsf{T}}A, which is therefore invertible. This is exactly what lets us write the unique least-squares solution in closed form as x^=(ATA)−1ATb\hat{\mathbf{x}}=(A^{\mathsf{T}}A)^{-1}A^{\mathsf{T}}\mathbf{b}.

Worked examples

Example 1

The system Ax=bA\mathbf{x}=\mathbf{b} with A=[111001]A=\begin{bmatrix}1&1\\1&0\\0&1\end{bmatrix} and b=[223]\mathbf{b}=\begin{bmatrix}2\\2\\3\end{bmatrix} is inconsistent. Find its least-squares solution x^\hat{\mathbf{x}}, the projection Ax^A\hat{\mathbf{x}} of b\mathbf{b} onto im⁡(A)\operatorname{im}(A), and the least-squares error ∥b−Ax^∥\|\mathbf{b}-A\hat{\mathbf{x}}\|.

  1. 1

    Confirm inconsistency: im⁡(A)=span⁡{(1,1,0),(1,0,1)}={(c1+c2, c1, c2)}\operatorname{im}(A)=\operatorname{span}\{(1,1,0),(1,0,1)\}=\{(c_1+c_2,\,c_1,\,c_2)\}. Matching the last two entries forces c1=2, c2=3c_1=2,\ c_2=3, but then the first entry would be 5≠25\neq 2, so b∉im⁡(A)\mathbf{b}\notin\operatorname{im}(A).

  2. 2

    Form the normal-equation pieces: ATA=[2112]A^{\mathsf{T}}A=\begin{bmatrix}2&1\\1&2\end{bmatrix} and ATb=[45]A^{\mathsf{T}}\mathbf{b}=\begin{bmatrix}4\\5\end{bmatrix}.

  3. 3

    Solve ATAx^=ATbA^{\mathsf{T}}A\hat{\mathbf{x}}=A^{\mathsf{T}}\mathbf{b} (Theorem 8.13). Since det⁡(ATA)=3≠0\det(A^{\mathsf{T}}A)=3\neq 0, (ATA)−1=13[2−1−12](A^{\mathsf{T}}A)^{-1}=\tfrac13\begin{bmatrix}2&-1\\-1&2\end{bmatrix}, so x^=13[2−1−12][45]=13[36]=[12]\hat{\mathbf{x}}=\tfrac13\begin{bmatrix}2&-1\\-1&2\end{bmatrix}\begin{bmatrix}4\\5\end{bmatrix}=\tfrac13\begin{bmatrix}3\\6\end{bmatrix}=\begin{bmatrix}1\\2\end{bmatrix}.

  4. 4

    Projection onto the column space: Ax^=1 (1,1,0)+2 (1,0,1)=(3,1,2)A\hat{\mathbf{x}}=1\,(1,1,0)+2\,(1,0,1)=(3,1,2).

  5. 5

    Residual and error: b−Ax^=(2,2,3)−(3,1,2)=(−1,1,1)\mathbf{b}-A\hat{\mathbf{x}}=(2,2,3)-(3,1,2)=(-1,1,1), which is orthogonal to both columns ((−1,1,1)⋅(1,1,0)=0(-1,1,1)\cdot(1,1,0)=0 and (−1,1,1)⋅(1,0,1)=0(-1,1,1)\cdot(1,0,1)=0), confirming the fit. Hence ∥b−Ax^∥=1+1+1=3≈1.732\|\mathbf{b}-A\hat{\mathbf{x}}\|=\sqrt{1+1+1}=\sqrt{3}\approx1.732.

Answer. x^=(1,2)\hat{\mathbf{x}}=(1,2); projection Ax^=(3,1,2)A\hat{\mathbf{x}}=(3,1,2); least-squares error ∥b−Ax^∥=3≈1.732\|\mathbf{b}-A\hat{\mathbf{x}}\|=\sqrt{3}\approx1.732.
Example 2

Find the line y=mx+cy=mx+c of best fit (least squares) through the points (0,1),(1,3),(2,4),(3,4)(0,1),(1,3),(2,4),(3,4).

Example 3

Use least squares to find the point of the plane V=span⁡{(1,1,1), (1,0,−1)}⊆R3V=\operatorname{span}\{(1,1,1),\,(1,0,-1)\}\subseteq\mathbb{R}^3 that is closest to b=(2,0,1)\mathbf{b}=(2,0,1), and compute the distance from b\mathbf{b} to VV.