Session 16 · Unit 2

Projection Matrices and Least Squares


The projection matrix P is put to work. First a recap of what the magic formula does, tested on the two extreme cases, then the full least-squares problem worked end to end: fitting the best straight line through three points that no line passes through. The normal equations solve it, the error vector comes out perpendicular to the column space, and a proof shows why A transpose A is invertible whenever A has independent columns.

What the projection matrix does: two extreme cases

Last lecture ended with a magic formula — the projection matrix

P=A(ATA)1AT.P = A(A^TA)^{-1}A^T.

What is it doing? Multiply by any b\mathbf{b}: PbP\mathbf{b} is supposed to project b\mathbf{b} to the nearest point in the column space of AA. Strang recaps by testing the two extreme cases.

Case 1: b\mathbf{b} is in the column space already. Then projecting should change nothing — Pb=bP\mathbf{b} = \mathbf{b}. Why does the formula deliver that? A vector in the column space is a combination of the columns, and Strang insists you know how to write one: it has the form AxA\mathbf{x}. (“I’m going to stick that on the quiz or the final for sure.”) Feed b=Ax\mathbf{b} = A\mathbf{x} into the formula:

Pb=A(ATA)1ATAx=A(ATA)1(ATA)Ix=Ax=b.P\mathbf{b} = A(A^TA)^{-1}A^T A\mathbf{x} = A\underbrace{(A^TA)^{-1}(A^TA)}_{I}\,\mathbf{x} = A\mathbf{x} = \mathbf{b}.

Cancel, cancel — the ATAA^TA meets its inverse, leaving AxA\mathbf{x}, which was b\mathbf{b}.

Case 2: b\mathbf{b} is perpendicular to the column space. Picture the column space as a plane and b\mathbf{b} sticking straight up at ninety degrees. The nearest point in the plane is the origin: PbP\mathbf{b} should be 0\mathbf{0}. Which vectors are perpendicular to the column space? The four fundamental subspaces answer instantly: they are the vectors in the null space of ATA^T. So ATb=0A^T\mathbf{b} = \mathbf{0}, and the formula dies right at its last factor:

Pb=A(ATA)1ATb0=0.P\mathbf{b} = A(A^TA)^{-1}\underbrace{A^T\mathbf{b}}_{\mathbf{0}} = \mathbf{0}.

The average vector is neither extreme: it has a piece p\mathbf{p} in the column space and a piece e\mathbf{e} perpendicular to it. The projection preserves the first piece and kills the second, and the two pieces add back to the original: p+e=b\mathbf{p} + \mathbf{e} = \mathbf{b}.

The best straight line through three points

Now put the formula to work on the problem started at the end of last lecture. Three data points: at times t=1,2,3t = 1, 2, 3 the measured heights are b=1,2,2b = 1, 2, 2. The first two points suggest a nice forty-five-degree line — but the third point doesn’t lie on it. No line goes through all three. We want the best line y=C+Dty = C + Dt.

If a line did go through all three points, CC and DD would solve three equations:

C+D=1C+2D=2C+3D=2i.e.[111213]A[CD]=[122]b\begin{aligned} C + D &= 1 \\ C + 2D &= 2 \\ C + 3D &= 2 \end{aligned} \qquad\text{i.e.}\qquad \underbrace{\begin{bmatrix} 1 & 1 \\ 1 & 2 \\ 1 & 3 \end{bmatrix}}_{A} \begin{bmatrix} C \\ D \end{bmatrix} = \underbrace{\begin{bmatrix} 1 \\ 2 \\ 2 \end{bmatrix}}_{\mathbf{b}}

Three equations, two unknowns, no solution: the two columns of AA are independent — they’re a basis for the column space — but that column space doesn’t contain b\mathbf{b}.

Strang keeps two pictures on the board, and insists you see both. Picture one is in the ttyy plane: three points, a candidate line, and three vertical errors e1,e2,e3e_1, e_2, e_3 — the gap at each tt between the data value bib_i and the line’s value pip_i. Picture two is a vector picture in R3\mathbb{R}^3: the vector b=(1,2,2)\mathbf{b} = (1,2,2), the column space of AA spanned by (1,1,1)(1,1,1) and (1,2,3)(1,2,3), and the nearest point p=(p1,p2,p3)\mathbf{p} = (p_1, p_2, p_3) in that column space. Same problem twice: if we replaced the data 1,2,21, 2, 2 by the numbers p1,p2,p3p_1, p_2, p_3, the three equations would be solvable — a line goes through the pp‘s — because the vector p\mathbf{p} is in the column space. It’s the closest such vector.

An aside Strang takes seriously: fitting straight lines is a big part of statistics — the right word is regression, linear regression, and this sum of squares is its measure of error. Statisticians like it because it leads to a beautiful linear system, but they’re careful about the squaring. Suppose a fourth measurement at t=0t = 0 was way off. Its squared error would be way out of sight compared to the others, and the whole problem turns topsy-turvy over one point — an outlier, which could be a mistake after all. Least squares is the most used method, but not exclusively used, because it overcompensates for outliers.

Solving the normal equations

How to find x^=(C^,D^)\hat{\mathbf{x}} = (\hat{C}, \hat{D})?

Compute the pieces:

ATA=[111123][111213]=[36614],ATb=[111123][122]=[511]A^TA = \begin{bmatrix} 1 & 1 & 1 \\ 1 & 2 & 3 \end{bmatrix} \begin{bmatrix} 1 & 1 \\ 1 & 2 \\ 1 & 3 \end{bmatrix} = \begin{bmatrix} 3 & 6 \\ 6 & 14 \end{bmatrix}, \qquad A^T\mathbf{b} = \begin{bmatrix} 1 & 1 & 1 \\ 1 & 2 & 3 \end{bmatrix} \begin{bmatrix} 1 \\ 2 \\ 2 \end{bmatrix} = \begin{bmatrix} 5 \\ 11 \end{bmatrix}

And what do we expect of ATAA^TA before continuing? Symmetric — check. Invertible — check. (Near the end of the course Strang will add: positive definite, a future fact about this crucial matrix.) The normal equations, written out:

3C+6D=56C+14D=11\begin{aligned} 3C + 6D &= 5 \\ 6C + 14D &= 11 \end{aligned}

Before solving, Strang derives the same equations a second way — from calculus. The total squared error is a function of two variables,

E(C,D)=(C+D1)2+(C+2D2)2+(C+3D2)2,E(C, D) = (C + D - 1)^2 + (C + 2D - 2)^2 + (C + 3D - 2)^2,

and at the minimum both partial derivatives vanish. Because we’re differentiating squares, the derivatives are linear — that’s the beauty of these squares — and E/C=0\partial E/\partial C = 0 gives the first normal equation, E/D=0\partial E/\partial D = 0 the second. Wherever you look, these equations keep coming.

Now solve by elimination, “because that’s the only thing I know how to do”: subtract twice the first equation from the second to get 2D=12D = 1, so D=12D = \tfrac{1}{2}; back substitution gives 3C+3=53C + 3 = 5, so C=23C = \tfrac{2}{3}. The best line is

y=23+12t.y = \tfrac{2}{3} + \tfrac{1}{2}t.

The projection p and the error e

With the line in hand, read off the points on the line — the components of p\mathbf{p}:

What do we know about this error vector? First, p+e=b\mathbf{p} + \mathbf{e} = \mathbf{b} — the two pieces rebuild the original. Second, p\mathbf{p} and e\mathbf{e} are perpendicular. Dare to verify: the dot product is 736+20361336=0-\tfrac{7}{36} + \tfrac{20}{36} - \tfrac{13}{36} = 0. (“Thank you, God.”)

But we know even a little more: e\mathbf{e} is perpendicular not just to this particular p\mathbf{p} — it’s perpendicular to the whole column space. Test it against a nice vector in the column space, (1,1,1)(1,1,1): the dot product is 16+2616=0-\tfrac{1}{6} + \tfrac{2}{6} - \tfrac{1}{6} = 0. And against (1,2,3)(1,2,3): 16+4636=0-\tfrac{1}{6} + \tfrac{4}{6} - \tfrac{3}{6} = 0 again.

Why A transpose A is invertible

The whole calculation leaned on ATAA^TA being invertible — Strang announced it as the chapter started, and now he pays the debt with a proof. He doesn’t hammer away at many proofs in this course, “but this is like the central fact and it brings in all the stuff we know.”

(If AA has independent columns and Ax=0A\mathbf{x} = \mathbf{0}, then x=0\mathbf{x} = \mathbf{0} — Strang flags that implication too as “a fill-in question on the final exam.”)

Looking ahead: orthonormal columns

One case makes the geometry even better. Columns are certainly independent if they’re perpendicular — ruling out the zero column by giving them all length one. Perpendicular unit vectors, like (1,0,0)(1,0,0), (0,1,0)(0,1,0), (0,0,1)(0,0,1), are called orthonormal: ortho for perpendicular, normal for unit length. Those are the best columns you could ask for — for the matrix with those three columns, ATAA^TA is the identity.

A final example, Strang’s absolute favorite pair of orthonormal vectors in the plane:

[cosθsinθ],[sinθcosθ]\begin{bmatrix} \cos\theta \\ \sin\theta \end{bmatrix}, \qquad \begin{bmatrix} -\sin\theta \\ \cos\theta \end{bmatrix}

Both unit vectors, and the angle between them is ninety degrees. The job next time: see why orthonormal vectors are great, and then make vectors orthonormal by picking the right basis.

Problems

Work these before revealing the solutions — the lecture’s worked example is the template for all of them.

Problem 16.1 Best line through three points

Find the best straight line y=C+Dty = C + Dt through the points (1,0)(1, 0), (2,1)(2, 1), (3,3)(3, 3) in the least squares sense. Set up AA, b\mathbf{b}, and the normal equations, then solve for C^\hat{C} and D^\hat{D}.

Show solution

The three (unsolvable) equations are C+D=0C + D = 0, C+2D=1C + 2D = 1, C+3D=3C + 3D = 3, so

A=[111213],b=[013].A = \begin{bmatrix} 1 & 1 \\ 1 & 2 \\ 1 & 3 \end{bmatrix}, \qquad \mathbf{b} = \begin{bmatrix} 0 \\ 1 \\ 3 \end{bmatrix}.

Then ATA=[36614]A^TA = \begin{bmatrix} 3 & 6 \\ 6 & 14 \end{bmatrix} (same matrix as the lecture — same tt‘s) and ATb=[0+1+30+2+9]=[411]A^T\mathbf{b} = \begin{bmatrix} 0 + 1 + 3 \\ 0 + 2 + 9 \end{bmatrix} = \begin{bmatrix} 4 \\ 11 \end{bmatrix}.

Normal equations: 3C+6D=43C + 6D = 4 and 6C+14D=116C + 14D = 11. Subtract twice the first from the second: 2D=32D = 3, so D=32D = \tfrac{3}{2}. Back substitute: 3C+9=43C + 9 = 4, so C=53C = -\tfrac{5}{3}. Best line: y=53+32ty = -\tfrac{5}{3} + \tfrac{3}{2}t.

Problem 16.2 Check p + e = b and orthogonality

For the line found in Problem 16.1, compute the projection p=Ax^\mathbf{p} = A\hat{\mathbf{x}} and the error e=bp\mathbf{e} = \mathbf{b} - \mathbf{p}. Verify that e\mathbf{e} is perpendicular to both columns of AA.

Show solution

Evaluate the line at t=1,2,3t = 1, 2, 3: 53+32=16-\tfrac{5}{3} + \tfrac{3}{2} = -\tfrac{1}{6}, then 53+3=43-\tfrac{5}{3} + 3 = \tfrac{4}{3}, then 53+92=176-\tfrac{5}{3} + \tfrac{9}{2} = \tfrac{17}{6}. So

p=[1/64/317/6],e=bp=[1/61/31/6].\mathbf{p} = \begin{bmatrix} -1/6 \\ 4/3 \\ 17/6 \end{bmatrix}, \qquad \mathbf{e} = \mathbf{b} - \mathbf{p} = \begin{bmatrix} 1/6 \\ -1/3 \\ 1/6 \end{bmatrix}.

Against (1,1,1)(1,1,1): 1626+16=0\tfrac{1}{6} - \tfrac{2}{6} + \tfrac{1}{6} = 0. Against (1,2,3)(1,2,3): 1646+36=0\tfrac{1}{6} - \tfrac{4}{6} + \tfrac{3}{6} = 0. Both zero, so e\mathbf{e} is perpendicular to the whole column space — the same (16,26,16)(\tfrac{1}{6}, -\tfrac{2}{6}, \tfrac{1}{6}) pattern of errors as the lecture, with signs flipped.

Problem 16.3 Extreme cases of P

Let A=[111213]A = \begin{bmatrix} 1 & 1 \\ 1 & 2 \\ 1 & 3 \end{bmatrix} as in the lecture. Without computing PP entry by entry, find PbP\mathbf{b} for (a) b=[234]\mathbf{b} = \begin{bmatrix} 2 \\ 3 \\ 4 \end{bmatrix} and (b) b=[121]\mathbf{b} = \begin{bmatrix} 1 \\ -2 \\ 1 \end{bmatrix}. Justify each answer using the two extreme cases.

Show solution

(a) b=(2,3,4)\mathbf{b} = (2,3,4) is in the column space: it equals 1(1,1,1)+1(1,2,3)1\cdot(1,1,1) + 1\cdot(1,2,3), i.e. b=Ax\mathbf{b} = A\mathbf{x} with x=(1,1)\mathbf{x} = (1,1). Vectors already in the column space are unchanged by projection, so Pb=b=(2,3,4)P\mathbf{b} = \mathbf{b} = (2,3,4).

(b) Check b=(1,2,1)\mathbf{b} = (1,-2,1) against the columns: (1,2,1)(1,1,1)=0(1,-2,1)\cdot(1,1,1) = 0 and (1,2,1)(1,2,3)=14+3=0(1,-2,1)\cdot(1,2,3) = 1 - 4 + 3 = 0. So ATb=0A^T\mathbf{b} = \mathbf{0}: this b\mathbf{b} is in the null space of ATA^T, perpendicular to the column space, and the formula kills it at its last factor. Pb=0P\mathbf{b} = \mathbf{0}.

Problem 16.4 I minus P is a projection

Suppose PP is a projection matrix, so PT=PP^T = P and P2=PP^2 = P. Show algebraically that Q=IPQ = I - P satisfies the same two properties, and identify the subspace that QQ projects onto when PP projects onto the column space of AA.

Show solution

Symmetric: QT=(IP)T=ITPT=IP=QQ^T = (I - P)^T = I^T - P^T = I - P = Q.

Idempotent: Q2=(IP)(IP)=IPP+P2=I2P+P=IP=QQ^2 = (I - P)(I - P) = I - P - P + P^2 = I - 2P + P = I - P = Q, using P2=PP^2 = P.

Qb=bPb=eQ\mathbf{b} = \mathbf{b} - P\mathbf{b} = \mathbf{e}, the error part of b\mathbf{b}. Since e\mathbf{e} is perpendicular to the column space, QQ projects onto the orthogonal complement of C(A)C(A) — the null space of ATA^T.

Problem 16.5 When A transpose A fails

Let A=[121212]A = \begin{bmatrix} 1 & 2 \\ 1 & 2 \\ 1 & 2 \end{bmatrix}, whose columns are dependent. Compute ATAA^TA and show directly that it is singular, by finding a nonzero vector in its null space. Explain how this fits the theorem proved in lecture.

Show solution
ATA=[111222][121212]=[36612].A^TA = \begin{bmatrix} 1 & 1 & 1 \\ 2 & 2 & 2 \end{bmatrix} \begin{bmatrix} 1 & 2 \\ 1 & 2 \\ 1 & 2 \end{bmatrix} = \begin{bmatrix} 3 & 6 \\ 6 & 12 \end{bmatrix}.

The second row is twice the first, so ATAA^TA is singular. Take x=[21]\mathbf{x} = \begin{bmatrix} 2 \\ -1 \end{bmatrix}: then ATAx=[661212]=0A^TA\mathbf{x} = \begin{bmatrix} 6 - 6 \\ 12 - 12 \end{bmatrix} = \mathbf{0}.

This is exactly the lecture’s theorem run in reverse: the proof showed ATAx=0A^TA\mathbf{x} = \mathbf{0} forces Ax=0A\mathbf{x} = \mathbf{0}, and indeed Ax=2c1c2=0A\mathbf{x} = 2\mathbf{c}_1 - \mathbf{c}_2 = \mathbf{0} here because column two is twice column one. Dependent columns give AA a nonzero null space, and ATAA^TA inherits it — the theorem’s hypothesis of independent columns is necessary, not just convenient.