Unit 6, lesson 31

Projection onto a Subspace

How to project a vector onto a subspace with the normal equations, why the projection matrix is idempotent and symmetric, why the projection is the closest vector in the subspace, and why it does not depend on the basis.

  • Video 31
  • Projections and Orthogonal Complements

Two vectors span a plane in R3\mathbb{R}^3, and a target vector b\mathbf{b} lies off the plane. Which point of the plane is closest to b\mathbf{b}? Projecting b\mathbf{b} onto each spanning vector separately and adding the results looks natural, but when the spanning vectors are not perpendicular it generally gives the wrong point. This lesson finds the closest point by making the residual perpendicular to the whole plane, packages the answer as a projection matrix, and shows that the answer depends only on the subspace, not on the basis used to describe it. Throughout, a tuple such as (1,0,1)(1, 0, 1) stands for a column vector, and all vectors are real with the ordinary dot product.

Key terms

Orthogonal projection onto a subspace

Definition Let WW be a subspace of Rm\mathbb{R}^m and let b\mathbf{b} be a vector in Rm\mathbb{R}^m. The orthogonal projection of b\mathbf{b} onto WW is the unique vector p=proj⁡Wb\mathbf{p} = \operatorname{proj}_W \mathbf{b} with

p∈Wandb−p∈W⊥.\mathbf{p} \in W \quad \text{and} \quad \mathbf{b} - \mathbf{p} \in W^{\perp}.

It exists and is unique because Rm=W⊕W⊥\mathbb{R}^m = W \oplus W^{\perp}, so p\mathbf{p} is the WW part of the orthogonal decomposition b=p+r\mathbf{b} = \mathbf{p} + \mathbf{r}. The vector r=b−p\mathbf{r} = \mathbf{b} - \mathbf{p} is the residual. If w1,…,wk\mathbf{w}_1, \dots, \mathbf{w}_k span WW, the residual condition is r⋅wi=0\mathbf{r} \cdot \mathbf{w}_i = 0 for every ii. The projection onto the zero subspace is 0\mathbf{0}. When w1,…,wk\mathbf{w}_1, \dots, \mathbf{w}_k is an orthogonal basis of WW, made of nonzero and mutually perpendicular vectors, the separate line projections add,

proj⁡Wb=∑i=1kwi⋅bwi⋅wi wi,\operatorname{proj}_W \mathbf{b} = \sum_{i=1}^{k} \frac{\mathbf{w}_i \cdot \mathbf{b}}{\mathbf{w}_i \cdot \mathbf{w}_i}\,\mathbf{w}_i,

but for a basis that is not orthogonal this sum is in general not the projection.

In plain words Split b\mathbf{b} into a piece inside the subspace and a piece perpendicular to all of it. The inside piece is the projection.

Example Let WW be the plane spanned by c1=(1,0,1)\mathbf{c}_1 = (1, 0, 1) and c2=(0,1,1)\mathbf{c}_2 = (0, 1, 1), which is the plane z=x+yz = x + y, and let b=(1,1,0)\mathbf{b} = (1, 1, 0). Adding the two line projections gives 12c1+12c2=(12,12,1)\tfrac12\mathbf{c}_1 + \tfrac12\mathbf{c}_2 = (\tfrac12, \tfrac12, 1). This point lies in WW, but its residual (12,12,−1)(\tfrac12, \tfrac12, -1) has dot product −12-\tfrac12 with c1\mathbf{c}_1, so it is not perpendicular to the plane. The shortcut fails because c1⋅c2=1≠0\mathbf{c}_1 \cdot \mathbf{c}_2 = 1 \ne 0. The true projection, computed in the next card, is p=(13,13,23)\mathbf{p} = (\tfrac13, \tfrac13, \tfrac23), whose residual (23,23,−23)(\tfrac23, \tfrac23, -\tfrac23) is perpendicular to both c1\mathbf{c}_1 and c2\mathbf{c}_2.

Normal equations

Definition Let CC be a real m×km \times k matrix with linearly independent columns c1,…,ck\mathbf{c}_1, \dots, \mathbf{c}_k, so that W=Col⁡CW = \operatorname{Col} C has dimension kk, and let b\mathbf{b} be in Rm\mathbb{R}^m. Write the projection as p=Cx^\mathbf{p} = C\hat{\mathbf{x}} with x^\hat{\mathbf{x}} in Rk\mathbb{R}^k. The residual conditions ci⊤(b−Cx^)=0\mathbf{c}_i^{\top}(\mathbf{b} - C\hat{\mathbf{x}}) = 0 for i=1,…,ki = 1, \dots, k stack into C⊤(b−Cx^)=0C^{\top}(\mathbf{b} - C\hat{\mathbf{x}}) = \mathbf{0}, that is

C⊤C x^=C⊤b.C^{\top}C\,\hat{\mathbf{x}} = C^{\top}\mathbf{b}.

These are the normal equations, where normal means perpendicular and refers to the residual. The k×kk \times k matrix C⊤CC^{\top}C is invertible. Indeed, if C⊤Cv=0C^{\top}C\mathbf{v} = \mathbf{0}, then 0=v⊤C⊤Cv=∥Cv∥20 = \mathbf{v}^{\top}C^{\top}C\mathbf{v} = \lVert C\mathbf{v} \rVert^2, so Cv=0C\mathbf{v} = \mathbf{0}, and independence of the columns forces v=0\mathbf{v} = \mathbf{0}. So the coordinate vector x^\hat{\mathbf{x}} is unique, and the projection is the vector p=Cx^\mathbf{p} = C\hat{\mathbf{x}} of Rm\mathbb{R}^m.

In plain words There is one equation per column, and each says that the residual is perpendicular to that column. Solving them gives the coordinates of the projection, and multiplying by CC gives the projection itself.

Example Let CC have columns c1=(1,0,1)\mathbf{c}_1 = (1, 0, 1) and c2=(0,1,1)\mathbf{c}_2 = (0, 1, 1), and let b=(1,1,0)\mathbf{b} = (1, 1, 0). Then

C⊤C=[2112],C⊤b=[11].C^{\top}C = \begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix}, \qquad C^{\top}\mathbf{b} = \begin{bmatrix} 1 \\ 1 \end{bmatrix}.

The equations 2x^1+x^2=12\hat{x}_1 + \hat{x}_2 = 1 and x^1+2x^2=1\hat{x}_1 + 2\hat{x}_2 = 1 give x^1=x^2=13\hat{x}_1 = \hat{x}_2 = \tfrac13, so

p=13c1+13c2=[1/31/32/3],r=b−p=[2/32/3−2/3].\begin{gathered} \mathbf{p} = \tfrac13\mathbf{c}_1 + \tfrac13\mathbf{c}_2 = \begin{bmatrix} 1/3 \\ 1/3 \\ 2/3 \end{bmatrix}, \\[0.4em] \mathbf{r} = \mathbf{b} - \mathbf{p} = \begin{bmatrix} 2/3 \\ 2/3 \\ -2/3 \end{bmatrix}. \end{gathered}

The residual passes both tests, since r⋅c1=23−23=0\mathbf{r} \cdot \mathbf{c}_1 = \tfrac23 - \tfrac23 = 0 and r⋅c2=23−23=0\mathbf{r} \cdot \mathbf{c}_2 = \tfrac23 - \tfrac23 = 0.

Projection matrix

Definition For a real m×km \times k matrix CC with linearly independent columns, the projection matrix onto Col⁡C\operatorname{Col} C is the m×mm \times m matrix

P=C(C⊤C)−1C⊤,P = C(C^{\top}C)^{-1}C^{\top},

and proj⁡Col⁡Cb=Pb\operatorname{proj}_{\operatorname{Col} C} \mathbf{b} = P\mathbf{b} for every b\mathbf{b} in Rm\mathbb{R}^m. The inverse is taken of the square k×kk \times k matrix C⊤CC^{\top}C, never of CC itself, which is not even square when k<mk < m. If the columns are orthonormal, then C⊤C=IC^{\top}C = I and the formula becomes P=CC⊤P = CC^{\top}. To project a single vector it is usually simpler to solve the normal equations than to form PP.

In plain words The matrix PP does the whole job at once. It takes any target in Rm\mathbb{R}^m and returns its projection onto the subspace.

Example For the same CC,

(C⊤C)−1=13[2−1−12],(C^{\top}C)^{-1} = \frac{1}{3}\begin{bmatrix} 2 & -1 \\ -1 & 2 \end{bmatrix},

and multiplying out gives

P=13[2−11−121112].P = \frac{1}{3}\begin{bmatrix} 2 & -1 & 1 \\ -1 & 2 & 1 \\ 1 & 1 & 2 \end{bmatrix}.

Then P(1,1,0)=(13,13,23)P(1, 1, 0) = (\tfrac13, \tfrac13, \tfrac23), as before. For the new target (0,0,3)(0, 0, 3) the same matrix gives (1,1,2)(1, 1, 2), with residual (−1,−1,1)(-1, -1, 1), which is the normal direction of the plane.

Idempotence

Definition A square matrix PP is idempotent if P2=PP^2 = P. Every projection matrix P=C(C⊤C)−1C⊤P = C(C^{\top}C)^{-1}C^{\top} is idempotent and symmetric. With G=C⊤CG = C^{\top}C,

P2=CG−1(C⊤C)G−1C⊤=CG−1C⊤=P,\begin{aligned} P^2 &= CG^{-1}(C^{\top}C)G^{-1}C^{\top} \\[0.4em] &= CG^{-1}C^{\top} = P, \end{aligned}

and P⊤=C(G−1)⊤C⊤=PP^{\top} = C(G^{-1})^{\top}C^{\top} = P, because GG is symmetric and so is its inverse. Conversely, every real square matrix with P2=PP^2 = P and P⊤=PP^{\top} = P is the orthogonal projection onto its own column space. Idempotence alone is not enough. An idempotent matrix that is not symmetric projects along a slanted direction, and some of its residuals b−Pb\mathbf{b} - P\mathbf{b} are not perpendicular to Col⁡P\operatorname{Col} P.

In plain words Projecting a second time changes nothing, because a vector already in the subspace is its own projection. Symmetry is what makes the projection perpendicular rather than slanted.

Example With PP from the previous card and b=(1,1,0)\mathbf{b} = (1, 1, 0), the projection p=(13,13,23)\mathbf{p} = (\tfrac13, \tfrac13, \tfrac23) satisfies Pp=pP\mathbf{p} = \mathbf{p}, and the residual satisfies Pr=0P\mathbf{r} = \mathbf{0}. By contrast, the matrix

K=[1100]K = \begin{bmatrix} 1 & 1 \\ 0 & 0 \end{bmatrix}

has K2=KK^2 = K and column space the xx-axis, but it sends (0,1)(0, 1) to (1,0)(1, 0) and leaves the residual (−1,1)(-1, 1), which is not perpendicular to the xx-axis. The closest point of the xx-axis to (0,1)(0, 1) is (0,0)(0, 0), not (1,0)(1, 0).

Closest vector in a subspace

Definition Let WW be a subspace of Rm\mathbb{R}^m, let b\mathbf{b} be in Rm\mathbb{R}^m, and let p=proj⁡Wb\mathbf{p} = \operatorname{proj}_W \mathbf{b} with residual r=b−p\mathbf{r} = \mathbf{b} - \mathbf{p}. For every y\mathbf{y} in WW,

∥b−y∥2=∥r∥2+∥p−y∥2.\lVert \mathbf{b} - \mathbf{y} \rVert^2 = \lVert \mathbf{r} \rVert^2 + \lVert \mathbf{p} - \mathbf{y} \rVert^2.

Hence ∥b−y∥≥∥b−p∥\lVert \mathbf{b} - \mathbf{y} \rVert \ge \lVert \mathbf{b} - \mathbf{p} \rVert, with equality only for y=p\mathbf{y} = \mathbf{p}. So p\mathbf{p} is the unique vector of WW closest to b\mathbf{b}, and the distance from b\mathbf{b} to WW is ∥r∥\lVert \mathbf{r} \rVert. The identity is the Pythagorean theorem. Write b−y=r+(p−y)\mathbf{b} - \mathbf{y} = \mathbf{r} + (\mathbf{p} - \mathbf{y}). Since p−y\mathbf{p} - \mathbf{y} lies in WW and r\mathbf{r} lies in W⊥W^{\perp}, the cross term 2 r⋅(p−y)2\,\mathbf{r} \cdot (\mathbf{p} - \mathbf{y}) in the expansion is zero.

In plain words Moving away from p\mathbf{p} inside the subspace adds an error perpendicular to the residual, so every other candidate is farther from b\mathbf{b}.

Example For b=(1,1,0)\mathbf{b} = (1, 1, 0) and the plane z=x+yz = x + y, the squared distance is ∥r∥2=49+49+49=43\lVert \mathbf{r} \rVert^2 = \tfrac49 + \tfrac49 + \tfrac49 = \tfrac43, so the distance is 2/32/\sqrt{3}. The shortcut point y=(12,12,1)\mathbf{y} = (\tfrac12, \tfrac12, 1) has p−y=(−16,−16,−13)\mathbf{p} - \mathbf{y} = (-\tfrac16, -\tfrac16, -\tfrac13) and ∥p−y∥2=16\lVert \mathbf{p} - \mathbf{y} \rVert^2 = \tfrac16, so its squared distance to b\mathbf{b} is 43+16=32\tfrac43 + \tfrac16 = \tfrac32. A direct computation agrees, since b−y=(12,12,−1)\mathbf{b} - \mathbf{y} = (\tfrac12, \tfrac12, -1) has squared length 14+14+1=32\tfrac14 + \tfrac14 + 1 = \tfrac32.

Basis independence of a projection

Definition The projection proj⁡Wb\operatorname{proj}_W \mathbf{b} depends only on the subspace WW and the dot product, not on the basis chosen for WW. If CC and DD are real m×km \times k matrices with linearly independent columns and Col⁡C=Col⁡D\operatorname{Col} C = \operatorname{Col} D, then

C(C⊤C)−1C⊤=D(D⊤D)−1D⊤.C(C^{\top}C)^{-1}C^{\top} = D(D^{\top}D)^{-1}D^{\top}.

The reason is uniqueness. Both formulas return a vector of WW whose residual lies in W⊥W^{\perp}, and there is only one such vector. The coordinates x^\hat{\mathbf{x}}, by contrast, generally change with the basis. If the spanning list is dependent, then C⊤CC^{\top}C is singular and the formula cannot be used, yet the projection still exists and is unique, while its coordinates in that list are not. One remedy is to remove redundant columns first.

In plain words Different bases describe the same plane with different coordinates, but the closest point of the plane does not depend on how the plane is described.

Example Replace c2\mathbf{c}_2 by c1+c2=(1,1,2)\mathbf{c}_1 + \mathbf{c}_2 = (1, 1, 2), so that DD has columns (1,0,1)(1, 0, 1) and (1,1,2)(1, 1, 2) and the same column space as CC. For b=(1,1,0)\mathbf{b} = (1, 1, 0),

D⊤D=[2336],D⊤b=[12],D^{\top}D = \begin{bmatrix} 2 & 3 \\ 3 & 6 \end{bmatrix}, \qquad D^{\top}\mathbf{b} = \begin{bmatrix} 1 \\ 2 \end{bmatrix},

with solution x^=(0,13)\hat{\mathbf{x}} = (0, \tfrac13). The coordinates changed from (13,13)(\tfrac13, \tfrac13), but 0⋅(1,0,1)+13(1,1,2)=(13,13,23)0 \cdot (1, 0, 1) + \tfrac13(1, 1, 2) = (\tfrac13, \tfrac13, \tfrac23) is the same p\mathbf{p}. With the dependent list (1,0,1)(1, 0, 1), (0,1,1)(0, 1, 1), (1,1,2)(1, 1, 2), the coefficient vectors (13,13,0)(\tfrac13, \tfrac13, 0) and (0,0,13)(0, 0, \tfrac13) both produce this same p\mathbf{p}.

Check yourself

  1. Project b=(2,3,1)\mathbf{b} = (2, 3, 1) onto the plane spanned by w1=(1,0,0)\mathbf{w}_1 = (1, 0, 0) and w2=(0,1,1)\mathbf{w}_2 = (0, 1, 1), and find the distance from b\mathbf{b} to the plane.
Show the answer

The spanning vectors are perpendicular, since w1⋅w2=0\mathbf{w}_1 \cdot \mathbf{w}_2 = 0, so the line projections add. The coefficients are 21=2\tfrac21 = 2 and 3+12=2\tfrac{3 + 1}{2} = 2, which gives p=2w1+2w2=(2,2,2)\mathbf{p} = 2\mathbf{w}_1 + 2\mathbf{w}_2 = (2, 2, 2). The residual (0,1,−1)(0, 1, -1) has zero dot product with both w1\mathbf{w}_1 and w2\mathbf{w}_2, and the distance is its length 2\sqrt{2}.

  1. Let CC have columns (1,1,0)(1, 1, 0) and (0,1,1)(0, 1, 1), and let b=(3,0,0)\mathbf{b} = (3, 0, 0). Solve the normal equations, then find the projection of b\mathbf{b} onto Col⁡C\operatorname{Col} C, the residual and the distance.
Show the answer

Here

C⊤C=[2112],C⊤b=[30].C^{\top}C = \begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix}, \qquad C^{\top}\mathbf{b} = \begin{bmatrix} 3 \\ 0 \end{bmatrix}.

The equations 2x^1+x^2=32\hat{x}_1 + \hat{x}_2 = 3 and x^1+2x^2=0\hat{x}_1 + 2\hat{x}_2 = 0 give x^1=2\hat{x}_1 = 2 and x^2=−1\hat{x}_2 = -1. So p=2(1,1,0)−(0,1,1)=(2,1,−1)\mathbf{p} = 2(1, 1, 0) - (0, 1, 1) = (2, 1, -1) and r=(1,−1,1)\mathbf{r} = (1, -1, 1). The residual is perpendicular to both columns, since 1−1+0=01 - 1 + 0 = 0 and 0−1+1=00 - 1 + 1 = 0, and the distance is 3\sqrt{3}. Adding the separate line projections would give (32,32,0)(\tfrac32, \tfrac32, 0), at squared distance 92\tfrac92 from b\mathbf{b} instead of 33.

  1. Both matrices below satisfy M2=MM^2 = M, and both have the line spanned by (1,1)(1, 1) as column space. Which one is the orthogonal projection onto that line?
P=12[1111],E=[1010]P = \frac{1}{2}\begin{bmatrix} 1 & 1 \\ 1 & 1 \end{bmatrix}, \qquad E = \begin{bmatrix} 1 & 0 \\ 1 & 0 \end{bmatrix}
Show the answer

Idempotence cannot decide, since both matrices have it. The matrix PP is symmetric and equals aa⊤/(a⊤a)\mathbf{a}\mathbf{a}^{\top}/(\mathbf{a}^{\top}\mathbf{a}) for a=(1,1)\mathbf{a} = (1, 1), so it is the orthogonal projection. The matrix EE is not symmetric, and it fails the residual test. It sends (1,0)(1, 0) to (1,1)(1, 1), leaving the residual (0,−1)(0, -1), whose dot product with (1,1)(1, 1) is −1-1. The matrix PP sends (1,0)(1, 0) to (12,12)(\tfrac12, \tfrac12), leaving (12,−12)(\tfrac12, -\tfrac12), which is perpendicular to the line. That point is at distance 1/21/\sqrt{2} from (1,0)(1, 0), closer than the distance 11 to the point (1,1)(1, 1) chosen by EE.

How this lesson connects