Practice / Linear algebra

Orthogonal bases, Gram–Schmidt and the QR decomposition

Ten problems on orthogonality worked by hand: dot products, lengths and angles, coordinates in an orthonormal basis by dot products, a Householder reflection and the properties of an orthogonal matrix, Gram–Schmidt in the plane and in three dimensions, the QR decomposition of a 3×2 and a 3×3 matrix with det A from R, least squares by solving Rx = Qᵀb, the projection QQᵀ and the orthogonal complement, a dependent column that Gram–Schmidt turns into zero, and why nearly collinear features break the normal equations but not QR, with worked solutions and the mistakes of skipping the division by a·a, projecting onto a column instead of q, transposing R and taking QQᵀ to be I.

Before you start

An orthonormal basis turns linear algebra into dot products. Coordinates come from a dot product instead of a linear system, a projection is a sum of dot products, and the inverse of a matrix with orthonormal columns is its transpose. Gram–Schmidt builds such a basis from any independent set of vectors by subtracting, from each new vector, its projections onto the directions already found, and the bookkeeping of that process is the factorization A=QRA = QR. The mistakes are in the projections: a coefficient not divided by a⊤aa^\top a, a projection onto an original column instead of a finished qq, and a tall QQ treated as if it were square. These ten problems build one orthonormal basis of R3\mathbb{R}^3 and use it for a reflection, a QR decomposition, a determinant and a least-squares fit, and end with the reason libraries solve least squares with QR rather than the normal equations.

  • Dot products. a⊤b=a1b1+⋯+anbna^\top b = a_1b_1 + \cdots + a_nb_n, ∥a∥=a⊤a\|a\| = \sqrt{a^\top a}, and the angle θ\theta between nonzero vectors has cos⁡θ=a⊤b∥a∥ ∥b∥\cos\theta = \dfrac{a^\top b}{\|a\|\,\|b\|}. aa and bb are orthogonal when a⊤b=0a^\top b = 0.
  • Orthonormal. Vectors q1,…,qkq_1, \dots, q_k are orthonormal when qi⊤qj=0q_i^\top q_j = 0 for i≠ji \ne j and ∥qi∥=1\|q_i\| = 1. Stacked as the columns of QQ, that says Q⊤Q=IkQ^\top Q = I_k.
  • Orthogonal matrix. A square QQ with Q⊤Q=IQ^\top Q = I. Then Q−1=Q⊤Q^{-1} = Q^\top, QQ⊤=IQQ^\top = I as well, ∥Qx∥=∥x∥\|Qx\| = \|x\| for every xx, and det⁡Q=±1\det Q = \pm1. A tall QQ (n×kn \times k with k<nk < n) still has Q⊤Q=IkQ^\top Q = I_k, but QQ⊤QQ^\top is not InI_n.
  • Projection onto a line. The projection of bb onto the line through a≠0a \ne 0 is a⊤ba⊤a a\dfrac{a^\top b}{a^\top a}\,a. For a unit vector qq it is (q⊤b) q(q^\top b)\,q.
  • Gram–Schmidt. u1=a1u_1 = a_1; then uj=aj−∑i<j(qi⊤aj) qiu_j = a_j - \sum_{i<j}(q_i^\top a_j)\,q_i and qj=uj/∥uj∥q_j = u_j/\|u_j\|. Each uju_j is aja_j minus its projections onto the qq's already built. If some uj=0u_j = 0, then aja_j is a combination of the earlier columns.
  • QR. For AA with independent columns, A=QRA = QR, with QQ the Gram–Schmidt vectors as columns and R=Q⊤AR = Q^\top A upper triangular: rij=qi⊤ajr_{ij} = q_i^\top a_j for i<ji < j and rjj=∥uj∥>0r_{jj} = \|u_j\|> 0.
  • Least squares. The x^\hat x that minimizes ∥Ax−b∥\|Ax - b\| solves A⊤Ax^=A⊤bA^\top A\hat x = A^\top b; with A=QRA = QR this becomes Rx^=Q⊤bR\hat x = Q^\top b, and the projection of bb onto col⁡(A)\operatorname{col}(A) is QQ⊤bQQ^\top b.
  • Condition number. κ(A)=σmax⁡/σmin⁡\kappa(A) = \sigma_{\max}/\sigma_{\min}, the ratio of the largest and smallest singular values. Roughly, solving a system with condition number κ\kappa in double precision loses log⁡10κ\log_{10}\kappa of its 1616 significant digits.

Problems

  1. ·

    For a=(1,2,2)⊤a = (1, 2, 2)^\top, b=(3,0,4)⊤b = (3, 0, 4)^\top and c=(2,1,−2)⊤c = (2, 1, -2)^\top, find a⊤ba^\top b, ∥a∥\|a\|, ∥b∥\|b\| and the angle between aa and bb, and show that aa and cc are orthogonal.

  2. ·

    q1=15(3,4)⊤q_1 = \tfrac15(3, 4)^\top and q2=15(−4,3)⊤q_2 = \tfrac15(-4, 3)^\top form an orthonormal basis of R2\mathbb{R}^2. Write x=(2,11)⊤x = (2, 11)^\top as c1q1+c2q2c_1q_1 + c_2q_2 without solving a system.

  3. ··

    Let x=(1,2,2)⊤x = (1, 2, 2)^\top and v=x−3e1=(−2,2,2)⊤v = x - 3e_1 = (-2, 2, 2)^\top. Compute the Householder reflection H=I−2vv⊤v⊤vH = I - \dfrac{2vv^\top}{v^\top v}, and show that Hx=3e1Hx = 3e_1, H⊤H=IH^\top H = I, H−1=HH^{-1} = H and det⁡H=−1\det H = -1.

  4. ··

    Apply Gram–Schmidt to a1=(3,4)⊤a_1 = (3, 4)^\top and a2=(2,11)⊤a_2 = (2, 11)^\top.

  5. ··

    Find the QR decomposition of A=[142522]A = \begin{bmatrix}1&4\\2&5\\2&2\end{bmatrix}.

  6. ···

    Extend Problem 5 with a third column: find the QR decomposition of A=[145251221]A = \begin{bmatrix}1&4&5\\2&5&1\\2&2&1\end{bmatrix}, and use it to find det⁡A\det A.

  7. ··

    Use the QR decomposition of Problem 5 to find the least-squares solution of Ax=bAx = b for A=[142522]A = \begin{bmatrix}1&4\\2&5\\2&2\end{bmatrix} and b=(3,3,6)⊤b = (3, 3, 6)^\top, and the residual.

  8. ··

    For the tall QQ of Problem 5, find P=QQ⊤P = QQ^\top and show it is the projection onto the plane spanned by the columns of AA. Find the orthogonal complement of that plane, and show QQ⊤≠IQQ^\top \ne I although Q⊤Q=IQ^\top Q = I.

  9. ··

    Run Gram–Schmidt on a1=(1,2,2)⊤a_1 = (1, 2, 2)^\top, a2=(4,5,2)⊤a_2 = (4, 5, 2)^\top, a3=(6,9,6)⊤a_3 = (6, 9, 6)^\top. What happens at the third vector, and what does it say about A=[a1 a2 a3]A = [a_1\ a_2\ a_3]?

  10. ···

    Two features are almost collinear:

    A=[11ε00ε],ε=10−8.A = \begin{bmatrix}1&1\\ \varepsilon&0\\ 0&\varepsilon\end{bmatrix},\qquad \varepsilon = 10^{-8}.

    Show that κ(A⊤A)=κ(A)2\kappa(A^\top A) = \kappa(A)^2 in general, find both for this AA, and explain why forming A⊤AA^\top A in double precision fails while QR on AA does not.

Worked solutions

Problem 1

For a=(1,2,2)⊤a = (1, 2, 2)^\top, b=(3,0,4)⊤b = (3, 0, 4)^\top and c=(2,1,−2)⊤c = (2, 1, -2)^\top, find a⊤ba^\top b, ∥a∥\|a\|, ∥b∥\|b\| and the angle between aa and bb, and show that aa and cc are orthogonal.

  1. a⊤b=1⋅3+2⋅0+2⋅4=11a^\top b = 1\cdot3 + 2\cdot0 + 2\cdot4 = 11Multiply matching entries and add.
  2. ∥a∥=1+4+4=3\|a\| = \sqrt{1 + 4 + 4} = 3 and ∥b∥=9+0+16=5\|b\| = \sqrt{9 + 0 + 16} = 5The length is the square root of a vector's dot product with itself.
  3. cos⁡θ=113⋅5=1115\cos\theta = \dfrac{11}{3\cdot5} = \dfrac{11}{15}Divide the dot product by both lengths.
  4. a⊤c=2+2−4=0a^\top c = 2 + 2 - 4 = 0A zero dot product is orthogonality.
  5. a⊤b=11a^\top b = 11, ∥a∥=3\|a\| = 3, ∥b∥=5\|b\| = 5, θ=arccos⁡1115≈42.8∘\theta = \arccos\tfrac{11}{15} \approx 42.8^\circ; a⊤c=0a^\top c = 0The cosine is between −1-1 and 11, as Cauchy–Schwarz, ∣a⊤b∣≤∥a∥ ∥b∥|a^\top b| \le \|a\|\,\|b\|, guarantees.

Problem 2

q1=15(3,4)⊤q_1 = \tfrac15(3, 4)^\top and q2=15(−4,3)⊤q_2 = \tfrac15(-4, 3)^\top form an orthonormal basis of R2\mathbb{R}^2. Write x=(2,11)⊤x = (2, 11)^\top as c1q1+c2q2c_1q_1 + c_2q_2 without solving a system.

  1. q1⊤q1=9+1625=1q_1^\top q_1 = \tfrac{9 + 16}{25} = 1, q2⊤q2=1q_2^\top q_2 = 1, q1⊤q2=−12+1225=0q_1^\top q_2 = \tfrac{-12 + 12}{25} = 0Check the basis is orthonormal before relying on it.
  2. q1⊤x=c1 q1⊤q1+c2 q1⊤q2=c1q_1^\top x = c_1\,q_1^\top q_1 + c_2\,q_1^\top q_2 = c_1Dot both sides of x=c1q1+c2q2x = c_1q_1 + c_2q_2 with q1q_1: the cross term is 00 and q1⊤q1=1q_1^\top q_1 = 1.
  3. c1=q1⊤x=6+445=10c_1 = q_1^\top x = \tfrac{6 + 44}{5} = 10The coordinate is the dot product.
  4. c2=q2⊤x=−8+335=5c_2 = q_2^\top x = \tfrac{-8 + 33}{5} = 5The same with q2q_2.
  5. x=10q1+5q2x = 10q_1 + 5q_2Check: 10q1+5q2=(6,8)⊤+(−4,3)⊤=(2,11)⊤10q_1 + 5q_2 = (6, 8)^\top + (-4, 3)^\top = (2, 11)^\top. In a basis that is not orthogonal, the coordinates need a 2×22\times2 solve; here they are two dot products.

Problem 3

Let x=(1,2,2)⊤x = (1, 2, 2)^\top and v=x−3e1=(−2,2,2)⊤v = x - 3e_1 = (-2, 2, 2)^\top. Compute the Householder reflection H=I−2vv⊤v⊤vH = I - \dfrac{2vv^\top}{v^\top v}, and show that Hx=3e1Hx = 3e_1, H⊤H=IH^\top H = I, H−1=HH^{-1} = H and det⁡H=−1\det H = -1.

  1. v⊤v=4+4+4=12v^\top v = 4 + 4 + 4 = 12 and vv⊤=[4−4−4−444−444]vv^\top = \begin{bmatrix}4&-4&-4\\-4&4&4\\-4&4&4\end{bmatrix}Entry (i,j)(i, j) of vv⊤vv^\top is vivjv_iv_j.
  2. H=I−16vv⊤=13[12221−22−21]H = I - \tfrac16 vv^\top = \tfrac13\begin{bmatrix}1&2&2\\2&1&-2\\2&-2&1\end{bmatrix}1−46=131 - \tfrac46 = \tfrac13 on the diagonal, 46=23\tfrac46 = \tfrac23 off it, with the signs of −vv⊤-vv^\top.
  3. Hx=13(1+4+4, 2+2−4, 2−4+2)⊤=(3,0,0)⊤Hx = \tfrac13(1 + 4 + 4,\ 2 + 2 - 4,\ 2 - 4 + 2)^\top = (3, 0, 0)^\top∥x∥=3\|x\| = 3, so xx is sent to the same length along e1e_1: this is how QR algorithms zero out a column.
  4. The columns 13(1,2,2)\tfrac13(1, 2, 2), 13(2,1,−2)\tfrac13(2, 1, -2), 13(2,−2,1)\tfrac13(2, -2, 1) have length 11 and pairwise dot products 19(2+2−4)=19(2−4+2)=19(4−2−2)=0\tfrac19(2 + 2 - 4) = \tfrac19(2 - 4 + 2) = \tfrac19(4 - 2 - 2) = 0So H⊤H=IH^\top H = I. HH is also symmetric, so H−1=H⊤=HH^{-1} = H^\top = H: reflecting twice puts every vector back.
  5. H=13[12221−22−21]H = \tfrac13\begin{bmatrix}1&2&2\\2&1&-2\\2&-2&1\end{bmatrix}, Hx=3e1Hx = 3e_1, H⊤H=IH^\top H = I, H−1=HH^{-1} = H, det⁡H=−1\det H = -1Hv=v−2v=−vHv = v - 2v = -v and Hw=wHw = w for every w⊥vw \perp v, so the eigenvalues are −1,1,1-1, 1, 1 and det⁡H=−1\det H = -1: a reflection, not a rotation. Lengths are kept: H(1,1,1)⊤=13(5,1,1)⊤H(1, 1, 1)^\top = \tfrac13(5, 1, 1)^\top, and both have length 3\sqrt3.

Problem 4

Apply Gram–Schmidt to a1=(3,4)⊤a_1 = (3, 4)^\top and a2=(2,11)⊤a_2 = (2, 11)^\top.

  1. ∥a1∥=5\|a_1\| = 5, so q1=15(3,4)⊤q_1 = \tfrac15(3, 4)^\topThe first vector only needs normalizing.
  2. q1⊤a2=6+445=10q_1^\top a_2 = \tfrac{6 + 44}{5} = 10The length of a2a_2 along q1q_1.
  3. u2=a2−10q1=(2,11)⊤−(6,8)⊤=(−4,3)⊤u_2 = a_2 - 10q_1 = (2, 11)^\top - (6, 8)^\top = (-4, 3)^\topRemove that component. Check: a1⊤u2=−12+12=0a_1^\top u_2 = -12 + 12 = 0.
  4. ∥u2∥=5\|u_2\| = 5, so q2=15(−4,3)⊤q_2 = \tfrac15(-4, 3)^\topNormalize what is left.
  5. q1=15(3,4)⊤q_1 = \tfrac15(3, 4)^\top, q2=15(−4,3)⊤q_2 = \tfrac15(-4, 3)^\topThe basis of Problem 2, and its coordinates of a2a_2 there, 1010 and 55, are exactly the q1⊤a2q_1^\top a_2 and ∥u2∥\|u_2\| found here: R=[51005]R = \begin{bmatrix}5&10\\0&5\end{bmatrix}.

Problem 5

Find the QR decomposition of A=[142522]A = \begin{bmatrix}1&4\\2&5\\2&2\end{bmatrix}.

  1. r11=∥a1∥=1+4+4=3r_{11} = \|a_1\| = \sqrt{1 + 4 + 4} = 3 and q1=13(1,2,2)⊤q_1 = \tfrac13(1, 2, 2)^\topThe first column of QQ is the first column of AA, normalized.
  2. r12=q1⊤a2=4+10+43=6r_{12} = q_1^\top a_2 = \tfrac{4 + 10 + 4}{3} = 6The entry above the diagonal in column 22.
  3. u2=a2−6q1=(4,5,2)⊤−(2,4,4)⊤=(2,1,−2)⊤u_2 = a_2 - 6q_1 = (4, 5, 2)^\top - (2, 4, 4)^\top = (2, 1, -2)^\topCheck: q1⊤u2=13(2+2−4)=0q_1^\top u_2 = \tfrac13(2 + 2 - 4) = 0.
  4. r22=∥u2∥=3r_{22} = \|u_2\| = 3 and q2=13(2,1,−2)⊤q_2 = \tfrac13(2, 1, -2)^\topThe diagonal entries of RR are the lengths of the uu's, so they are positive.
  5. Q=13[12212−2]Q = \tfrac13\begin{bmatrix}1&2\\2&1\\2&-2\end{bmatrix}, R=[3603]R = \begin{bmatrix}3&6\\0&3\end{bmatrix}Check QRQR: column 11 is 3q1=(1,2,2)⊤3q_1 = (1, 2, 2)^\top and column 22 is 6q1+3q2=(2,4,4)⊤+(2,1,−2)⊤=(4,5,2)⊤6q_1 + 3q_2 = (2, 4, 4)^\top + (2, 1, -2)^\top = (4, 5, 2)^\top. And R=Q⊤AR = Q^\top A, as it must.

Problem 6

Extend Problem 5 with a third column: find the QR decomposition of A=[145251221]A = \begin{bmatrix}1&4&5\\2&5&1\\2&2&1\end{bmatrix}, and use it to find det⁡A\det A.

  1. q1=13(1,2,2)⊤q_1 = \tfrac13(1, 2, 2)^\top, q2=13(2,1,−2)⊤q_2 = \tfrac13(2, 1, -2)^\top, r11=3r_{11} = 3, r12=6r_{12} = 6, r22=3r_{22} = 3The first two columns are Problem 5 unchanged: Gram–Schmidt never revisits earlier columns.
  2. r13=q1⊤a3=5+2+23=3r_{13} = q_1^\top a_3 = \tfrac{5 + 2 + 2}{3} = 3 and r23=q2⊤a3=10+1−23=3r_{23} = q_2^\top a_3 = \tfrac{10 + 1 - 2}{3} = 3Project a3=(5,1,1)⊤a_3 = (5, 1, 1)^\top onto each finished qq.
  3. u3=a3−3q1−3q2=(5,1,1)⊤−(1,2,2)⊤−(2,1,−2)⊤=(2,−2,1)⊤u_3 = a_3 - 3q_1 - 3q_2 = (5, 1, 1)^\top - (1, 2, 2)^\top - (2, 1, -2)^\top = (2, -2, 1)^\topCheck: q1⊤u3=13(2−4+2)=0q_1^\top u_3 = \tfrac13(2 - 4 + 2) = 0 and q2⊤u3=13(4−2−2)=0q_2^\top u_3 = \tfrac13(4 - 2 - 2) = 0.
  4. r33=3r_{33} = 3, q3=13(2,−2,1)⊤q_3 = \tfrac13(2, -2, 1)^\top, so Q=13[12221−22−21]Q = \tfrac13\begin{bmatrix}1&2&2\\2&1&-2\\2&-2&1\end{bmatrix}The same matrix as the reflection HH of Problem 3, so det⁡Q=−1\det Q = -1.
  5. R=[363033003]R = \begin{bmatrix}3&6&3\\0&3&3\\0&0&3\end{bmatrix} and det⁡A=det⁡Q det⁡R=(−1)(27)=−27\det A = \det Q\,\det R = (-1)(27) = -27RR is triangular, so its determinant is 3⋅3⋅33\cdot3\cdot3. The sign comes from QQ: ∣det⁡A∣\lvert\det A\rvert is always the product of the rjjr_{jj}, the volume of the box the columns span.

Problem 7

Use the QR decomposition of Problem 5 to find the least-squares solution of Ax=bAx = b for A=[142522]A = \begin{bmatrix}1&4\\2&5\\2&2\end{bmatrix} and b=(3,3,6)⊤b = (3, 3, 6)^\top, and the residual.

  1. A⊤Ax^=A⊤bA^\top A\hat x = A^\top b becomes R⊤Q⊤QRx^=R⊤Q⊤bR^\top Q^\top QR\hat x = R^\top Q^\top b, so Rx^=Q⊤bR\hat x = Q^\top bQ⊤Q=IQ^\top Q = I, and R⊤R^\top is invertible because RR's diagonal is positive.
  2. Q⊤b=(3+6+123, 6+3−123)⊤=(7,−1)⊤Q^\top b = \Big(\tfrac{3 + 6 + 12}{3},\ \tfrac{6 + 3 - 12}{3}\Big)^\top = (7, -1)^\topOne dot product with each qq.
  3. 3x^2=−13\hat x_2 = -1 and 3x^1+6x^2=73\hat x_1 + 6\hat x_2 = 7RR is upper triangular: back substitution, no elimination needed.
  4. x^2=−13\hat x_2 = -\tfrac13 and x^1=7+23=3\hat x_1 = \tfrac{7 + 2}{3} = 3From the bottom row up.
  5. x^=(3,−13)⊤\hat x = \big(3, -\tfrac13\big)^\top, Ax^=13(5,13,16)⊤A\hat x = \tfrac13(5, 13, 16)^\top, residual r=23(2,−2,1)⊤r = \tfrac23(2, -2, 1)^\top with ∥r∥=2\|r\| = 2Ax^=7q1−q2A\hat x = 7q_1 - q_2. The residual is a multiple of q3q_3 from Problem 6, orthogonal to both columns: a1⊤r=23(2−4+2)=0a_1^\top r = \tfrac23(2 - 4 + 2) = 0 and a2⊤r=23(8−10+2)=0a_2^\top r = \tfrac23(8 - 10 + 2) = 0.

Problem 8

For the tall QQ of Problem 5, find P=QQ⊤P = QQ^\top and show it is the projection onto the plane spanned by the columns of AA. Find the orthogonal complement of that plane, and show QQ⊤≠IQQ^\top \ne I although Q⊤Q=IQ^\top Q = I.

  1. Q⊤Q=19[9009]=I2Q^\top Q = \tfrac19\begin{bmatrix}9&0\\0&9\end{bmatrix} = I_2The columns are orthonormal.
  2. P=q1q1⊤+q2q2⊤=19[54−2452−228]P = q_1q_1^\top + q_2q_2^\top = \tfrac19\begin{bmatrix}5&4&-2\\4&5&2\\-2&2&8\end{bmatrix}QQ⊤QQ^\top is the sum of the projections onto each qq; for example, its top-left entry is 1+49\tfrac{1 + 4}{9}.
  3. P2=Q(Q⊤Q)Q⊤=QQ⊤=PP^2 = Q(Q^\top Q)Q^\top = QQ^\top = P and Pa1=a1Pa_1 = a_1, Pa2=a2Pa_2 = a_2Projecting twice is projecting once, and vectors in the plane are left alone.
  4. The complement is spanned by (2,−2,1)⊤(2, -2, 1)^\top: it is orthogonal to (1,2,2)(1, 2, 2) and (4,5,2)(4, 5, 2), and P(2,−2,1)⊤=0P(2, -2, 1)^\top = 0Gram–Schmidt on a third vector gave it in Problem 6. So I−P=q3q3⊤I - P = q_3q_3^\top, the projection onto the complement.
  5. P=19[54−2452−228]P = \tfrac19\begin{bmatrix}5&4&-2\\4&5&2\\-2&2&8\end{bmatrix}; complement spanned by (2,−2,1)⊤(2, -2, 1)^\top; QQ⊤QQ^\top has trace 22, so it is not I3I_3The trace of a projection is the dimension it projects onto: 5+5+89=2\tfrac{5 + 5 + 8}{9} = 2, not 33. Only a square QQ has QQ⊤=IQQ^\top = I.

Problem 9

Run Gram–Schmidt on a1=(1,2,2)⊤a_1 = (1, 2, 2)^\top, a2=(4,5,2)⊤a_2 = (4, 5, 2)^\top, a3=(6,9,6)⊤a_3 = (6, 9, 6)^\top. What happens at the third vector, and what does it say about A=[a1 a2 a3]A = [a_1\ a_2\ a_3]?

  1. q1=13(1,2,2)⊤q_1 = \tfrac13(1, 2, 2)^\top and q2=13(2,1,−2)⊤q_2 = \tfrac13(2, 1, -2)^\topAs in Problem 5.
  2. q1⊤a3=6+18+123=12q_1^\top a_3 = \tfrac{6 + 18 + 12}{3} = 12 and q2⊤a3=12+9−123=3q_2^\top a_3 = \tfrac{12 + 9 - 12}{3} = 3The two projection coefficients.
  3. u3=(6,9,6)⊤−(4,8,8)⊤−(2,1,−2)⊤=(0,0,0)⊤u_3 = (6, 9, 6)^\top - (4, 8, 8)^\top - (2, 1, -2)^\top = (0, 0, 0)^\topNothing is left after removing the components along q1q_1 and q2q_2: a3a_3 lies in their plane.
  4. a3=12q1+3q2=4a1+(a2−2a1)=2a1+a2a_3 = 12q_1 + 3q_2 = 4a_1 + (a_2 - 2a_1) = 2a_1 + a_2Rewrite the qq's in terms of the aa's with Problem 5: 3q1=a13q_1 = a_1 and 3q2=a2−2a13q_2 = a_2 - 2a_1. Check: 2(1,2,2)+(4,5,2)=(6,9,6)2(1, 2, 2) + (4, 5, 2) = (6, 9, 6).
  5. u3=0u_3 = 0: a3=2a1+a2a_3 = 2a_1 + a_2, the columns are dependent, AA has rank 22 and det⁡A=0\det A = 0There is no q3q_3 to make, and r33r_{33} would be 00. In a data matrix this is a feature that is a combination of two others.

Problem 10

Two features are almost collinear:

A=[11ε00ε],ε=10−8.A = \begin{bmatrix}1&1\\ \varepsilon&0\\ 0&\varepsilon\end{bmatrix},\qquad \varepsilon = 10^{-8}.

Show that κ(A⊤A)=κ(A)2\kappa(A^\top A) = \kappa(A)^2 in general, find both for this AA, and explain why forming A⊤AA^\top A in double precision fails while QR on AA does not.

  1. A=UΣV⊤A = U\Sigma V^\top gives A⊤A=VΣ2V⊤A^\top A = V\Sigma^2V^\top, so the singular values of A⊤AA^\top A are σi2\sigma_i^2U⊤U=IU^\top U = I cancels in the middle.
  2. κ(A⊤A)=σmax⁡2/σmin⁡2=κ(A)2\kappa(A^\top A) = \sigma_{\max}^2/\sigma_{\min}^2 = \kappa(A)^2Forming A⊤AA^\top A squares the condition number before any solving starts.
  3. A⊤A=[1+ε2111+ε2]A^\top A = \begin{bmatrix}1 + \varepsilon^2&1\\1&1 + \varepsilon^2\end{bmatrix} with eigenvalues 2+ε22 + \varepsilon^2 and ε2\varepsilon^2Eigenvectors (1,1)⊤(1, 1)^\top and (1,−1)⊤(1, -1)^\top. So σ1(A)=2+ε2\sigma_1(A) = \sqrt{2 + \varepsilon^2} and σ2(A)=ε\sigma_2(A) = \varepsilon.
  4. κ(A)=2+ε2/ε≈1.41×108\kappa(A) = \sqrt{2 + \varepsilon^2}/\varepsilon \approx 1.41\times10^{8} and κ(A⊤A)≈2×1016\kappa(A^\top A) \approx 2\times10^{16}1+ε2=1+10−161 + \varepsilon^2 = 1 + 10^{-16} rounds to 11 in double precision (the spacing of doubles near 11 is about 2.2×10−162.2\times10^{-16}), so the computed A⊤AA^\top A is exactly [1111]\begin{bmatrix}1&1\\1&1\end{bmatrix}, which is singular.
  5. κ(A)≈1.41×108\kappa(A) \approx 1.41\times10^8, κ(A⊤A)≈2×1016\kappa(A^\top A) \approx 2\times10^{16}: the normal equations lose every digit, while QR works on AA itself, with condition number κ(R)=κ(A)\kappa(R) = \kappa(A)For b=A(2,1)⊤b = A(2, 1)^\top, QR returns (2,1)(2, 1) to at least eight digits, and r22≈2 εr_{22} \approx \sqrt2\,\varepsilon is small but not lost. This is why least-squares solvers use QR or the SVD, and why nearly collinear features make the normal equations unreliable long before they are exactly singular.

Where this goes wrong

1. Projection coefficient not divided by a·a

The projection onto a1a_1 is remembered as "dot product times a1a_1", and a1a_1 is not a unit vector.

  1. a1=(3,4)⊤a_1 = (3, 4)^\top, a2=(2,11)⊤a_2 = (2, 11)^\top, a1⊤a2=50a_1^\top a_2 = 50Right so far: Problem 4.
  2. “Subtract the dot product times a1a_1.”The step that causes the mistake: (a1⊤a2) a1(a_1^\top a_2)\,a_1 is the projection only when ∥a1∥=1\|a_1\| = 1.
  3. u2=(2,11)⊤−50(3,4)⊤=(−148,−189)⊤u_2 = (2, 11)^\top - 50(3, 4)^\top = (-148, -189)^\topCheck: a1⊤u2=−444−756=−1200a_1^\top u_2 = -444 - 756 = -1200, not 00. The coefficient is a1⊤a2a1⊤a1=5025=2\tfrac{a_1^\top a_2}{a_1^\top a_1} = \tfrac{50}{25} = 2, giving u2=(2,11)⊤−(6,8)⊤=(−4,3)⊤u_2 = (2, 11)^\top - (6, 8)^\top = (-4, 3)^\top. Dividing by ∥a1∥=5\|a_1\| = 5 instead of ∥a1∥2\|a_1\|^2 fails too: (2,11)⊤−10(3,4)⊤=(−28,−29)⊤(2, 11)^\top - 10(3, 4)^\top = (-28, -29)^\top, with a1⊤u2=−200a_1^\top u_2 = -200.

2. Projecting onto a column of A instead of q

The third step subtracts projections onto the earlier vectors, and the earlier columns of AA are the nearest to hand.

  1. a1=(1,2,2)⊤a_1 = (1, 2, 2)^\top, a2=(4,5,2)⊤a_2 = (4, 5, 2)^\top, a3=(5,1,1)⊤a_3 = (5, 1, 1)^\topRight so far: Problem 6.
  2. “Subtract the projections of a3a_3 onto a1a_1 and onto a2a_2.”The step that causes the mistake: a1a_1 and a2a_2 are not orthogonal (a1⊤a2=18a_1^\top a_2 = 18), so their projections overlap.
  3. u3=a3−99a1−2745a2=(85,−4,−115)⊤u_3 = a_3 - \tfrac99a_1 - \tfrac{27}{45}a_2 = \big(\tfrac85, -4, -\tfrac{11}5\big)^\topCheck: a1⊤u3=85−8−225=−545a_1^\top u_3 = \tfrac85 - 8 - \tfrac{22}5 = -\tfrac{54}5, not 00. The part of a3a_3 along the shared direction is removed twice. Project onto q1q_1 and q2q_2, which are orthogonal: u3=a3−3q1−3q2=(2,−2,1)⊤u_3 = a_3 - 3q_1 - 3q_2 = (2, -2, 1)^\top.

3. R computed as AᵀQ

RR is built from dot products of qq's with aa's, and the order of the transpose is easy to swap.

  1. Q=13[12212−2]Q = \tfrac13\begin{bmatrix}1&2\\2&1\\2&-2\end{bmatrix} for A=[142522]A = \begin{bmatrix}1&4\\2&5\\2&2\end{bmatrix}Right so far: Problem 5.
  2. “RR holds the dot products of the columns, so R=A⊤QR = A^\top Q.”The step that causes the mistake: A⊤Q=(Q⊤A)⊤A^\top Q = (Q^\top A)^\top is RR transposed.
  3. R=[3063]R = \begin{bmatrix}3&0\\6&3\end{bmatrix}It is lower triangular, which is already the warning. Multiplying back, the first column of QRQR is 3q1+6q2=(5,4,−2)⊤3q_1 + 6q_2 = (5, 4, -2)^\top, not a1=(1,2,2)⊤a_1 = (1, 2, 2)^\top. The right order is R=Q⊤A=[3603]R = Q^\top A = \begin{bmatrix}3&6\\0&3\end{bmatrix}: row ii is qiq_i dotted with each column of AA.

4. QQᵀ taken to be I for a tall Q

Q⊤Q=IQ^\top Q = I, and the identity is expected to work in the other order too.

  1. Q⊤Q=I2Q^\top Q = I_2 for the 3×23\times2 matrix QQ of Problem 5Right so far.
  2. “So Q−1=Q⊤Q^{-1} = Q^\top, and QQ⊤=IQQ^\top = I.”The step that causes the mistake: QQ is not square, so it has no inverse, and Q⊤Q^\top is only a left inverse.
  3. Ax^=QQ⊤b=bA\hat x = QQ^\top b = b, so the least-squares fit of Problem 7 is exact and the residual is 00QQ⊤b=7q1−q2=13(5,13,16)⊤QQ^\top b = 7q_1 - q_2 = \tfrac13(5, 13, 16)^\top, not b=(3,3,6)⊤b = (3, 3, 6)^\top: QQ⊤QQ^\top projects onto a plane, with trace 22. The residual is 23(2,−2,1)⊤\tfrac23(2, -2, 1)^\top, of length 22.

5. Normalizing a zero vector

Every Gram–Schmidt step ends with a division by a length, and it is done without looking.

  1. u3=a3−12q1−3q2u_3 = a_3 - 12q_1 - 3q_2 for a3=(6,9,6)⊤a_3 = (6, 9, 6)^\topRight so far: Problem 9.
  2. With measured data or inexact arithmetic, a dependent column leaves a u3u_3 of size around 10−1510^{-15} rather than exactly 00Rounding leaves crumbs; these small integers happen to cancel exactly.
  3. q3=u3/∥u3∥q_3 = u_3/\|u_3\| is a unit vector, so the columns are independent and AA has rank 33The direction of u3u_3 is rounding noise, and dividing by its tiny length blows the noise up to length 11. Compare ∥u3∥\|u_3\| with ∥a3∥=153≈12.4\|a_3\| = \sqrt{153} \approx 12.4 before normalizing: a ratio near machine precision means a3a_3 is a combination of the earlier columns, here a3=2a1+a2a_3 = 2a_1 + a_2, and the rank is 22.

Print this set: gram-schmidt-and-qr.pdf (problems, answers, and worked solutions on separate pages).

Read the guide: Gram–Schmidt and the QR decomposition: subtract the projections, normalize, then R = QᵀA