Session 15 · Unit 2

Projections onto Subspaces


How to project a vector b onto a line, and then onto any subspace. One perpendicularity fact gives three formulas: the coefficient x-hat, the projection p = Ax-hat, and the projection matrix P = A(A-transpose-A)-inverse A-transpose. The payoff is least squares: when Ax = b has no solution, project b onto the column space and solve the closest problem you can.

Projecting onto a line

“We’re almost ready to make this lecture immortal,” Strang says — and it is an important one. It’s about projections. Start in two dimensions with the simplest case: a vector b\mathbf{b}, and a line through a vector a\mathbf{a} — a one-dimensional subspace. Which point on that line is closest to b\mathbf{b}?

The whole point of the picture is where orthogonality comes in. The closest point — the projection p\mathbf{p} of b\mathbf{b} onto the line — is the point where the connecting segment meets the line at a right angle. The error e=bp\mathbf{e} = \mathbf{b} - \mathbf{p}, “how much I’m wrong by,” is perpendicular to a\mathbf{a}. That’s the one fact we know, and it’s got to tell us everything. (You could do this triangle with trigonometry, angles θ\theta, sines and cosines — that leads to “lousy formulas compared to linear algebra.”)

Since p\mathbf{p} lies on the line, it’s some multiple of a\mathbf{a}: write p=xa\mathbf{p} = x\mathbf{a}, and the problem is to find the number xx. The perpendicularity condition is

aT(bxa)=0\mathbf{a}^T(\mathbf{b} - x\mathbf{a}) = 0

That’s the central equation. Simplify: xaTa=aTbx\,\mathbf{a}^T\mathbf{a} = \mathbf{a}^T\mathbf{b}, and since aTa\mathbf{a}^T\mathbf{a} is just a number — the length of a\mathbf{a} squared — divide by it:

x=aTbaTa,p=aaTbaTax = \frac{\mathbf{a}^T\mathbf{b}}{\mathbf{a}^T\mathbf{a}}, \qquad \mathbf{p} = \mathbf{a}\,\frac{\mathbf{a}^T\mathbf{b}}{\mathbf{a}^T\mathbf{a}}

Two sanity checks before moving on. Double b\mathbf{b}, and the projection doubles too — an extra factor 22 rides straight through the formula. But double a\mathbf{a} and nothing changes: the line through 2a2\mathbf{a} (or a-\mathbf{a}) is the same line, and indeed the formula picks up a factor 44 above and a factor 44 below, which cancel. The projection depends on the subspace, not on which vector you used to describe it.

Feel the formula before moving on — drag b\mathbf{b} anywhere and watch p\mathbf{p} slide along the line while e\mathbf{e} stays stubbornly perpendicular:

Try it — drag b, the projection follows

The line through a is fixed. p is the point on it closest to b; the error e = b − p is perpendicular to a — always. Drag b (mouse or touch) and try to break it.

The projection matrix

Now lift this into matrices. The projection is some matrix acting on b\mathbf{b}: p=Pb\mathbf{p} = P\mathbf{b}. Reading the formula with the parentheses moved,

P=aaTaTaP = \frac{\mathbf{a}\,\mathbf{a}^T}{\mathbf{a}^T\mathbf{a}}

Don’t cancel! Below is aTa\mathbf{a}^T\mathbf{a}, a number. Above is aaT\mathbf{a}\,\mathbf{a}^T, a column times a row — a full-scale n×nn \times n matrix. And it’s an interesting one.

What’s its column space? Multiply PP by any vector b\mathbf{b} and you land on the line through a\mathbf{a} — so the column space of PP is exactly that line, and the rank is one. It’s exactly the form we know for a rank-one matrix: column times row, with that column as the basis for the column space.

So there are three formulas to remember from the one-dimensional case: the number x=aTb/aTax = \mathbf{a}^T\mathbf{b}/\mathbf{a}^T\mathbf{a}, the projection p=xa\mathbf{p} = x\mathbf{a}, and the matrix P=aaT/aTaP = \mathbf{a}\mathbf{a}^T/\mathbf{a}^T\mathbf{a}. The same three will reappear, slightly changed, in nn dimensions.

Why project? Because Ax = b may have no solution

Why do we want projections at all? Because this chapter deals with systems Ax=bA\mathbf{x} = \mathbf{b} that have no solution — typically too many equations, more equations than unknowns. That’s the real problem.

So what do you do? “I solve the closest problem that I can solve.” The vector AxA\mathbf{x} is always in the column space of AA — that’s the constraint — but b\mathbf{b} probably is not. So replace b\mathbf{b} by the closest vector that is in the column space:

Ax^=p,p=projection of b onto the column space of AA\hat{\mathbf{x}} = \mathbf{p}, \qquad \mathbf{p} = \text{projection of } \mathbf{b} \text{ onto the column space of } A

The hat on x^\hat{\mathbf{x}} is a reminder: this is not the x\mathbf{x} that doesn’t exist — it’s the best possible x\mathbf{x}.

Projecting onto a subspace

Now the real problem, in three dimensions: a plane, and a vector b\mathbf{b} not in the plane. Project b\mathbf{b} down into the plane. To get a formula we need to describe the plane: give a basis, two vectors a1\mathbf{a}_1 and a2\mathbf{a}_2. They don’t have to be perpendicular, but they’d better be independent. And here’s the clean move — that plane is a column space: it’s the column space of the matrix AA whose columns are a1\mathbf{a}_1 and a2\mathbf{a}_2. Everything is cleaner in terms of a matrix. (And once we can do two columns, we can do nn.)

The projection p\mathbf{p} is some combination of the basis vectors — rather than writing x^1a1+x^2a2\hat{x}_1\mathbf{a}_1 + \hat{x}_2\mathbf{a}_2, write it as p=Ax^\mathbf{p} = A\hat{\mathbf{x}}. The problem: find the right combination of columns so that the error is perpendicular to the plane. The error e=bAx^\mathbf{e} = \mathbf{b} - A\hat{\mathbf{x}} must be perpendicular to everything in the plane, and the plane is told to us by a1\mathbf{a}_1 and a2\mathbf{a}_2, so two equations:

a1T(bAx^)=0,a2T(bAx^)=0\mathbf{a}_1^T(\mathbf{b} - A\hat{\mathbf{x}}) = 0, \qquad \mathbf{a}_2^T(\mathbf{b} - A\hat{\mathbf{x}}) = 0

Stack them into a matrix equation — a1T\mathbf{a}_1^T and a2T\mathbf{a}_2^T are exactly the rows of ATA^T — and it comes out just right:

AT(bAx^)=0A^T(\mathbf{b} - A\hat{\mathbf{x}}) = \mathbf{0}

Two remarks before solving. First: in the very first problem, AA had one column, little a\mathbf{a} — change capital AA to little a\mathbf{a} and this is the equation we solved on the line. Second: which of the four subspaces is e\mathbf{e} in? The equation says ATe=0A^T\mathbf{e} = \mathbf{0}, so e\mathbf{e} is in the null space of ATA^T.

The three formulas in n dimensions

Rewrite the equation in its famous form:

The three answers, matching the three from the line:

x^=(ATA)1ATb,p=Ax^=A(ATA)1ATb,P=A(ATA)1AT\hat{\mathbf{x}} = (A^TA)^{-1}A^T\mathbf{b}, \qquad \mathbf{p} = A\hat{\mathbf{x}} = A(A^TA)^{-1}A^T\mathbf{b}, \qquad P = A(A^TA)^{-1}A^T

The projection matrix looks a little messy but it’s not bad — it’s the “magic combination,” the nn-dimensional version of aaT/aTa\mathbf{a}\mathbf{a}^T/\mathbf{a}^T\mathbf{a}. In more dimensions we’re not allowed to just divide, so inverse replaces the division.

Now the first thing that occurs to you is something bad. Why not split the inverse of the product, (ATA)1=A1(AT)1(A^TA)^{-1} = A^{-1}(A^T)^{-1}? Then P=AA1(AT)1AT=IP = A\,A^{-1}(A^T)^{-1}A^T = I — the identity! Strang writes it on the board but warns: “don’t take the videotape quite so carefully.” The step is not allowed: AA is not square — it has too many rows, just a couple of columns — so A1A^{-1} doesn’t exist. ATAA^TA is square and invertible, but it can’t be pulled apart.

Except… suppose AA were square and invertible. Then its column space is all of Rn\mathbb{R}^n, and projecting b\mathbf{b} onto the whole space should give back b\mathbf{b} itself — the projection matrix should be the identity. And that’s exactly what the split-apart computation gives: P=IP = I is the correct answer in that case. For a genuine subspace, stay with the full formula.

Do the two projection properties still hold? PT=PP^T = P: transpose the product — the symmetric middle factor (ATA)1(A^TA)^{-1} stays symmetric, the outer AA and ATA^T swap ends, and you’re back to PP. And P2=PP^2 = P — writing it out gives “eight AA‘s in a row, which is quite obscene,” but put the parentheses in good places: an ATAA^TA sits next to its inverse in the middle, all that stuff goes, and what’s left is PP again. Geometrically it had to work: the first projection pops you onto the column space, the second leaves you where you are.

Least squares: fitting a line to three points

Now to use the formulas. When do you have too many equations and still want a best answer? The most important, most common example: fitting points by a straight line.

Take three data points: at t=1,2,3t = 1, 2, 3 the measurements are b=1,2,2b = 1, 2, 2. They lie close to a line but not on one. We want the best line b=C+Dtb = C + Dt — the one whose errors at the three points are as small as we can get them.

Today was all letters; numbers next time — the next lecture carries this example through to the actual best line.

Problems

Work these before revealing the solutions — the lecture’s three formulas are all you need.

Problem 15.1 Projection onto a line

Project b=[126]\mathbf{b} = \begin{bmatrix} 1 \\ 2 \\ 6 \end{bmatrix} onto the line through a=[111]\mathbf{a} = \begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix}. Find xx, the projection p\mathbf{p}, and the error e\mathbf{e}, and verify that e\mathbf{e} is perpendicular to a\mathbf{a}.

Show solution
x=aTbaTa=1+2+61+1+1=93=3,p=3a=[333]x = \frac{\mathbf{a}^T\mathbf{b}}{\mathbf{a}^T\mathbf{a}} = \frac{1 + 2 + 6}{1 + 1 + 1} = \frac{9}{3} = 3, \qquad \mathbf{p} = 3\mathbf{a} = \begin{bmatrix} 3 \\ 3 \\ 3 \end{bmatrix}

The error is

e=bp=[213]\mathbf{e} = \mathbf{b} - \mathbf{p} = \begin{bmatrix} -2 \\ -1 \\ 3 \end{bmatrix}

and aTe=21+3=0\mathbf{a}^T\mathbf{e} = -2 - 1 + 3 = 0: perpendicular, as the geometry demands.

Problem 15.2 Rank-one projection matrix

Compute the projection matrix P=aaTaTaP = \dfrac{\mathbf{a}\mathbf{a}^T}{\mathbf{a}^T\mathbf{a}} for a=[122]\mathbf{a} = \begin{bmatrix} 1 \\ 2 \\ 2 \end{bmatrix}. Check that PP is symmetric, that P2=PP^2 = P, and give its rank and column space.

Show solution

aTa=1+4+4=9\mathbf{a}^T\mathbf{a} = 1 + 4 + 4 = 9, and the column-times-row product gives

P=19[122244244]P = \frac{1}{9}\begin{bmatrix} 1 & 2 & 2 \\ 2 & 4 & 4 \\ 2 & 4 & 4 \end{bmatrix}

Symmetric: yes — the (i,j)(i,j) entry aiaj/9a_i a_j / 9 equals the (j,i)(j,i) entry.

P2=PP^2 = P: compute (aaT)(aaT)=a(aTa)aT=9aaT(\mathbf{a}\mathbf{a}^T)(\mathbf{a}\mathbf{a}^T) = \mathbf{a}(\mathbf{a}^T\mathbf{a})\mathbf{a}^T = 9\,\mathbf{a}\mathbf{a}^T, so P2=9aaT81=aaT9=PP^2 = \dfrac{9\,\mathbf{a}\mathbf{a}^T}{81} = \dfrac{\mathbf{a}\mathbf{a}^T}{9} = P.

Rank and column space: every column is a multiple of a\mathbf{a}, so the rank is 11 and the column space is the line through a\mathbf{a} — exactly the line we project onto.

Problem 15.3 Projection onto a plane

Let

A=[101112],b=[600]A = \begin{bmatrix} 1 & 0 \\ 1 & 1 \\ 1 & 2 \end{bmatrix}, \qquad \mathbf{b} = \begin{bmatrix} 6 \\ 0 \\ 0 \end{bmatrix}

Project b\mathbf{b} onto the column space of AA: find x^\hat{\mathbf{x}}, the projection p\mathbf{p}, and the error e\mathbf{e}, and check that e\mathbf{e} is perpendicular to both columns of AA.

Show solution

Form the normal equation ATAx^=ATbA^TA\hat{\mathbf{x}} = A^T\mathbf{b}:

ATA=[3335],ATb=[60]A^TA = \begin{bmatrix} 3 & 3 \\ 3 & 5 \end{bmatrix}, \qquad A^T\mathbf{b} = \begin{bmatrix} 6 \\ 0 \end{bmatrix}

Solve 3x^1+3x^2=63\hat{x}_1 + 3\hat{x}_2 = 6 and 3x^1+5x^2=03\hat{x}_1 + 5\hat{x}_2 = 0: subtracting, 2x^2=62\hat{x}_2 = -6, so x^2=3\hat{x}_2 = -3 and x^1=5\hat{x}_1 = 5.

x^=[53],p=Ax^=[521],e=bp=[121]\hat{\mathbf{x}} = \begin{bmatrix} 5 \\ -3 \end{bmatrix}, \qquad \mathbf{p} = A\hat{\mathbf{x}} = \begin{bmatrix} 5 \\ 2 \\ -1 \end{bmatrix}, \qquad \mathbf{e} = \mathbf{b} - \mathbf{p} = \begin{bmatrix} 1 \\ -2 \\ 1 \end{bmatrix}

Perpendicularity: against column one, 12+1=01 - 2 + 1 = 0; against column two, 02+2=00 - 2 + 2 = 0. The error is in N(AT)N(A^T), perpendicular to the whole plane.

Problem 15.4 Extreme cases of P

Using P=A(ATA)1ATP = A(A^TA)^{-1}A^T, show two facts without choosing numbers: (a) if b\mathbf{b} is already in the column space of AA, then Pb=bP\mathbf{b} = \mathbf{b}; (b) if b\mathbf{b} is perpendicular to the column space of AA, then Pb=0P\mathbf{b} = \mathbf{0}.

Show solution

(a) If b\mathbf{b} is in the column space, then b=Ax\mathbf{b} = A\mathbf{x} for some x\mathbf{x}. Then

Pb=A(ATA)1ATAx=Ax=b,P\mathbf{b} = A(A^TA)^{-1}A^TA\mathbf{x} = A\mathbf{x} = \mathbf{b},

since (ATA)1(A^TA)^{-1} cancels ATAA^TA. A vector already in the subspace projects to itself.

(b) Perpendicular to the column space means b\mathbf{b} is in N(AT)N(A^T), so ATb=0A^T\mathbf{b} = \mathbf{0}. Then

Pb=A(ATA)1(ATb)=A(ATA)10=0.P\mathbf{b} = A(A^TA)^{-1}(A^T\mathbf{b}) = A(A^TA)^{-1}\mathbf{0} = \mathbf{0}.

A vector with no component in the subspace projects to zero.

Problem 15.5 Best line by least squares

Find the best straight line b=C+Dtb = C + Dt through the points (0,1)(0, 1), (1,1)(1, 1), and (2,3)(2, 3), by setting up the unsolvable system Ax=bA\mathbf{x} = \mathbf{b} and solving the normal equation ATAx^=ATbA^TA\hat{\mathbf{x}} = A^T\mathbf{b}.

Show solution

Asking the line to pass through each point:

C+0D=1C+D=1C+2D=3A=[101112],b=[113]\begin{aligned} C \phantom{{}+ 0D} &= 1 \\ C + D &= 1 \\ C + 2D &= 3 \end{aligned} \qquad\Longrightarrow\qquad A = \begin{bmatrix} 1 & 0 \\ 1 & 1 \\ 1 & 2 \end{bmatrix}, \quad \mathbf{b} = \begin{bmatrix} 1 \\ 1 \\ 3 \end{bmatrix}

No solution — so multiply both sides by ATA^T:

ATA=[3335],ATb=[57]A^TA = \begin{bmatrix} 3 & 3 \\ 3 & 5 \end{bmatrix}, \qquad A^T\mathbf{b} = \begin{bmatrix} 5 \\ 7 \end{bmatrix}

From 3C+3D=53C + 3D = 5 and 3C+5D=73C + 5D = 7: subtracting gives 2D=22D = 2, so D=1D = 1 and C=23C = \frac{2}{3}. The best line is

b=23+tb = \tfrac{2}{3} + t

Check the errors at t=0,1,2t = 0, 1, 2: predictions 23,53,83\frac{2}{3}, \frac{5}{3}, \frac{8}{3}, errors 13,23,13\frac{1}{3}, -\frac{2}{3}, \frac{1}{3}. They sum to zero — the error vector is perpendicular to the all-ones column of AA, just as the projection picture requires.