Tutorials › Linear Algebra › Residual Geometry And Orthogonality

Least Squares and QR Factorization · Tutorial 682 of 1000

Residual Geometry And Orthogonality

Learn to read fitted vectors and residuals through the geometry of the column space and its orthogonal complement.

Intermediate 9 min read

What You'll Learn

  • Relate a least-squares fit to a vector in the column space of a matrix
  • Explain why a best-fit residual is perpendicular to the column space
  • Use orthogonal decompositions to separate captured and uncaptured components
  • Compute residuals and verify their geometry in exact examples
  • Identify a concrete error about what a residual is perpendicular to

The Fit and the Residual as Vectors

For a system \(\mathbf{A}\mathbf{x}=\mathbf{b}\), an exact solution would make the residual \(\mathbf{b}-\mathbf{A}\mathbf{x}\) equal to zero. When there is no exact solution, least squares chooses a vector \(\mathbf{x}\) that makes the residual as short as possible. The geometry is easiest to read by separating the fitted vector \(\mathbf{A}\mathbf{x}\) from the residual: the fitted vector lies in the column space of \(\mathbf{A}\), while the residual of a best fit points perpendicular to that space.

Let \(\mathbf{A}\in\mathbb{R}^{m\times n}\) and \(\mathbf{b}\in\mathbb{R}^m\). The column space \(\operatorname{Col}\mathbf{A}\) is the set of all linear combinations of the columns of \(\mathbf{A}\). By the definition of matrix multiplication, it is also the set of all vectors \(\mathbf{A}\mathbf{x}\) as \(\mathbf{x}\) ranges over \(\mathbb{R}^n\). A candidate coefficient vector \(\mathbf{x}\) therefore gives a fitted vector \(\mathbf{y}=\mathbf{A}\mathbf{x}\in\operatorname{Col}\mathbf{A}\) and a residual \(\mathbf{r}=\mathbf{b}-\mathbf{y}\). A least-squares solution is a choice of \(\mathbf{x}\) that minimizes \(\|\mathbf{b}-\mathbf{A}\mathbf{x}\|\), or equivalently minimizes its square.

Definition: For a subspace \(S\subseteq\mathbb{R}^m\), its orthogonal complement is \(S^\perp=\{\mathbf{u}\in\mathbb{R}^m:\mathbf{u}\cdot\mathbf{s}=0\text{ for every }\mathbf{s}\in S\}\). A residual is orthogonal to the column space when \(\mathbf{r}\in(\operatorname{Col}\mathbf{A})^\perp\), meaning its dot product with every column of \(\mathbf{A}\), and hence every vector in their span, is zero.

The earlier results on orthogonal projections give an orthogonal decomposition of any \(\mathbf{b}\in\mathbb{R}^m\) relative to a subspace \(S\): \(\mathbf{b}=\mathbf{b}_S+\mathbf{b}_{S^\perp}\), where \(\mathbf{b}_S\in S\) and \(\mathbf{b}_{S^\perp}\in S^\perp\). These two components are unique. Applied to \(S=\operatorname{Col}\mathbf{A}\), the component \(\mathbf{b}_S\) is the part available to be fitted, and \(\mathbf{b}_{S^\perp}\) is the part that lies outside the model space.

Why the Best-Fit Residual Is Perpendicular

The following characterization expresses least squares entirely in terms of vectors in the column space. It applies to any subspace \(S\) of \(\mathbb{R}^m\), so it does not require the columns of \(\mathbf{A}\) to be independent. For a least-squares problem, take \(S=\operatorname{Col}\mathbf{A}\). The statement is conditional: it characterizes a best fit whenever one is under consideration; existence is a separate question.

Theorem: Let \(S\subseteq\mathbb{R}^m\) be a subspace, let \(\mathbf{b}\in\mathbb{R}^m\), and let \(\mathbf{y}_*\in S\). Then \(\mathbf{y}_*\) minimizes \(\|\mathbf{b}-\mathbf{y}\|\) over all \(\mathbf{y}\in S\) if and only if \(\mathbf{b}-\mathbf{y}_*\in S^\perp\).

Proof. Set \(\mathbf{e}=\mathbf{b}-\mathbf{y}_*\). First suppose \(\mathbf{e}\in S^\perp\). For any \(\mathbf{y}\in S\), the difference \(\mathbf{y}_*-\mathbf{y}\) belongs to \(S\), so it is perpendicular to \(\mathbf{e}\). Since \(\mathbf{b}-\mathbf{y}=\mathbf{e}+(\mathbf{y}_*-\mathbf{y})\), expansion of the squared norm gives \(\|\mathbf{b}-\mathbf{y}\|^2=\|\mathbf{e}\|^2+\|\mathbf{y}_*-\mathbf{y}\|^2\geq\|\mathbf{e}\|^2=\|\mathbf{b}-\mathbf{y}_*\|^2\). Thus \(\mathbf{y}_*\) minimizes the distance.

Conversely, suppose \(\mathbf{y}_*\) minimizes the distance. Take any nonzero \(\mathbf{u}\in S\). For every real \(t\), the vector \(\mathbf{y}_*+t\mathbf{u}\) belongs to \(S\). Minimality implies \(\|\mathbf{e}-t\mathbf{u}\|^2\geq\|\mathbf{e}\|^2\) for every \(t\in\mathbb{R}\). Expanding the left side gives \(-2t(\mathbf{e}\cdot\mathbf{u})+t^2\|\mathbf{u}\|^2\geq0\). If \(\mathbf{e}\cdot\mathbf{u}\ne0\), choose \(t=(\mathbf{e}\cdot\mathbf{u})/\|\mathbf{u}\|^2\). The left side then equals \(-(\mathbf{e}\cdot\mathbf{u})^2/\|\mathbf{u}\|^2<0\), a contradiction. Hence \(\mathbf{e}\cdot\mathbf{u}=0\) for every nonzero \(\mathbf{u}\in S\); the same equality holds for \(\mathbf{u}=\mathbf{0}\). Therefore \(\mathbf{e}\in S^\perp\). \(\square\)

With \(S=\operatorname{Col}\mathbf{A}\), this says that any best-fit vector \(\mathbf{y}_*\) has residual \(\mathbf{r}_*=\mathbf{b}-\mathbf{y}_*\) perpendicular to every column combination. The residual is not an extra direction that the model could still use: if it had a nonzero component inside the column space, moving the fit in that direction would shorten the error.

What a Fit Can and Cannot Capture

Write the orthogonal decomposition of \(\mathbf{b}\) relative to \(S=\operatorname{Col}\mathbf{A}\) as \(\mathbf{b}=\mathbf{b}_S+\mathbf{b}_{S^\perp}\). The first component can be represented as \(\mathbf{A}\mathbf{x}\) for at least one coefficient vector, because it belongs to the column space. The second is perpendicular to every possible fitted vector. Thus a best fit captures \(\mathbf{b}_S\), and its residual is \(\mathbf{b}_{S^\perp}\). The decomposition also separates the error for any candidate fit.

Geometric consequence: For any \(\mathbf{y}\in S\), \(\|\mathbf{b}-\mathbf{y}\|^2=\|\mathbf{b}_{S^\perp}\|^2+\|\mathbf{b}_S-\mathbf{y}\|^2\). The first term is independent of the candidate fit. The second measures how far its fitted vector is from the available component \(\mathbf{b}_S\).

This identity follows because \(\mathbf{b}-\mathbf{y}=\mathbf{b}_{S^\perp}+(\mathbf{b}_S-\mathbf{y})\), and the two terms are perpendicular: the first lies in \(S^\perp\), while the second lies in \(S\). It gives a useful interpretation of the smallest possible error. No choice of coefficients can remove \(\mathbf{b}_{S^\perp}\), because every fitted vector remains in \(S\).

Worked Example: A Best Fit in a Plane

Let \(\mathbf{A}=\begin{bmatrix}1&0\\0&1\\1&1\end{bmatrix}\) and \(\mathbf{b}=\begin{bmatrix}1\\2\\4\end{bmatrix}\). Find a fitted vector and residual, and check that the residual is perpendicular to the column space.

The columns of \(\mathbf{A}\) are \(\mathbf{a}_1=(1,0,1)^\top\) and \(\mathbf{a}_2=(0,1,1)^\top\). The vector \(\mathbf{n}=(-1,-1,1)^\top\) is perpendicular to both, since \(\mathbf{n}\cdot\mathbf{a}_1=-1+1=0\) and \(\mathbf{n}\cdot\mathbf{a}_2=-1+1=0\). The component of \(\mathbf{b}\) along \(\mathbf{n}\) is \(\frac{\mathbf{b}\cdot\mathbf{n}}{\mathbf{n}\cdot\mathbf{n}}\mathbf{n} =\frac{-1-2+4}{1+1+1}\mathbf{n} =\frac13\mathbf{n}\). Thus take \(\mathbf{r}=\frac13\mathbf{n}=(-\frac13,-\frac13,\frac13)^\top\) and \(\mathbf{y}=\mathbf{b}-\mathbf{r}=(\frac43,\frac73,\frac{11}3)^\top\).

To verify that this fitted vector is available from the matrix, use \(\mathbf{x}=(\frac43,\frac73)^\top\): \(\mathbf{A}\mathbf{x}=(\frac43,\frac73,\frac43+\frac73)^\top=(\frac43,\frac73,\frac{11}3)^\top=\mathbf{y}\). The residual check is \(\mathbf{b}-\mathbf{A}\mathbf{x}=(1-\frac43,2-\frac73,4-\frac{11}3)^\top=(-\frac13,-\frac13,\frac13)^\top=\mathbf{r}\). Also \(\mathbf{r}\cdot\mathbf{a}_1=-\frac13+\frac13=0\) and \(\mathbf{r}\cdot\mathbf{a}_2=-\frac13+\frac13=0\). By the theorem, this is a best-fit vector. Its squared error is \(\|\mathbf{r}\|^2=\frac19+\frac19+\frac19=\frac13\).

Worked Example: Orthogonal Columns Make the Geometry Visible

Let \(\mathbf{A}=\begin{bmatrix}1&0\\1&0\\0&1\\0&1\end{bmatrix}\) and \(\mathbf{b}=(2,0,3,1)^\top\). The columns \(\mathbf{a}_1=(1,1,0,0)^\top\) and \(\mathbf{a}_2=(0,0,1,1)^\top\) are perpendicular. Their squared norms are both \(2\). The projection coefficients onto these perpendicular directions are \(\frac{\mathbf{b}\cdot\mathbf{a}_1}{\|\mathbf{a}_1\|^2}=\frac{2}{2}=1\) and \(\frac{\mathbf{b}\cdot\mathbf{a}_2}{\|\mathbf{a}_2\|^2}=\frac{4}{2}=2\). So choose \(\mathbf{x}=(1,2)^\top\). Then \(\mathbf{A}\mathbf{x}=(1,1,2,2)^\top=\mathbf{y}\) and \(\mathbf{r}=\mathbf{b}-\mathbf{y}=(1,-1,1,-1)^\top\).

Indeed, \(\mathbf{r}\cdot\mathbf{a}_1=1-1=0\) and \(\mathbf{r}\cdot\mathbf{a}_2=1-1=0\), so the residual is perpendicular to the whole column space. The fitted vector is verified by multiplying the matrix by the stated coefficients, and the residual by subtracting it from \(\mathbf{b}\). Its squared length is \(1+1+1+1=4\); that is the part of the data vector these two column directions cannot capture.

Worked Example: An Uncapturable Component Along a Line

Let \(\mathbf{A}=(1,1,1)^\top\) and \(\mathbf{b}=(1,0,2)^\top\). Every fitted vector has the form \(\mathbf{y}=t(1,1,1)^\top\) for some \(t\in\mathbb{R}\). The projection coefficient is \(t=\frac{\mathbf{b}\cdot(1,1,1)}{(1,1,1)\cdot(1,1,1)}=\frac{1+0+2}{3}=1\). This gives \(\mathbf{y}_*=(1,1,1)^\top=\mathbf{A}(1)\) and \(\mathbf{r}_*=\mathbf{b}-\mathbf{y}_*=(0,-1,1)^\top\). Since \(\mathbf{r}_*\cdot(1,1,1)=0-1+1=0\), the residual is perpendicular to the line and the fit is best.

For any other coefficient \(t\), the residual is \(\mathbf{b}-t(1,1,1)^\top=\mathbf{r}_*+(1-t)(1,1,1)^\top\). The two terms on the right are perpendicular, so \(\|\mathbf{b}-t(1,1,1)^\top\|^2=\|\mathbf{r}_*\|^2+(1-t)^2\|(1,1,1)\|^2=2+3(1-t)^2\geq2\). Equality holds at \(t=1\). This check shows both what the line can capture and why the perpendicular component of squared length \(2\) remains in every error.

A Common Mistake: Perpendicular to the Wrong Vector

A residual of a best fit is perpendicular to the column space, not necessarily to the data vector \(\mathbf{b}\), the coefficient vector \(\mathbf{x}\), or each residual component considered separately. For the plane example, the correct residual is \(\mathbf{r}=(-\frac13,-\frac13,\frac13)^\top\). A tempting but wrong check is to claim that \(\mathbf{r}\cdot\mathbf{b}=0\). In fact, \(\mathbf{r}\cdot\mathbf{b}=-\frac13- \frac23+\frac43=\frac13\ne0\). The fit is still best: the relevant checks are \(\mathbf{r}\cdot\mathbf{a}_1=0\) and \(\mathbf{r}\cdot\mathbf{a}_2=0\), which imply perpendicularity to every linear combination of the columns.

There is also an important distinction between a fitted vector and its coefficients. The geometry takes place in \(\mathbb{R}^m\), where \(\mathbf{b}\), \(\mathbf{A}\mathbf{x}\), and the residual live. The coefficient vector \(\mathbf{x}\) lives in \(\mathbb{R}^n\). It is the image \(\mathbf{A}\mathbf{x}\), not \(\mathbf{x}\) itself, that is compared with \(\mathbf{b}\) and belongs to \(\operatorname{Col}\mathbf{A}\).

VectorWhere it liesGeometric role
\(\mathbf{b}\)\(\mathbb{R}^m\)Data vector being approximated
\(\mathbf{A}\mathbf{x}\)\(\operatorname{Col}\mathbf{A}\)Candidate fitted vector
\(\mathbf{b}-\mathbf{A}\mathbf{x}\)\(\mathbb{R}^m\)Residual; perpendicular to the column space at a best fit
\(\mathbf{x}\)\(\mathbb{R}^n\)Coefficients used to produce a fitted vector

Check Your Understanding

Use the column-space geometry in each question, and check computations by substitution or dot products.

  1. For the plane example, verify directly that \(\mathbf{b}=\mathbf{y}+\mathbf{r}\) and that \(\mathbf{y}\cdot\mathbf{r}=0\).
  2. In the orthogonal-columns example, compute the dot product of the residual with each column and state why those two checks suffice.
  3. For a subspace \(S\), explain why \(\mathbf{b}_{S^\perp}\) cannot be changed by choosing a different fitted vector in \(S\).
  4. In the line example, evaluate \(2+3(1-t)^2\) at \(t=0\) and compare it with the squared error at \(t=1\).
  5. Give the correct geometric condition on the residual of a best fit, and explain why perpendicularity to \(\mathbf{b}\) is not required.

Test yourself: Linear Algebra final exam practice

Ready to check what you know? Work a full-length practice final, then open the complete worked solutions.

Want all 1,114 practice problems?

This tutorial covers one topic. Calc Pass covers the full Calculus AB & BC curriculum (the core of Calculus I and II): full review, every practice question, and step-by-step solutions to each one.

QR code to download the Calc Pass app

Get the Calc Pass App

Scan to download and practice on the go, with instant scoring.

Calc Pass Volume 1: 1,114 Practice Questions book cover

Vol. 1: 1,114 Questions

The complete practice question bank, organized by AB & BC topic.

View on Amazon
Calc Pass Volume 2: Solutions Manual book cover

Vol. 2: Solutions Manual

Fully worked solutions to every question in Volume 1.

View on Amazon