Ten problems on projections, least squares and the singular value decomposition: projecting onto a line and a column space, fitting a line through three points, why the residual is orthogonal, an SVD computed by hand, reading rank and null space off it, the pseudoinverse and the best rank-one approximation, with worked solutions and the mistakes that confuse U with V or drop (AᵀA)⁻¹.
Before you start
A system Ax=b with more equations than unknowns usually has no solution; least squares drops b perpendicularly onto the column space of A and takes the foot of the perpendicular as the best fit. These ten problems build that projection, fit a line with it, then compute an SVD by hand and read the same answers off it.
Vectors are columns, like ;(1,2,2)⊤;,∥w∥=w⊤w, and ,w,z are orthogonal when .w⊤z=0.
b∈Rn and A∈Rn×k (n rows, k columns, ).n≥k).col(A) is the set of all ;Ax;col(A)⊥ is the set of vectors orthogonal to every column of .A.
P is a projection matrix: Pb is the point of a line or a column space closest to .b.x^ is the least-squares solution, the x that minimises ,∥Ax−b∥, so ;Pb=Ax^;r=b−Ax^ is the residual.
The matrix-calculus page derived the normal equations A⊤Ax^=A⊤b from the gradient of ,∥Ax−b∥2, and showed A⊤A is invertible exactly when the columns of A are independent; this page reaches them by geometry, and then by the SVD.
The singular value decomposition (SVD) is A=UΣV⊤ with ,Σ=diag(σ1,…,σk),σ1≥σ2≥⋯≥0 the singular values; V is a k×k orthogonal matrix, ,V⊤V=VV⊤=I, with columns ;vi;U is n×k with orthonormal columns ,ui,.U⊤U=I. Column by column, AV=UΣ says .Avi=σiui. (This is the thin SVD; the full one pads U to square and Σ with zero rows.)
Σ+ is Σ with each nonzero σi replaced by ,1/σi, and A+=VΣ+U⊤ is the pseudoinverse. ,∥M∥2, the spectral norm, is the largest ∥Mw∥ over unit vectors ;w; it equals .σ1(M).
Find the projection p of b onto the line through ,a, and the matrix P with .p=Pb.
··
Show that P from Problem 1 satisfies P2=P and ,P⊤=P, has rank ,1, and has eigenvalues 1 and .0. What are the eigenvectors?
··
For A with independent columns, find the matrix that projects onto ,col(A), and show the residual b−Pb is orthogonal to every column of .A.
··
Fit the line y=c+mx to the points ,(0,1),,(1,2),(2,2) by least squares: set up A and ,b, form the normal equations and solve them.
··
Compute the residual r=b−Ax^ of Problem 4 and verify .A⊤r=0.
···
Compute the SVD of A=[3405] by hand: singular values, ,V, then .U.
··
A 3×2 matrix has singular values σ1>0 and .σ2=0. Describe its rank, column space and null space in terms of u1 and .v2.
···
Define .A+=VΣ+U⊤. Show that when A has independent columns, ,A+=(A⊤A)−1A⊤, and compute A+b for Problem 4.
··
For A of Problem 6, write down the best rank-one approximation A1 and the size of the error .∥A−A1∥2.
···
Show I−P projects onto the orthogonal complement of ,col(A), and that .∥b∥2=∥Pb∥2+∥(I−P)b∥2.
Answers
,p=a⊤aa⊤ba,P=a⊤aaa⊤
P2=P and P⊤=P by direct multiplication; rank 1 (every column is a multiple of );a);Pa=a so λ=1 with eigenvector ,a, and Px=0 for every ,x⊥a, so λ=0 with multiplicity n−1
rank ;1;;col(A)=span{u1}; null space ,=span{v2}, because Av2=σ2u2=0
;(A⊤A)−1A⊤=VΣ−1U⊤=A+;A+b=(67,21)⊤
A1=σ1u1v1⊤ and ∥A−A1∥2=σ2=5
I−P is the projection onto ,col(A)⊥, and ∥b∥2=∥Pb∥2+∥(I−P)b∥2
Worked solutions
Problem 1
Find the projection p of b onto the line through ,a, and the matrix P with .p=Pb.
Here a,b∈Rn and .a=0.
p=ta for some number .t.p lies on the line through ,a, and every point of that line is a multiple of .a.
.a⊤(b−ta)=0.The closest point is the foot of the perpendicular: the error b−p must be orthogonal to the line. If it were not, sliding p along the line would shorten it.
,a⊤b−ta⊤a=0, so .t=a⊤aa⊤b.Distribute ;a⊤;,a⊤a=∥a∥2>0, so it can be divided by.
.p=a⊤aa⊤ba=a⊤aa(a⊤b)=a⊤aaa⊤b.a⊤b is a number, so it can move to the other side of ;a; then regroup .a(a⊤b)=(aa⊤)b.
,p=a⊤aa⊤ba,P=a⊤aaa⊤P is :n×n: a column times a row, divided by a number. Check: a=(1,0)⊤ gives ,P=[1000], which keeps the first coordinate and zeroes the second.
Problem 2
Show that P from Problem 1 satisfies P2=P and ,P⊤=P, has rank ,1, and has eigenvalues 1 and .0. What are the eigenvectors?
.P2=(a⊤a)2aa⊤aa⊤=(a⊤a)2a(a⊤a)a⊤=a⊤aaa⊤=P.The middle a⊤a is a number and cancels one factor of the denominator. Geometrically, projecting a point already on the line leaves it where it is.
.P⊤=a⊤a(aa⊤)⊤=a⊤a(a⊤)⊤a⊤=a⊤aaa⊤=P.,(XY)⊤=Y⊤X⊤, and .(a⊤)⊤=a.
Column j of P is .a⊤aaja.Column j of aa⊤ is a times the j-th entry of .a⊤. Every column is a multiple of ,a=0, so the column space is the line through a and the rank is .1.
.Pa=a⊤aa(a⊤a)=a.So a is an eigenvector with eigenvalue .1.
For :x⊥a:.Px=a⊤aa(a⊤x)=0=0⋅x.a⊤x=0 is what x⊥a means. So every nonzero x orthogonal to a is an eigenvector with eigenvalue .0.
The vectors orthogonal to a form a subspace of dimension ,n−1, so λ=0 has n−1 independent eigenvectors; with a that makes ,n, so there are no other eigenvalues.They are the solutions of one nonzero equation a⊤x=0 in n unknowns. n independent eigenvectors account for all n eigenvalues, counted with multiplicity.
P2=P and P⊤=P by direct multiplication; rank 1 (every column is a multiple of );a);Pa=a so λ=1 with eigenvector ,a, and Px=0 for every ,x⊥a, so λ=0 with multiplicity n−1In R2 with :a=(1,1)⊤:,P=21[1111],P(1,1)⊤=(1,1)⊤ and .P(1,−1)⊤=0.P is symmetric, so its eigenvectors for 1 and 0 are orthogonal, as on the previous page.
Problem 3
For A with independent columns, find the matrix that projects onto ,col(A), and show the residual b−Pb is orthogonal to every column of .A.
Pb=Ax^ for some .x^∈Rk.Pb lies in ,col(A), and every point there is A times some vector.
,b−Ax^⊥col(A), that is .A⊤(b−Ax^)=0.As in Problem 1, the closest point is where the error is perpendicular to the whole subspace. Orthogonal to every column of A means every entry of ,A⊤(b−Ax^), one column dotted with the error per entry, is .0.
,A⊤Ax^=A⊤b, so .x^=(A⊤A)−1A⊤b.These are the normal equations, the same ones the matrix-calculus page reached by setting the gradient to .0.A⊤A is invertible because the columns of A are independent.
Pb=Ax^=A(A⊤A)−1A⊤b for every ,b, so .P=A(A⊤A)−1A⊤.Read the matrix off. With one column ,a,A⊤A=a⊤a is a number and this is Problem 1's .P.
.P2=A(A⊤A)−1(A⊤A)(A⊤A)−1A⊤=A(A⊤A)−1A⊤=P.The inner (A⊤A)(A⊤A)−1 is .I.
.P⊤=A((A⊤A)−1)⊤A⊤=A(A⊤A)−1A⊤=P.Transpose reverses the product; the transpose of an inverse is the inverse of the transpose, and .(A⊤A)⊤=A⊤A.
;P=A(A⊤A)−1A⊤;A⊤(b−Pb)=A⊤b−A⊤b=0.A⊤Pb=(A⊤A)(A⊤A)−1A⊤b=A⊤b. Each entry of A⊤(b−Pb) is one column of A dotted with the residual, so all of them are .0.
Problem 4
Fit the line y=c+mx to the points ,(0,1),,(1,2),(2,2) by least squares: set up A and ,b, form the normal equations and solve them.
Each point (x,y) asks for .c+mx=y. The three equations, in the unknowns c and ,m, are A(c,m)⊤=b with A=111012 and .b=(1,2,2)⊤.
Row i of A is (1,xi) and entry i of b is .yi.c is multiplied by 1 in every equation and m by the point's .x. Three equations, two unknowns, and the points are not on one line, so there is no exact solution.
.A⊤A=[1+1+10+1+20+1+20+1+4]=[3335].Entry (i,j) is column i of A dotted with column .j.
.A⊤b=(1+2+2,0+2+4)⊤=(5,6)⊤.Each column of A dotted with .b.
3c+3m=5 and .3c+5m=6.The normal equations A⊤A(c,m)⊤=A⊤b written out.
,2m=1, so ;m=21; then ,3c=5−23=27, so .c=67.Subtract the first equation from the second to remove ,c, then substitute back.
,A=111012,;b=(1,2,2)⊤;,A⊤A=[3335],;A⊤b=(5,6)⊤;,c=67,m=21Check in the first equation: .3⋅67+3⋅21=27+23=5. The fitted line y=67+21x passes at heights ,67,,35,613 over .x=0,1,2.
Problem 5
Compute the residual r=b−Ax^ of Problem 4 and verify .A⊤r=0.
x^=(67,21)⊤ and .Ax^=(67,67+21,67+1)⊤=(67,35,613)⊤.Row i of A is ,(1,xi), so Ax^ lists the line's heights at the three x values.
.r=(1−67,2−35,2−613)⊤=(−61,31,−61)⊤.Subtract entry by entry; each entry is how far the point is above the line.
First entry of :A⊤r:.−61+31−61=0.The first column of A is all ones, so this is the sum of the residuals: a least-squares line with an intercept always balances its errors.
Second entry: .0⋅(−61)+1⋅31+2⋅(−61)=31−31=0.The second column of A is the x values, so the residuals are also uncorrelated with .x.
r=(−61,31,−61)⊤ and A⊤r=(0,0)⊤This is Problem 3's orthogonality on actual numbers: r is orthogonal to both columns, so to all of .col(A). The squared error is ,∥r∥2=361+91+361=61, and no other line does better.
Problem 6
Compute the SVD of A=[3405] by hand: singular values, ,V, then .U.
,A⊤A=[25202025], with eigenvalues 45 and 5 and eigenvectors (1,1)⊤ and .(1,−1)⊤.The previous page computed all of this (its Problem 10). A=UΣV⊤ gives ,A⊤A=VΣU⊤UΣV⊤=VΣ2V⊤, so the eigenvectors of A⊤A are the vi and its eigenvalues are the .σi2.
σ1=45=35 and .σ2=5.Square roots of the eigenvalues, largest first.
,v1=21(1,1)⊤,,v2=21(1,−1)⊤, so .V=21[111−1].The columns of V must be unit vectors, so divide each eigenvector by its length ,2, in the order of the singular values.
,Av1=21(3,4+5)⊤=21(3,9)⊤, and .u1=σ1Av1=2⋅35(3,9)⊤=101(1,3)⊤.Avi=σiui (Before you start), so .ui=Avi/σi. Computing U this way keeps its signs matched to .V.
,Av2=21(3,4−5)⊤=21(3,−1)⊤, and .u2=2⋅5(3,−1)⊤=101(3,−1)⊤.Same rule with .σ2=5.
u1⊤u2=101(3−3)=0 and .u1⊤u1=u2⊤u2=101+9=1.They come out orthonormal automatically: ,ui⊤uj=vi⊤A⊤Avj/(σiσj)=σj2vi⊤vj/(σiσj), which is 0 for i=j and 1 for .i=j.
.UΣ=101[359535−5]=21[393−1].Multiplying by a diagonal matrix on the right scales the columns of U by 35 and ;5;.5/10=1/2.
.UΣV⊤=21[393−1][111−1]=21[68010]=[3405].V⊤=V here, and the two factors of 21 make .21. Multiplying back is the check that nothing slipped.
,σ1=35,;σ2=5;;V=21[111−1];ui=Avi/σi gives ;U=101[133−1]; check UΣV⊤=AWhen the singular values are distinct and nonzero, as here, the SVD is unique up to signs: replacing vi by −vi flips ui too, and the product is unchanged.
Problem 7
A 3×2 matrix has singular values σ1>0 and .σ2=0. Describe its rank, column space and null space in terms of u1 and .v2.
.A=UΣV⊤=σ1u1v1⊤+σ2u2v2⊤=σ1u1v1⊤.Multiply out column by row: UΣ has columns ,σiui, and a product of matrices is the sum of each column of the first times the matching row of the second. The σ2 term is .0.
Ax=σ1u1(v1⊤x) for every .x∈R2.v1⊤x is a number, so every output is a multiple of .u1.
,col(A)=span{u1}, so the rank is .1.Step 2 puts every Ax on the line through ,u1, and x=v1 gives ,Av1=σ1u1=0, so the whole line is reached. The rank is the dimension of the column space.
Ax=0 exactly when ,v1⊤x=0, that is when x is a multiple of .v2.,σ1u1=0, so the product in step 2 is 0 only when the number v1⊤x is. In R2 the vectors orthogonal to the unit vector v1 are the multiples of ,v2, since V is orthogonal.
rank ;1;;col(A)=span{u1}; null space ,=span{v2}, because Av2=σ2u2=0In general the rank is the number of nonzero singular values, the ui that go with them span the column space, and the vi that go with zero singular values span the null space. The rank count agrees: 2 columns =1 (rank) +1 (null space dimension).
Problem 8
Define .A+=VΣ+U⊤. Show that when A has independent columns, ,A+=(A⊤A)−1A⊤, and compute A+b for Problem 4.
Independent columns make every ,σi>0, so Σ is invertible and .Σ+=Σ−1.,σi=∥Avi∥, because Avi=σiui with ;∥ui∥=1; and Avi=0 since vi=0 and only x=0 gives Ax=0 when the columns are independent.
.A⊤A=VΣU⊤UΣV⊤=VΣ2V⊤.Substitute A=UΣV⊤ and A⊤=VΣU⊤ (Σ is diagonal, so );Σ⊤=Σ);U⊤U=I removes the middle.
.(A⊤A)−1=VΣ−2V⊤.Multiply to check: ,VΣ2V⊤VΣ−2V⊤=VΣ2Σ−2V⊤=VV⊤=I, using V⊤V=I and then .VV⊤=I.
.(A⊤A)−1A⊤=VΣ−2V⊤VΣU⊤=VΣ−2ΣU⊤=VΣ−1U⊤.V⊤V=I again, and diagonal matrices multiply entry by entry: .σi−2σi=σi−1.
For Problem 4, .(A⊤A)−1=61[5−3−33].The 2×2 inverse of :[3335]: determinant ,15−9=6, swap the diagonal, negate the off-diagonal.
.A+b=(A⊤A)−1A⊤b=61[5−3−33][56]=61(25−18,−15+18)⊤=(67,21)⊤.Step 4 lets us use A⊤b=(5,6)⊤ from Problem 4 instead of computing an SVD of that .A.
;(A⊤A)−1A⊤=VΣ−1U⊤=A+;A+b=(67,21)⊤The same c and m as Problem 4: when the columns are independent, A+b is the least-squares solution .x^.A+ is still defined when they are not, which is why robust solvers such as NumPy's lstsq go through the SVD; there A+b is the least-squares solution of smallest norm.
Problem 9
For A of Problem 6, write down the best rank-one approximation A1 and the size of the error .∥A−A1∥2.
The Eckart–Young theorem: among all matrices of rank at most ,1,A1=σ1u1v1⊤ is closest to A in ,∥⋅∥2, and .∥A−A1∥2=σ2.Quoted here, not proved (the spectral-norm form is Mirsky's). In ∥⋅∥2 it is a closest matrix, not the only one; in the Frobenius norm it is the unique closest when .σ1>σ2. The same holds for rank ,j, with Aj=∑i≤jσiuivi⊤ and error .σj+1.
.A1=35⋅101(1,3)⊤⋅21(1,1)=2035[1313]=23[1313].,u1,v1 and σ1 from Problem 6; v1⊤=21(1,1) is a row, and a column times a row gives the matrix of products. .102=20=25.
.A−A1=[3−234−290−235−29]=21[3−1−31].Subtract entry by entry.
.21[3−1−31]=5⋅101(3,−1)⊤⋅21(1,−1)=σ2u2v2⊤.A=σ1u1v1⊤+σ2u2v2⊤ (Problem 7, step 1), so the error is exactly the dropped term; .5/20=21.
∥σ2u2v2⊤x∥=σ2∣v2⊤x∣≤σ2 for unit ,x, with equality at .x=v2.,∥u2∥=1, and ∣v2⊤x∣≤∥v2∥∥x∥=1 with equality when x points along ;v2; so the largest stretch is .σ2.
A1=σ1u1v1⊤ and ∥A−A1∥2=σ2=5Here .A1=23[1313]. The error is the largest singular value thrown away, so a matrix whose singular values fall off fast is well approximated by few terms: the idea behind low-rank compression and PCA.
Problem 10
Show I−P projects onto the orthogonal complement of ,col(A), and that .∥b∥2=∥Pb∥2+∥(I−P)b∥2.
P=A(A⊤A)−1A⊤ is the projection of Problem 3, with P2=P and .P⊤=P.
,(I−P)2=I−2P+P2=I−2P+P=I−P, and .(I−P)⊤=I−P.Expand, then ;P2=P; the transpose of I is I and .P⊤=P. So I−P is a projection matrix of the same kind as .P.
.A⊤(I−P)=A⊤−A⊤A(A⊤A)−1A⊤=A⊤−A⊤=0.So every (I−P)b is orthogonal to every column of :A:I−P lands in .col(A)⊥.
For :w∈col(A)⊥:,Pw=A(A⊤A)−1(A⊤w)=0, so .(I−P)w=w.A⊤w=0 is what orthogonal to every column means. I−P leaves that subspace fixed, so with step 2 its range is exactly .col(A)⊥. And b−(I−P)b=Pb lies in ,col(A), orthogonal to that subspace, so (I−P)b is the foot of the perpendicular from ,b, as in Problem 3; it is the residual .b−Pb.
,b=Pb+(I−P)b, so .∥b∥2=∥Pb∥2+2(Pb)⊤(I−P)b+∥(I−P)b∥2.Expand .∥w+z∥2=(w+z)⊤(w+z)=∥w∥2+2w⊤z+∥z∥2.
.(Pb)⊤(I−P)b=b⊤P⊤(I−P)b=b⊤(P−P2)b=0.P⊤=P and ,P2=P, so P⊤(I−P)=P−P2 is the zero matrix. The cross term vanishes.
I−P is the projection onto ,col(A)⊥, and ∥b∥2=∥Pb∥2+∥(I−P)b∥2Pythagoras: b splits into perpendicular pieces. For Problem 4, ,∥b∥2=9,∥r∥2=61 (Problem 5) and ;∥Ax^∥2=3649+36100+36169=653; indeed .653+61=9.
Where this goes wrong
1. Projecting with AAᵀ
aa⊤ is the top of the projection onto a line, and AA⊤ looks like its many-column version.
Project b=(1,2,2)⊤ onto col(A) for the A of Problem 4Right so far: the answer should be .Ax^=(67,35,613)⊤.
“For a line it is aa⊤ over something; for a matrix, .AA⊤.”The half-memory that causes the mistake: the denominator of Problem 1, and what it becomes for many columns, is dropped.
P=AA⊤Right only when the columns of A are orthonormal, so that ;A⊤A=I; in general (A⊤A)−1 undoes the columns' lengths and angles. Here ,AA⊤b=(5,11,17)⊤, nowhere near ,b, and AA⊤ is not a projection: its top-left entry is ,1, while that of (AA⊤)2 is .3. The test P2=P catches it.
2. Solving Ax = b when it has no solution
A system of equations invites row reduction, and row reduction is the right tool only when a solution exists.
,c=1,,c+m=2,c+2m=2 for the points of Problem 4Right so far: these are the three equations .A(c,m)⊤=b.
“Row-reduce [A∣b] and solve.”The step that causes the mistake: it looks for an exact solution.
The first two equations give ,c=1,;m=1; the third then says ,3=2, so “no line fits and there is no answer.”Three equations, two unknowns: the inconsistency is expected, since the three points are not on one line. Least squares solves A⊤Ax^=A⊤b instead, which always has a solution, and gives ,c=67,.m=21. The line through the first two points, ,c=1,,m=1, misses the third by 1 and has squared error ,1, against 61 for the fit.
3. Singular values as eigenvalues of A
For symmetric matrices with nonnegative eigenvalues the two lists agree, and the habit carries over.
A=[3405] from Problem 6Right so far.
“A is triangular, so its eigenvalues are 3 and 5 on the diagonal.”True, and the step that sets up the mistake: an easy list of numbers is at hand.
,σi=λi(A), so σ1=5 and σ2=3Singular values are the square roots of the eigenvalues of :A⊤A: for Problem 6 the eigenvalues of A are 3 and 5 and the singular values are 5 and .35. The check: σ1=∥A∥2 is the largest stretch, and .∥Av1∥=∥21(3,9)⊤∥=45=35>5.
4. U from the eigenvectors of AᵀA
A⊤A is the matrix the singular values came from, so it is tempting to take both U and V from it.
A⊤A for Problem 6 has unit eigenvectors 21(1,1)⊤ and 21(1,−1)⊤Right so far.
“The SVD needs two orthogonal matrices, and here are two orthogonal eigenvectors.”The step that causes the mistake: A⊤A has already supplied .V.
U is built from the eigenvectors of ,A⊤A, that is U=21[111−1]Those are ;V;U comes from AA⊤ or from .ui=Avi/σi. With this ,U,,UΣV⊤=5[2112], a symmetric matrix, and A is not symmetric. Multiplying back catches it.
5. Residual orthogonal to b
“The residual is orthogonal” is remembered without the thing it is orthogonal to.
r=(−61,31,−61)⊤ for Problem 4Right so far: Problem 5.
“Check the fit by confirming the residual is orthogonal.”The step that causes the mistake: orthogonal to what is left unsaid, and b is the vector at hand.
Check :b⊤r=0:,−61+32−31=61=0, so “the fit is wrong”The residual is orthogonal to the column space, not to .b. Since b=Ax^+r and ,A⊤r=0,b⊤r=x^⊤A⊤r+r⊤r=∥r∥2>0 whenever the fit is not exact; here ,61=∥r∥2, exactly as it should be. The right test is .A⊤r=0.