Projection onto a subspace and least squares
The question
Where on a plane would you put a point to get closest to a target off the plane?
- 1 SeeMove a point across a plane.
- 2 TryPredict what rescaling a column changes.
- 3 ProveBuild the closest-point proof.
- 4 ApplyFit measurements and check residuals.
Before you start · 3 quick checks on column combinations, dot products and lengths
Two ideas before you start
2 ideasColumns give directions you can move
Adding a multiple of the second column moves the output parallel to that column.
Each coefficient sets how much of a column to use. The output is the sum of those contributions.
Exact definition
Let \(A\) be a real matrix with \(m\) rows and \(n\) columns \(a_1,\ldots,a_n\), with \(m,n\) positive integers. Its column space \(S=\operatorname{Col}(A)\) is the set of all outputs \(Ax=\sum_j x_j a_j\) as the coefficient vector \(x\) ranges over \(\mathbb R^n\).
That set contains the origin and is closed under addition and scalar multiplication, so it is a subspace of \(\mathbb R^m\). If two columns are independent and there are no others, their combinations form a plane through the origin.
A residual runs from output to target
A dashed segment joins the candidate output to a fixed target.
Move the output and the residual changes, even though the target stays put.
Exact definition
For a candidate \(q=Ax\) and a target \(b\) in the same output space, the residual is \(r=b-q\). The Euclidean squared distance from \(q\) to \(b\) is \(\|r\|^2=r\cdot r\), the sum of the squares of the entries of \(r\).
A least-squares coefficient vector minimizes \(\|b-Ax\|^2\) over all \(x\). Since squaring is increasing on nonnegative numbers, it also minimizes the Euclidean distance.
Refresher on column space, independence and orthogonality
If \(Ax\) and \(Ay\) are outputs, their difference is \(A(x-y)\), also an output. The columns are independent when \(Ax=0\) forces \(x=0\). In that case, two coefficient vectors with the same output must be equal. The column space and null space lesson develops these ideas.
The dot product of real vectors is the sum of their coordinatewise products. Two vectors are orthogonal when this sum is zero. This includes the zero vector, for which no angle is defined. See the dot product and projection onto a line for a geometric example.
A vector is orthogonal to a subspace if its dot product with every vector in the subspace is zero. Dot products distribute over sums and respect scalar multiples. That lets you test orthogonality against a spanning list of vectors.
Place the closest point
- Drag the mark on the plane or use the coefficient controls. On the mark, the arrow keys move along the column directions.
- Press Check my point to see how to improve your placement or to confirm that you found the closest point.
How to read the plane drawing
The grid is a finite patch of a plane in three-dimensional output space. Every intersection represents a combination of the columns. The controls move the mark in smaller steps than the drawn grid spacing.
Screen lengths and angles depend on the viewing direction. Use the displayed dot products to test the residual against the actual columns in space. The squared distance is computed from the three coordinates of the residual.
What changes when the columns change?
Answer the prediction to open the coefficient controls.
Match the fitted output
- Set the coefficients for the current columns. The table shows the point you made and the two residual tests.
- Meet each goal, then advance. A new goal resets your coefficients and may replace the columns.
When the columns are multiples of one another, the output depends on their combined contribution along the line. There can be many ways to deliver that same contribution. Use different coefficient pairs to see this in the last goals.
Why the perpendicular residual gives the closest point
Let \(S\) be a linear subspace of finite-dimensional real Euclidean space, and let \(b\) be any vector in that space. A point \(p\in S\) is the unique closest point to \(b\) in \(S\) exactly when \(r=b-p\) is orthogonal to \(S\). Such a \(p\) always exists and is called the orthogonal projection of \(b\) onto \(S\).
For the proof builder, assume \(p\in S\) and \(r=b-p\) is orthogonal to \(S\). Fix any competing \(q\in S\) with \(q\ne p\), and write \(d=q-p\). The goal is to compare the squared distances. If \(S\) consists only of the origin, there is no competitor and \(p\) is the only choice.
Why the perpendicular residual always exists
Choose an orthonormal basis \(u_1,\ldots,u_k\) of \(S\). Its vectors span \(S\), each has length one, and distinct vectors have zero dot product. Such a basis exists in finite dimensions. Starting with any basis, subtract the next vector’s components along the unit vectors already constructed, then divide the remainder by its length. Independence keeps each remainder nonzero, and the subtraction preserves the span after each step.
Take \(p=\sum_j (u_j\cdot b)u_j\). It belongs to \(S\). Taking the dot product with one \(u_i\) leaves only its own term, \(u_i\cdot p=u_i\cdot b\). Thus \(b-p\) is orthogonal to every \(u_i\) and to their entire span. For \(S=\{0\}\), the sum is empty and \(p=0\).
The proof builder shows that this constructed point beats every distinct competitor. That also proves the converse: any closest point must equal the constructed point, so its residual is orthogonal to \(S\).
For \(S=\operatorname{Col}(A)\), write \(A^\mathsf T\) for the transpose of \(A\), with each column written as a row. Then \(A^\mathsf T(b-Ax)\) lists the dot products of the residual with the columns. Setting those dots to zero gives the normal equations.
\[A^\mathsf T A x=A^\mathsf T b.\]So \(x\) minimizes \(\|b-Ax\|^2\) exactly when \(Ax\) is the orthogonal projection of \(b\) onto \(\operatorname{Col}(A)\), and that happens exactly when \(x\) solves the normal equations. Every solution gives the same fitted point because the closest point is unique. If the columns are independent, the coefficient vector is unique too. Indeed, \(v^\mathsf T A^\mathsf T A v=\|Av\|^2\), which is positive for every nonzero \(v\) in that case, so the square matrix \(A^\mathsf T A\) is invertible. If the columns are dependent, a nonzero vector with \(Av=0\) can be added to any solution without changing the fit.
With independent columns the solution is \(\hat x=(A^\mathsf T A)^{-1}A^\mathsf T b\), so the projection is \(p=A\hat x=Pb\) with
\[P=A(A^\mathsf T A)^{-1}A^\mathsf T.\]This matrix satisfies \(P^\mathsf T=P\) and \(P^2=P\), and \(b-Pb\) is the residual. The formula needs independent columns. With dependent columns \(A^\mathsf T A\) has no inverse and the formula is undefined, although the projection onto \(\operatorname{Col}(A)\) still exists. Keeping only an independent set of columns with the same span gives a matrix for which the formula works.
Each entry of \(A^\mathsf T A\) is a dot product of columns. The off-diagonal entries couple the equations when the columns are nonorthogonal. If all columns are nonzero and mutually orthogonal, each coefficient can instead be computed separately as \((a_j\cdot b)/(a_j\cdot a_j)\).
Find and check fitted outputs
Enter a whole number, a decimal, or a fraction for an exact answer. Enter a decimal within the stated tolerance for the distance question. Hints are available before you open each worked solution.
Further reading
- G. Strang, Introduction to Linear Algebra, 5th ed., chapter 4, “Orthogonality.”
- S. Axler, Linear Algebra Done Right, 3rd ed., chapter 6, “Inner Product Spaces.”