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.
“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, and a line through a vector a — a one-dimensional subspace. Which point on that line is closest to b?
The whole point of the picture is where orthogonality comes in. The closest point — the projectionp of b onto the line — is the point where the connecting segment meets the line at a right angle. The errore=b−p, “how much I’m wrong by,” is perpendicular to a. That’s the one fact we know, and it’s got to tell us everything. (You could do this triangle with trigonometry, angles θ, sines and cosines — that leads to “lousy formulas compared to linear algebra.”)
Since p lies on the line, it’s some multiple of a: write p=xa, and the problem is to find the number x. The perpendicularity condition is
aT(b−xa)=0
That’s the central equation. Simplify: xaTa=aTb, and since aTa is just a number — the length of a squared — divide by it:
x=aTaaTb,p=aaTaaTb
Two sanity checks before moving on. Double b, and the projection doubles too — an extra factor 2 rides straight through the formula. But double a and nothing changes: the line through 2a (or −a) is the same line, and indeed the formula picks up a factor 4 above and a factor 4 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 anywhere and watch p slide along the line while 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 onb: p=Pb. Reading the formula with the parentheses moved,
P=aTaaaT
Don’t cancel! Below is aTa, a number. Above is aaT, a column times a row — a full-scale n×n matrix. And it’s an interesting one.
What’s its column space? Multiply P by any vector b and you land on the line through a — so the column space of P 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/aTa, the projection p=xa, and the matrix P=aaT/aTa. The same three will reappear, slightly changed, in n dimensions.
Why project? Because Ax = b may have no solution
Why do we want projections at all? Because this chapter deals with systems Ax=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 Ax is always in the column space of A — that’s the constraint — but b probably is not. So replace b by the closest vector that is in the column space:
Ax^=p,p=projection of b onto the column space of A
The hat on x^ is a reminder: this is not the x that doesn’t exist — it’s the best possible x.
Projecting onto a subspace
Now the real problem, in three dimensions: a plane, and a vector b not in the plane. Project b down into the plane. To get a formula we need to describe the plane: give a basis, two vectors a1 and a2. 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 A whose columns are a1 and a2. Everything is cleaner in terms of a matrix. (And once we can do two columns, we can do n.)
The projection p is some combination of the basis vectors — rather than writing x^1a1+x^2a2, write it as p=Ax^. The problem: find the right combination of columns so that the error is perpendicular to the plane. The error e=b−Ax^ must be perpendicular to everything in the plane, and the plane is told to us by a1 and a2, so two equations:
a1T(b−Ax^)=0,a2T(b−Ax^)=0
Stack them into a matrix equation — a1T and a2T are exactly the rows of AT — and it comes out just right:
AT(b−Ax^)=0
Two remarks before solving. First: in the very first problem, A had one column, little a — change capital A to little a and this is the equation we solved on the line. Second: which of the four subspaces is e in? The equation says ATe=0, so e is in the null space ofAT.
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
The projection matrix looks a little messy but it’s not bad — it’s the “magic combination,” the n-dimensional version of aaT/aTa. 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=A−1(AT)−1? Then P=AA−1(AT)−1AT=I — the identity! Strang writes it on the board but warns: “don’t take the videotape quite so carefully.” The step is not allowed: A is not square — it has too many rows, just a couple of columns — so A−1 doesn’t exist. ATA is square and invertible, but it can’t be pulled apart.
Except… suppose Awere square and invertible. Then its column space is all of Rn, and projecting b onto the whole space should give back b itself — the projection matrix should be the identity. And that’s exactly what the split-apart computation gives: P=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=P: transpose the product — the symmetric middle factor (ATA)−1 stays symmetric, the outer A and AT swap ends, and you’re back to P. And P2=P — writing it out gives “eight A‘s in a row, which is quite obscene,” but put the parentheses in good places: an ATA sits next to its inverse in the middle, all that stuff goes, and what’s left is P 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,3 the measurements are b=1,2,2. They lie close to a line but not on one. We want the best line b=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.1Projection onto a line
Project b=126 onto the line through a=111. Find x, the projection p, and the error e, and verify that e is perpendicular to a.
Show solution
x=aTaaTb=1+1+11+2+6=39=3,p=3a=333
The error is
e=b−p=−2−13
and aTe=−2−1+3=0: perpendicular, as the geometry demands.
Problem 15.2Rank-one projection matrix
Compute the projection matrix P=aTaaaT for a=122. Check that P is symmetric, that P2=P, and give its rank and column space.
Show solution
aTa=1+4+4=9, and the column-times-row product gives
P=91122244244
Symmetric: yes — the (i,j) entry aiaj/9 equals the (j,i) entry.
P2=P: compute (aaT)(aaT)=a(aTa)aT=9aaT, so P2=819aaT=9aaT=P.
Rank and column space: every column is a multiple of a, so the rank is 1 and the column space is the line through a — exactly the line we project onto.
Problem 15.3Projection onto a plane
Let
A=111012,b=600
Project b onto the column space of A: find x^, the projection p, and the error e, and check that e is perpendicular to both columns of A.
Show solution
Form the normal equation ATAx^=ATb:
ATA=[3335],ATb=[60]
Solve 3x^1+3x^2=6 and 3x^1+5x^2=0: subtracting, 2x^2=−6, so x^2=−3 and x^1=5.
x^=[5−3],p=Ax^=52−1,e=b−p=1−21
Perpendicularity: against column one, 1−2+1=0; against column two, 0−2+2=0. The error is in N(AT), perpendicular to the whole plane.
Problem 15.4Extreme cases of P
Using P=A(ATA)−1AT, show two facts without choosing numbers: (a) if b is already in the column space of A, then Pb=b; (b) if b is perpendicular to the column space of A, then Pb=0.
Show solution
(a) If b is in the column space, then b=Ax for some x. Then
Pb=A(ATA)−1ATAx=Ax=b,
since (ATA)−1 cancels ATA. A vector already in the subspace projects to itself.
(b) Perpendicular to the column space means b is in N(AT), so ATb=0. Then
Pb=A(ATA)−1(ATb)=A(ATA)−10=0.
A vector with no component in the subspace projects to zero.
Problem 15.5Best line by least squares
Find the best straight line b=C+Dt through the points (0,1), (1,1), and (2,3), by setting up the unsolvable system Ax=b and solving the normal equation ATAx^=ATb.
Show solution
Asking the line to pass through each point:
C+0DC+DC+2D=1=1=3⟹A=111012,b=113
No solution — so multiply both sides by AT:
ATA=[3335],ATb=[57]
From 3C+3D=5 and 3C+5D=7: subtracting gives 2D=2, so D=1 and C=32. The best line is
b=32+t
Check the errors at t=0,1,2: predictions 32,35,38, errors 31,−32,31. They sum to zero — the error vector is perpendicular to the all-ones column of A, just as the projection picture requires.