Practice / Linear algebra

Least squares, projections and the SVD

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=bAx = b with more equations than unknowns usually has no solution; least squares drops bb perpendicularly onto the column space of AA 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)⊤(1, 2, 2)^\top; ∥w∥=w⊤w\|w\| = \sqrt{w^\top w}, and ww, zz are orthogonal when w⊤z=0w^\top z = 0.
  • b∈Rnb \in \mathbb{R}^n and A∈Rn×kA \in \mathbb{R}^{n\times k} (nn rows, kk columns, n≥kn \ge k). col⁡(A)\operatorname{col}(A) is the set of all AxAx; col⁡(A)⊥\operatorname{col}(A)^\perp is the set of vectors orthogonal to every column of AA.
  • PP is a projection matrix: PbPb is the point of a line or a column space closest to bb. x^\hat x is the least-squares solution, the xx that minimises ∥Ax−b∥\|Ax - b\|, so Pb=Ax^P b = A\hat x; r=b−Ax^r = b - A\hat x is the residual.
  • The matrix-calculus page derived the normal equations A⊤Ax^=A⊤bA^\top A\hat x = A^\top b from the gradient of ∥Ax−b∥2\|Ax - b\|^2, and showed A⊤AA^\top A is invertible exactly when the columns of AA are independent; this page reaches them by geometry, and then by the SVD.
  • The singular value decomposition (SVD) is A=UΣV⊤A = U\Sigma V^\top with Σ=diag⁡(σ1,…,σk)\Sigma = \operatorname{diag}(\sigma_1, \dots, \sigma_k), σ1≥σ2≥⋯≥0\sigma_1 \ge \sigma_2 \ge \cdots \ge 0 the singular values; VV is a k×kk\times k orthogonal matrix, V⊤V=VV⊤=IV^\top V = VV^\top = I, with columns viv_i; UU is n×kn\times k with orthonormal columns uiu_i, U⊤U=IU^\top U = I. Column by column, AV=UΣAV = U\Sigma says Avi=σiuiAv_i = \sigma_iu_i. (This is the thin SVD; the full one pads UU to square and Σ\Sigma with zero rows.)
  • Σ+\Sigma^+ is Σ\Sigma with each nonzero σi\sigma_i replaced by 1/σi1/\sigma_i, and A+=VΣ+U⊤A^+ = V\Sigma^+U^\top is the pseudoinverse. ∥M∥2\|M\|_2, the spectral norm, is the largest ∥Mw∥\|Mw\| over unit vectors ww; it equals σ1(M)\sigma_1(M).

Builds on: Eigenvalues and eigenvectors by hand

Problems

  1. ·

    Find the projection pp of bb onto the line through aa, and the matrix PP with p=Pbp = Pb.

  2. ··

    Show that PP from Problem 1 satisfies P2=PP^2 = P and P⊤=PP^\top = P, has rank 11, and has eigenvalues 11 and 00. What are the eigenvectors?

  3. ··

    For AA with independent columns, find the matrix that projects onto col⁡(A)\operatorname{col}(A), and show the residual b−Pbb - Pb is orthogonal to every column of AA.

  4. ··

    Fit the line y=c+mxy = c + mx to the points (0,1)(0,1), (1,2)(1,2), (2,2)(2,2) by least squares: set up AA and bb, form the normal equations and solve them.

  5. ··

    Compute the residual r=b−Ax^r = b - A\hat x of Problem 4 and verify A⊤r=0A^\top r = 0.

  6. ···

    Compute the SVD of A=[3045]A = \begin{bmatrix}3&0\\4&5\end{bmatrix} by hand: singular values, VV, then UU.

  7. ··

    A 3×23\times2 matrix has singular values σ1>0\sigma_1 > 0 and σ2=0\sigma_2 = 0. Describe its rank, column space and null space in terms of u1u_1 and v2v_2.

  8. ···

    Define A+=VΣ+U⊤A^+ = V\Sigma^+U^\top. Show that when AA has independent columns, A+=(A⊤A)−1A⊤A^+ = (A^\top A)^{-1}A^\top, and compute A+bA^+b for Problem 4.

  9. ··

    For AA of Problem 6, write down the best rank-one approximation A1A_1 and the size of the error ∥A−A1∥2\|A - A_1\|_2.

  10. ···

    Show I−PI - P projects onto the orthogonal complement of col⁡(A)\operatorname{col}(A), and that ∥b∥2=∥Pb∥2+∥(I−P)b∥2\|b\|^2 = \|Pb\|^2 + \|(I-P)b\|^2.

Worked solutions

Problem 1

Find the projection pp of bb onto the line through aa, and the matrix PP with p=Pbp = Pb.

Here a,b∈Rna, b \in \mathbb{R}^n and a≠0a \neq 0.

  1. p=tap = ta for some number tt.pp lies on the line through aa, and every point of that line is a multiple of aa.
  2. a⊤(b−ta)=0a^\top(b - ta) = 0.The closest point is the foot of the perpendicular: the error b−pb - p must be orthogonal to the line. If it were not, sliding pp along the line would shorten it.
  3. a⊤b−t a⊤a=0a^\top b - t\,a^\top a = 0, so t=a⊤ba⊤at = \dfrac{a^\top b}{a^\top a}.Distribute a⊤a^\top; a⊤a=∥a∥2>0a^\top a = \|a\|^2 > 0, so it can be divided by.
  4. p=a⊤ba⊤a a=a (a⊤b)a⊤a=aa⊤a⊤a bp = \dfrac{a^\top b}{a^\top a}\,a = \dfrac{a\,(a^\top b)}{a^\top a} = \dfrac{aa^\top}{a^\top a}\,b.a⊤ba^\top b is a number, so it can move to the other side of aa; then regroup a(a⊤b)=(aa⊤)ba(a^\top b) = (aa^\top)b.
  5. p=a⊤ba⊤aap = \dfrac{a^\top b}{a^\top a}a, P=aa⊤a⊤aP = \dfrac{aa^\top}{a^\top a}PP is n×nn\times n: a column times a row, divided by a number. Check: a=(1,0)⊤a = (1, 0)^\top gives P=[1000]P = \begin{bmatrix}1&0\\0&0\end{bmatrix}, which keeps the first coordinate and zeroes the second.

Problem 2

Show that PP from Problem 1 satisfies P2=PP^2 = P and P⊤=PP^\top = P, has rank 11, and has eigenvalues 11 and 00. What are the eigenvectors?

  1. P2=aa⊤aa⊤(a⊤a)2=a (a⊤a) a⊤(a⊤a)2=aa⊤a⊤a=PP^2 = \dfrac{aa^\top aa^\top}{(a^\top a)^2} = \dfrac{a\,(a^\top a)\,a^\top}{(a^\top a)^2} = \dfrac{aa^\top}{a^\top a} = P.The middle a⊤aa^\top a is a number and cancels one factor of the denominator. Geometrically, projecting a point already on the line leaves it where it is.
  2. P⊤=(aa⊤)⊤a⊤a=(a⊤)⊤a⊤a⊤a=aa⊤a⊤a=PP^\top = \dfrac{(aa^\top)^\top}{a^\top a} = \dfrac{(a^\top)^\top a^\top}{a^\top a} = \dfrac{aa^\top}{a^\top a} = P.(XY)⊤=Y⊤X⊤(XY)^\top = Y^\top X^\top, and (a⊤)⊤=a(a^\top)^\top = a.
  3. Column jj of PP is aja⊤a a\dfrac{a_j}{a^\top a}\,a.Column jj of aa⊤aa^\top is aa times the jj-th entry of a⊤a^\top. Every column is a multiple of a≠0a \neq 0, so the column space is the line through aa and the rank is 11.
  4. Pa=a (a⊤a)a⊤a=aPa = \dfrac{a\,(a^\top a)}{a^\top a} = a.So aa is an eigenvector with eigenvalue 11.
  5. For x⊥ax \perp a: Px=a (a⊤x)a⊤a=0=0⋅xPx = \dfrac{a\,(a^\top x)}{a^\top a} = 0 = 0\cdot x.a⊤x=0a^\top x = 0 is what x⊥ax \perp a means. So every nonzero xx orthogonal to aa is an eigenvector with eigenvalue 00.
  6. The vectors orthogonal to aa form a subspace of dimension n−1n - 1, so λ=0\lambda = 0 has n−1n - 1 independent eigenvectors; with aa that makes nn, so there are no other eigenvalues.They are the solutions of one nonzero equation a⊤x=0a^\top x = 0 in nn unknowns. nn independent eigenvectors account for all nn eigenvalues, counted with multiplicity.
  7. P2=PP^2 = P and P⊤=PP^\top = P by direct multiplication; rank 11 (every column is a multiple of aa); Pa=aPa = a so λ=1\lambda = 1 with eigenvector aa, and Px=0Px = 0 for every x⊥ax\perp a, so λ=0\lambda = 0 with multiplicity n−1n-1In R2\mathbb{R}^2 with a=(1,1)⊤a = (1,1)^\top: P=12[1111]P = \tfrac12\begin{bmatrix}1&1\\1&1\end{bmatrix}, P(1,1)⊤=(1,1)⊤P(1,1)^\top = (1,1)^\top and P(1,−1)⊤=0P(1,-1)^\top = 0. PP is symmetric, so its eigenvectors for 11 and 00 are orthogonal, as on the previous page.

Problem 3

For AA with independent columns, find the matrix that projects onto col⁡(A)\operatorname{col}(A), and show the residual b−Pbb - Pb is orthogonal to every column of AA.

  1. Pb=Ax^Pb = A\hat x for some x^∈Rk\hat x \in \mathbb{R}^k.PbPb lies in col⁡(A)\operatorname{col}(A), and every point there is AA times some vector.
  2. b−Ax^⊥col⁡(A)b - A\hat x \perp \operatorname{col}(A), that is A⊤(b−Ax^)=0A^\top(b - A\hat x) = 0.As in Problem 1, the closest point is where the error is perpendicular to the whole subspace. Orthogonal to every column of AA means every entry of A⊤(b−Ax^)A^\top(b - A\hat x), one column dotted with the error per entry, is 00.
  3. A⊤Ax^=A⊤bA^\top A\hat x = A^\top b, so x^=(A⊤A)−1A⊤b\hat x = (A^\top A)^{-1}A^\top b.These are the normal equations, the same ones the matrix-calculus page reached by setting the gradient to 00. A⊤AA^\top A is invertible because the columns of AA are independent.
  4. Pb=Ax^=A(A⊤A)−1A⊤bPb = A\hat x = A(A^\top A)^{-1}A^\top b for every bb, so P=A(A⊤A)−1A⊤P = A(A^\top A)^{-1}A^\top.Read the matrix off. With one column aa, A⊤A=a⊤aA^\top A = a^\top a is a number and this is Problem 1's PP.
  5. P2=A(A⊤A)−1(A⊤A)(A⊤A)−1A⊤=A(A⊤A)−1A⊤=PP^2 = A(A^\top A)^{-1}(A^\top A)(A^\top A)^{-1}A^\top = A(A^\top A)^{-1}A^\top = P.The inner (A⊤A)(A⊤A)−1(A^\top A)(A^\top A)^{-1} is II.
  6. P⊤=A((A⊤A)−1)⊤A⊤=A(A⊤A)−1A⊤=PP^\top = A\big((A^\top A)^{-1}\big)^\top A^\top = A(A^\top A)^{-1}A^\top = P.Transpose reverses the product; the transpose of an inverse is the inverse of the transpose, and (A⊤A)⊤=A⊤A(A^\top A)^\top = A^\top A.
  7. P=A(A⊤A)−1A⊤P = A(A^\top A)^{-1}A^\top; A⊤(b−Pb)=A⊤b−A⊤b=0A^\top(b - Pb) = A^\top b - A^\top b = 0A⊤Pb=(A⊤A)(A⊤A)−1A⊤b=A⊤bA^\top Pb = (A^\top A)(A^\top A)^{-1}A^\top b = A^\top b. Each entry of A⊤(b−Pb)A^\top(b - Pb) is one column of AA dotted with the residual, so all of them are 00.

Problem 4

Fit the line y=c+mxy = c + mx to the points (0,1)(0,1), (1,2)(1,2), (2,2)(2,2) by least squares: set up AA and bb, form the normal equations and solve them.

Each point (x,y)(x, y) asks for c+m x=yc + m\,x = y. The three equations, in the unknowns cc and mm, are A (c,m)⊤=bA\,(c, m)^\top = b with A=[101112]A = \begin{bmatrix}1&0\\1&1\\1&2\end{bmatrix} and b=(1,2,2)⊤b = (1, 2, 2)^\top.

  1. Row ii of AA is (1,xi)(1, x_i) and entry ii of bb is yiy_i.cc is multiplied by 11 in every equation and mm by the point's xx. Three equations, two unknowns, and the points are not on one line, so there is no exact solution.
  2. A⊤A=[1+1+10+1+20+1+20+1+4]=[3335]A^\top A = \begin{bmatrix}1+1+1&0+1+2\\0+1+2&0+1+4\end{bmatrix} = \begin{bmatrix}3&3\\3&5\end{bmatrix}.Entry (i,j)(i,j) is column ii of AA dotted with column jj.
  3. A⊤b=(1+2+2, 0+2+4)⊤=(5,6)⊤A^\top b = (1+2+2,\ 0+2+4)^\top = (5, 6)^\top.Each column of AA dotted with bb.
  4. 3c+3m=53c + 3m = 5 and 3c+5m=63c + 5m = 6.The normal equations A⊤A (c,m)⊤=A⊤bA^\top A\,(c, m)^\top = A^\top b written out.
  5. 2m=12m = 1, so m=12m = \tfrac12; then 3c=5−32=723c = 5 - \tfrac32 = \tfrac72, so c=76c = \tfrac76.Subtract the first equation from the second to remove cc, then substitute back.
  6. A=[101112]A = \begin{bmatrix}1&0\\1&1\\1&2\end{bmatrix}, b=(1,2,2)⊤b = (1,2,2)^\top; A⊤A=[3335]A^\top A = \begin{bmatrix}3&3\\3&5\end{bmatrix}, A⊤b=(5,6)⊤A^\top b = (5,6)^\top; c=76c = \tfrac76, m=12m = \tfrac12Check in the first equation: 3⋅76+3⋅12=72+32=53\cdot\tfrac76 + 3\cdot\tfrac12 = \tfrac72 + \tfrac32 = 5. The fitted line y=76+12xy = \tfrac76 + \tfrac12x passes at heights 76\tfrac76, 53\tfrac53, 136\tfrac{13}6 over x=0,1,2x = 0, 1, 2.

Problem 5

Compute the residual r=b−Ax^r = b - A\hat x of Problem 4 and verify A⊤r=0A^\top r = 0.

  1. x^=(76,12)⊤\hat x = (\tfrac76, \tfrac12)^\top and Ax^=(76, 76+12, 76+1)⊤=(76,53,136)⊤A\hat x = (\tfrac76,\ \tfrac76 + \tfrac12,\ \tfrac76 + 1)^\top = (\tfrac76, \tfrac53, \tfrac{13}6)^\top.Row ii of AA is (1,xi)(1, x_i), so Ax^A\hat x lists the line's heights at the three xx values.
  2. r=(1−76, 2−53, 2−136)⊤=(−16,13,−16)⊤r = (1 - \tfrac76,\ 2 - \tfrac53,\ 2 - \tfrac{13}6)^\top = (-\tfrac16, \tfrac13, -\tfrac16)^\top.Subtract entry by entry; each entry is how far the point is above the line.
  3. First entry of A⊤rA^\top r: −16+13−16=0-\tfrac16 + \tfrac13 - \tfrac16 = 0.The first column of AA is all ones, so this is the sum of the residuals: a least-squares line with an intercept always balances its errors.
  4. Second entry: 0⋅(−16)+1⋅13+2⋅(−16)=13−13=00\cdot(-\tfrac16) + 1\cdot\tfrac13 + 2\cdot(-\tfrac16) = \tfrac13 - \tfrac13 = 0.The second column of AA is the xx values, so the residuals are also uncorrelated with xx.
  5. r=(−16,13,−16)⊤r = (-\tfrac16, \tfrac13, -\tfrac16)^\top and A⊤r=(0,0)⊤A^\top r = (0, 0)^\topThis is Problem 3's orthogonality on actual numbers: rr is orthogonal to both columns, so to all of col⁡(A)\operatorname{col}(A). The squared error is ∥r∥2=136+19+136=16\|r\|^2 = \tfrac1{36} + \tfrac19 + \tfrac1{36} = \tfrac16, and no other line does better.

Problem 6

Compute the SVD of A=[3045]A = \begin{bmatrix}3&0\\4&5\end{bmatrix} by hand: singular values, VV, then UU.

  1. A⊤A=[25202025]A^\top A = \begin{bmatrix}25&20\\20&25\end{bmatrix}, with eigenvalues 4545 and 55 and eigenvectors (1,1)⊤(1,1)^\top and (1,−1)⊤(1,-1)^\top.The previous page computed all of this (its Problem 10). A=UΣV⊤A = U\Sigma V^\top gives A⊤A=VΣU⊤UΣV⊤=VΣ2V⊤A^\top A = V\Sigma U^\top U\Sigma V^\top = V\Sigma^2V^\top, so the eigenvectors of A⊤AA^\top A are the viv_i and its eigenvalues are the σi2\sigma_i^2.
  2. σ1=45=35\sigma_1 = \sqrt{45} = 3\sqrt5 and σ2=5\sigma_2 = \sqrt5.Square roots of the eigenvalues, largest first.
  3. v1=12(1,1)⊤v_1 = \tfrac1{\sqrt2}(1,1)^\top, v2=12(1,−1)⊤v_2 = \tfrac1{\sqrt2}(1,-1)^\top, so V=12[111−1]V = \tfrac1{\sqrt2}\begin{bmatrix}1&1\\1&-1\end{bmatrix}.The columns of VV must be unit vectors, so divide each eigenvector by its length 2\sqrt2, in the order of the singular values.
  4. Av1=12(3, 4+5)⊤=12(3,9)⊤Av_1 = \tfrac1{\sqrt2}(3,\ 4 + 5)^\top = \tfrac1{\sqrt2}(3, 9)^\top, and u1=Av1σ1=(3,9)⊤2⋅35=110(1,3)⊤u_1 = \dfrac{Av_1}{\sigma_1} = \dfrac{(3, 9)^\top}{\sqrt2\cdot3\sqrt5} = \tfrac1{\sqrt{10}}(1, 3)^\top.Avi=σiuiAv_i = \sigma_iu_i (Before you start), so ui=Avi/σiu_i = Av_i/\sigma_i. Computing UU this way keeps its signs matched to VV.
  5. Av2=12(3, 4−5)⊤=12(3,−1)⊤Av_2 = \tfrac1{\sqrt2}(3,\ 4 - 5)^\top = \tfrac1{\sqrt2}(3, -1)^\top, and u2=(3,−1)⊤2⋅5=110(3,−1)⊤u_2 = \dfrac{(3, -1)^\top}{\sqrt2\cdot\sqrt5} = \tfrac1{\sqrt{10}}(3, -1)^\top.Same rule with σ2=5\sigma_2 = \sqrt5.
  6. u1⊤u2=110(3−3)=0u_1^\top u_2 = \tfrac1{10}(3 - 3) = 0 and u1⊤u1=u2⊤u2=1+910=1u_1^\top u_1 = u_2^\top u_2 = \tfrac{1 + 9}{10} = 1.They come out orthonormal automatically: ui⊤uj=vi⊤A⊤Avj/(σiσj)=σj2 vi⊤vj/(σiσj)u_i^\top u_j = v_i^\top A^\top Av_j/(\sigma_i\sigma_j) = \sigma_j^2\,v_i^\top v_j/(\sigma_i\sigma_j), which is 00 for i≠ji \neq j and 11 for i=ji = j.
  7. UΣ=110[353595−5]=12[339−1]U\Sigma = \tfrac1{\sqrt{10}}\begin{bmatrix}3\sqrt5&3\sqrt5\\9\sqrt5&-\sqrt5\end{bmatrix} = \tfrac1{\sqrt2}\begin{bmatrix}3&3\\9&-1\end{bmatrix}.Multiplying by a diagonal matrix on the right scales the columns of UU by 353\sqrt5 and 5\sqrt5; 5/10=1/2\sqrt5/\sqrt{10} = 1/\sqrt2.
  8. UΣV⊤=12[339−1][111−1]=12[60810]=[3045]U\Sigma V^\top = \tfrac12\begin{bmatrix}3&3\\9&-1\end{bmatrix}\begin{bmatrix}1&1\\1&-1\end{bmatrix} = \tfrac12\begin{bmatrix}6&0\\8&10\end{bmatrix} = \begin{bmatrix}3&0\\4&5\end{bmatrix}.V⊤=VV^\top = V here, and the two factors of 12\tfrac1{\sqrt2} make 12\tfrac12. Multiplying back is the check that nothing slipped.
  9. σ1=35\sigma_1 = 3\sqrt5, σ2=5\sigma_2 = \sqrt5; V=12[111−1]V = \tfrac1{\sqrt2}\begin{bmatrix}1&1\\1&-1\end{bmatrix}; ui=Avi/σiu_i = Av_i/\sigma_i gives U=110[133−1]U = \tfrac1{\sqrt{10}}\begin{bmatrix}1&3\\3&-1\end{bmatrix}; check UΣV⊤=AU\Sigma V^\top = AWhen the singular values are distinct and nonzero, as here, the SVD is unique up to signs: replacing viv_i by −vi-v_i flips uiu_i too, and the product is unchanged.

Problem 7

A 3×23\times2 matrix has singular values σ1>0\sigma_1 > 0 and σ2=0\sigma_2 = 0. Describe its rank, column space and null space in terms of u1u_1 and v2v_2.

  1. A=UΣV⊤=σ1u1v1⊤+σ2u2v2⊤=σ1u1v1⊤A = U\Sigma V^\top = \sigma_1u_1v_1^\top + \sigma_2u_2v_2^\top = \sigma_1u_1v_1^\top.Multiply out column by row: UΣU\Sigma has columns σiui\sigma_iu_i, and a product of matrices is the sum of each column of the first times the matching row of the second. The σ2\sigma_2 term is 00.
  2. Ax=σ1u1 (v1⊤x)Ax = \sigma_1u_1\,(v_1^\top x) for every x∈R2x \in \mathbb{R}^2.v1⊤xv_1^\top x is a number, so every output is a multiple of u1u_1.
  3. col⁡(A)=span⁡{u1}\operatorname{col}(A) = \operatorname{span}\{u_1\}, so the rank is 11.Step 2 puts every AxAx on the line through u1u_1, and x=v1x = v_1 gives Av1=σ1u1≠0Av_1 = \sigma_1u_1 \neq 0, so the whole line is reached. The rank is the dimension of the column space.
  4. Ax=0Ax = 0 exactly when v1⊤x=0v_1^\top x = 0, that is when xx is a multiple of v2v_2.σ1u1≠0\sigma_1u_1 \neq 0, so the product in step 2 is 00 only when the number v1⊤xv_1^\top x is. In R2\mathbb{R}^2 the vectors orthogonal to the unit vector v1v_1 are the multiples of v2v_2, since VV is orthogonal.
  5. rank 11; col⁡(A)=span⁡{u1}\operatorname{col}(A) = \operatorname{span}\{u_1\}; null space =span⁡{v2}=\operatorname{span}\{v_2\}, because Av2=σ2u2=0Av_2 = \sigma_2u_2 = 0In general the rank is the number of nonzero singular values, the uiu_i that go with them span the column space, and the viv_i that go with zero singular values span the null space. The rank count agrees: 22 columns =1= 1 (rank) +1+ 1 (null space dimension).

Problem 8

Define A+=VΣ+U⊤A^+ = V\Sigma^+U^\top. Show that when AA has independent columns, A+=(A⊤A)−1A⊤A^+ = (A^\top A)^{-1}A^\top, and compute A+bA^+b for Problem 4.

  1. Independent columns make every σi>0\sigma_i > 0, so Σ\Sigma is invertible and Σ+=Σ−1\Sigma^+ = \Sigma^{-1}.σi=∥Avi∥\sigma_i = \|Av_i\|, because Avi=σiuiAv_i = \sigma_iu_i with ∥ui∥=1\|u_i\| = 1; and Avi≠0Av_i \neq 0 since vi≠0v_i \neq 0 and only x=0x = 0 gives Ax=0Ax = 0 when the columns are independent.
  2. A⊤A=VΣU⊤UΣV⊤=VΣ2V⊤A^\top A = V\Sigma U^\top U\Sigma V^\top = V\Sigma^2V^\top.Substitute A=UΣV⊤A = U\Sigma V^\top and A⊤=VΣU⊤A^\top = V\Sigma U^\top (Σ\Sigma is diagonal, so Σ⊤=Σ\Sigma^\top = \Sigma); U⊤U=IU^\top U = I removes the middle.
  3. (A⊤A)−1=VΣ−2V⊤(A^\top A)^{-1} = V\Sigma^{-2}V^\top.Multiply to check: VΣ2V⊤ VΣ−2V⊤=VΣ2Σ−2V⊤=VV⊤=IV\Sigma^2V^\top\,V\Sigma^{-2}V^\top = V\Sigma^2\Sigma^{-2}V^\top = VV^\top = I, using V⊤V=IV^\top V = I and then VV⊤=IVV^\top = I.
  4. (A⊤A)−1A⊤=VΣ−2V⊤ VΣU⊤=VΣ−2ΣU⊤=VΣ−1U⊤(A^\top A)^{-1}A^\top = V\Sigma^{-2}V^\top\,V\Sigma U^\top = V\Sigma^{-2}\Sigma U^\top = V\Sigma^{-1}U^\top.V⊤V=IV^\top V = I again, and diagonal matrices multiply entry by entry: σi−2σi=σi−1\sigma_i^{-2}\sigma_i = \sigma_i^{-1}.
  5. For Problem 4, (A⊤A)−1=16[5−3−33](A^\top A)^{-1} = \tfrac16\begin{bmatrix}5&-3\\-3&3\end{bmatrix}.The 2×22\times2 inverse of [3335]\begin{bmatrix}3&3\\3&5\end{bmatrix}: determinant 15−9=615 - 9 = 6, swap the diagonal, negate the off-diagonal.
  6. A+b=(A⊤A)−1A⊤b=16[5−3−33][56]=16(25−18, −15+18)⊤=(76,12)⊤A^+b = (A^\top A)^{-1}A^\top b = \tfrac16\begin{bmatrix}5&-3\\-3&3\end{bmatrix}\begin{bmatrix}5\\6\end{bmatrix} = \tfrac16(25 - 18,\ -15 + 18)^\top = (\tfrac76, \tfrac12)^\top.Step 4 lets us use A⊤b=(5,6)⊤A^\top b = (5, 6)^\top from Problem 4 instead of computing an SVD of that AA.
  7. (A⊤A)−1A⊤=VΣ−1U⊤=A+(A^\top A)^{-1}A^\top = V\Sigma^{-1}U^\top = A^+; A+b=(76,12)⊤A^+b = (\tfrac76, \tfrac12)^\topThe same cc and mm as Problem 4: when the columns are independent, A+bA^+b is the least-squares solution x^\hat x. A+A^+ is still defined when they are not, which is why robust solvers such as NumPy's lstsq go through the SVD; there A+bA^+b is the least-squares solution of smallest norm.

Problem 9

For AA of Problem 6, write down the best rank-one approximation A1A_1 and the size of the error ∥A−A1∥2\|A - A_1\|_2.

  1. The Eckart–Young theorem: among all matrices of rank at most 11, A1=σ1u1v1⊤A_1 = \sigma_1u_1v_1^\top is closest to AA in ∥⋅∥2\|\cdot\|_2, and ∥A−A1∥2=σ2\|A - A_1\|_2 = \sigma_2.Quoted here, not proved (the spectral-norm form is Mirsky's). In ∥⋅∥2\|\cdot\|_2 it is a closest matrix, not the only one; in the Frobenius norm it is the unique closest when σ1>σ2\sigma_1 > \sigma_2. The same holds for rank jj, with Aj=∑i≤jσiuivi⊤A_j = \sum_{i \le j}\sigma_iu_iv_i^\top and error σj+1\sigma_{j+1}.
  2. A1=35⋅110(1,3)⊤⋅12(1,1)=3520[1133]=32[1133]A_1 = 3\sqrt5\cdot\tfrac1{\sqrt{10}}(1,3)^\top\cdot\tfrac1{\sqrt2}(1,1) = \tfrac{3\sqrt5}{\sqrt{20}}\begin{bmatrix}1&1\\3&3\end{bmatrix} = \tfrac32\begin{bmatrix}1&1\\3&3\end{bmatrix}.u1u_1, v1v_1 and σ1\sigma_1 from Problem 6; v1⊤=12(1,1)v_1^\top = \tfrac1{\sqrt2}(1,1) is a row, and a column times a row gives the matrix of products. 102=20=25\sqrt{10}\sqrt2 = \sqrt{20} = 2\sqrt5.
  3. A−A1=[3−320−324−925−92]=12[3−3−11]A - A_1 = \begin{bmatrix}3 - \tfrac32&0 - \tfrac32\\4 - \tfrac92&5 - \tfrac92\end{bmatrix} = \tfrac12\begin{bmatrix}3&-3\\-1&1\end{bmatrix}.Subtract entry by entry.
  4. 12[3−3−11]=5⋅110(3,−1)⊤⋅12(1,−1)=σ2u2v2⊤\tfrac12\begin{bmatrix}3&-3\\-1&1\end{bmatrix} = \sqrt5\cdot\tfrac1{\sqrt{10}}(3,-1)^\top\cdot\tfrac1{\sqrt2}(1,-1) = \sigma_2u_2v_2^\top.A=σ1u1v1⊤+σ2u2v2⊤A = \sigma_1u_1v_1^\top + \sigma_2u_2v_2^\top (Problem 7, step 1), so the error is exactly the dropped term; 5/20=12\sqrt5/\sqrt{20} = \tfrac12.
  5. ∥σ2u2v2⊤x∥=σ2 ∣v2⊤x∣≤σ2\|\sigma_2u_2v_2^\top x\| = \sigma_2\,\lvert v_2^\top x\rvert \le \sigma_2 for unit xx, with equality at x=v2x = v_2.∥u2∥=1\|u_2\| = 1, and ∣v2⊤x∣≤∥v2∥ ∥x∥=1\lvert v_2^\top x\rvert \le \|v_2\|\,\|x\| = 1 with equality when xx points along v2v_2; so the largest stretch is σ2\sigma_2.
  6. A1=σ1u1v1⊤A_1 = \sigma_1u_1v_1^\top and ∥A−A1∥2=σ2=5\|A - A_1\|_2 = \sigma_2 = \sqrt5Here A1=32[1133]A_1 = \tfrac32\begin{bmatrix}1&1\\3&3\end{bmatrix}. 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−PI - P projects onto the orthogonal complement of col⁡(A)\operatorname{col}(A), and that ∥b∥2=∥Pb∥2+∥(I−P)b∥2\|b\|^2 = \|Pb\|^2 + \|(I-P)b\|^2.

P=A(A⊤A)−1A⊤P = A(A^\top A)^{-1}A^\top is the projection of Problem 3, with P2=PP^2 = P and P⊤=PP^\top = P.

  1. (I−P)2=I−2P+P2=I−2P+P=I−P(I-P)^2 = I - 2P + P^2 = I - 2P + P = I - P, and (I−P)⊤=I−P(I - P)^\top = I - P.Expand, then P2=PP^2 = P; the transpose of II is II and P⊤=PP^\top = P. So I−PI - P is a projection matrix of the same kind as PP.
  2. A⊤(I−P)=A⊤−A⊤A(A⊤A)−1A⊤=A⊤−A⊤=0A^\top(I-P) = A^\top - A^\top A(A^\top A)^{-1}A^\top = A^\top - A^\top = 0.So every (I−P)b(I-P)b is orthogonal to every column of AA: I−PI - P lands in col⁡(A)⊥\operatorname{col}(A)^\perp.
  3. For w∈col⁡(A)⊥w \in \operatorname{col}(A)^\perp: Pw=A(A⊤A)−1(A⊤w)=0Pw = A(A^\top A)^{-1}(A^\top w) = 0, so (I−P)w=w(I-P)w = w.A⊤w=0A^\top w = 0 is what orthogonal to every column means. I−PI - P leaves that subspace fixed, so with step 2 its range is exactly col⁡(A)⊥\operatorname{col}(A)^\perp. And b−(I−P)b=Pbb - (I-P)b = Pb lies in col⁡(A)\operatorname{col}(A), orthogonal to that subspace, so (I−P)b(I-P)b is the foot of the perpendicular from bb, as in Problem 3; it is the residual b−Pbb - Pb.
  4. b=Pb+(I−P)bb = Pb + (I-P)b, so ∥b∥2=∥Pb∥2+2 (Pb)⊤(I−P)b+∥(I−P)b∥2\|b\|^2 = \|Pb\|^2 + 2\,(Pb)^\top(I-P)b + \|(I-P)b\|^2.Expand ∥w+z∥2=(w+z)⊤(w+z)=∥w∥2+2w⊤z+∥z∥2\|w + z\|^2 = (w+z)^\top(w+z) = \|w\|^2 + 2w^\top z + \|z\|^2.
  5. (Pb)⊤(I−P)b=b⊤P⊤(I−P)b=b⊤(P−P2)b=0(Pb)^\top(I-P)b = b^\top P^\top(I-P)b = b^\top(P - P^2)b = 0.P⊤=PP^\top = P and P2=PP^2 = P, so P⊤(I−P)=P−P2P^\top(I - P) = P - P^2 is the zero matrix. The cross term vanishes.
  6. I−PI - P is the projection onto col⁡(A)⊥\operatorname{col}(A)^\perp, and ∥b∥2=∥Pb∥2+∥(I−P)b∥2\|b\|^2 = \|Pb\|^2 + \|(I-P)b\|^2Pythagoras: bb splits into perpendicular pieces. For Problem 4, ∥b∥2=9\|b\|^2 = 9, ∥r∥2=16\|r\|^2 = \tfrac16 (Problem 5) and ∥Ax^∥2=4936+10036+16936=536\|A\hat x\|^2 = \tfrac{49}{36} + \tfrac{100}{36} + \tfrac{169}{36} = \tfrac{53}6; indeed 536+16=9\tfrac{53}6 + \tfrac16 = 9.

Where this goes wrong

1. Projecting with AAᵀ

aa⊤aa^\top is the top of the projection onto a line, and AA⊤AA^\top looks like its many-column version.

  1. Project b=(1,2,2)⊤b = (1,2,2)^\top onto col⁡(A)\operatorname{col}(A) for the AA of Problem 4Right so far: the answer should be Ax^=(76,53,136)⊤A\hat x = (\tfrac76, \tfrac53, \tfrac{13}6)^\top.
  2. “For a line it is aa⊤aa^\top over something; for a matrix, AA⊤AA^\top.”The half-memory that causes the mistake: the denominator of Problem 1, and what it becomes for many columns, is dropped.
  3. P=AA⊤P = AA^\topRight only when the columns of AA are orthonormal, so that A⊤A=IA^\top A = I; in general (A⊤A)−1(A^\top A)^{-1} undoes the columns' lengths and angles. Here AA⊤b=(5,11,17)⊤AA^\top b = (5, 11, 17)^\top, nowhere near bb, and AA⊤AA^\top is not a projection: its top-left entry is 11, while that of (AA⊤)2(AA^\top)^2 is 33. The test P2=PP^2 = 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.

  1. c=1c = 1, c+m=2c + m = 2, c+2m=2c + 2m = 2 for the points of Problem 4Right so far: these are the three equations A(c,m)⊤=bA(c, m)^\top = b.
  2. “Row-reduce [A ∣ b][A\,|\,b] and solve.”The step that causes the mistake: it looks for an exact solution.
  3. The first two equations give c=1c = 1, m=1m = 1; the third then says 3=23 = 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⊤bA^\top A\hat x = A^\top b instead, which always has a solution, and gives c=76c = \tfrac76, m=12m = \tfrac12. The line through the first two points, c=1c = 1, m=1m = 1, misses the third by 11 and has squared error 11, against 16\tfrac16 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.

  1. A=[3045]A = \begin{bmatrix}3&0\\4&5\end{bmatrix} from Problem 6Right so far.
  2. “AA is triangular, so its eigenvalues are 33 and 55 on the diagonal.”True, and the step that sets up the mistake: an easy list of numbers is at hand.
  3. σi=λi(A)\sigma_i = \lambda_i(A), so σ1=5\sigma_1 = 5 and σ2=3\sigma_2 = 3Singular values are the square roots of the eigenvalues of A⊤AA^\top A: for Problem 6 the eigenvalues of AA are 33 and 55 and the singular values are 5\sqrt5 and 353\sqrt5. The check: σ1=∥A∥2\sigma_1 = \|A\|_2 is the largest stretch, and ∥Av1∥=∥12(3,9)⊤∥=45=35>5\|Av_1\| = \|\tfrac1{\sqrt2}(3,9)^\top\| = \sqrt{45} = 3\sqrt5 > 5.

4. U from the eigenvectors of AᵀA

A⊤AA^\top A is the matrix the singular values came from, so it is tempting to take both UU and VV from it.

  1. A⊤AA^\top A for Problem 6 has unit eigenvectors 12(1,1)⊤\tfrac1{\sqrt2}(1,1)^\top and 12(1,−1)⊤\tfrac1{\sqrt2}(1,-1)^\topRight so far.
  2. “The SVD needs two orthogonal matrices, and here are two orthogonal eigenvectors.”The step that causes the mistake: A⊤AA^\top A has already supplied VV.
  3. UU is built from the eigenvectors of A⊤AA^\top A, that is U=12[111−1]U = \tfrac1{\sqrt2}\begin{bmatrix}1&1\\1&-1\end{bmatrix}Those are VV; UU comes from AA⊤AA^\top or from ui=Avi/σiu_i = Av_i/\sigma_i. With this UU, UΣV⊤=5[2112]U\Sigma V^\top = \sqrt5\begin{bmatrix}2&1\\1&2\end{bmatrix}, a symmetric matrix, and AA 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.

  1. r=(−16,13,−16)⊤r = (-\tfrac16, \tfrac13, -\tfrac16)^\top for Problem 4Right so far: Problem 5.
  2. “Check the fit by confirming the residual is orthogonal.”The step that causes the mistake: orthogonal to what is left unsaid, and bb is the vector at hand.
  3. Check b⊤r=0b^\top r = 0: −16+23−13=16≠0-\tfrac16 + \tfrac23 - \tfrac13 = \tfrac16 \neq 0, so “the fit is wrong”The residual is orthogonal to the column space, not to bb. Since b=Ax^+rb = A\hat x + r and A⊤r=0A^\top r = 0, b⊤r=x^⊤A⊤r+r⊤r=∥r∥2>0b^\top r = \hat x^\top A^\top r + r^\top r = \|r\|^2 > 0 whenever the fit is not exact; here 16=∥r∥2\tfrac16 = \|r\|^2, exactly as it should be. The right test is A⊤r=0A^\top r = 0.

Print this set: least-squares-projections-and-the-svd.pdf (problems, answers, and worked solutions on separate pages).