Unit 7, lesson 35

Least Squares with QR

How a reduced QR factorization of a design matrix with independent columns reduces least squares to a triangular equation, what the orthonormal coordinates of the data are, and how the squared error splits.

  • Video 35
  • Least-Squares Fitting

Can the least-squares line for the points (0,1)(0, 1), (1,2)(1, 2) and (2,2)(2, 2) be found without forming the matrix X⊤XX^{\top}X of the normal equations? A reduced QR factorization of the design matrix gives the model’s column space perpendicular unit directions, and the least-squares problem then shrinks to one triangular system solved from the bottom up. This lesson derives that system, carries out a complete fit with it, and keeps track of the part of the data the triangular system never sees, the residual that the model cannot explain.

Key terms

Least squares by QR factorization

Definition Let XX be a real m×nm \times n matrix with linearly independent columns, so m≥nm \ge n, and let y\mathbf{y} be a vector in Rm\mathbb{R}^m. The reduced QR factorization X=QRX = QR has an m×nm \times n matrix QQ with orthonormal columns, so Q⊤Q=InQ^{\top}Q = I_n, whose columns span Col⁡X\operatorname{Col} X, and an n×nn \times n upper triangular matrix RR with positive diagonal entries, which is therefore invertible. The method has three steps. Compute QQ and RR, for example by Gram-Schmidt. Compute the vector Q⊤yQ^{\top}\mathbf{y}. Solve the triangular system Rc=Q⊤yR\mathbf{c} = Q^{\top}\mathbf{y} by back substitution. Its solution is the unique least-squares solution c^\hat{\mathbf{c}} of Xc=yX\mathbf{c} = \mathbf{y}. On this page a tuple written for a vector stands for a column.

In plain words QR replaces the possibly slanted directions of the design columns by perpendicular unit directions that span the same subspace. The data and the model stay the same, and only the coordinates used inside the column space change. In exact arithmetic this route gives the same answer as the normal equations. In floating point, forming X⊤XX^{\top}X can make rounding errors more harmful, and a QR route avoids forming it, although a small exact example cannot show which route is more accurate.

Example Fit the line a+bta + bt to the points (0,1)(0, 1), (1,2)(1, 2) and (2,2)(2, 2). The design columns are (1,1,1)(1, 1, 1) and (0,1,2)(0, 1, 2). The second column has dot product 3/3=33/\sqrt{3} = \sqrt{3} with the first unit vector, its projection is (1,1,1)(1, 1, 1), and the remainder (−1,0,1)(-1, 0, 1) has length 2\sqrt{2}. So Gram-Schmidt gives

q1=13[111],q2=12[−101],\mathbf{q}_1 = \frac{1}{\sqrt{3}}\begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix}, \qquad \mathbf{q}_2 = \frac{1}{\sqrt{2}}\begin{bmatrix} -1 \\ 0 \\ 1 \end{bmatrix},

and with Q=[q1q2]Q = \begin{bmatrix} \mathbf{q}_1 & \mathbf{q}_2 \end{bmatrix},

R=[3302].R = \begin{bmatrix} \sqrt{3} & \sqrt{3} \\ 0 & \sqrt{2} \end{bmatrix}.

As a check, 3 q1=(1,1,1)\sqrt{3}\,\mathbf{q}_1 = (1, 1, 1) and 3 q1+2 q2=(0,1,2)\sqrt{3}\,\mathbf{q}_1 + \sqrt{2}\,\mathbf{q}_2 = (0, 1, 2), the two columns of XX.

Orthonormal coordinates of the data

Definition Let the orthonormal columns q1,…,qn\mathbf{q}_1, \dots, \mathbf{q}_n of QQ span Col⁡X\operatorname{Col} X. The orthonormal coordinates of the data are the entries of

Q⊤y=[q1⋅y⋮qn⋅y].Q^{\top}\mathbf{y} = \begin{bmatrix} \mathbf{q}_1 \cdot \mathbf{y} \\ \vdots \\ \mathbf{q}_n \cdot \mathbf{y} \end{bmatrix}.

They are the coordinates of the fitted vector p\mathbf{p}, the orthogonal projection of y\mathbf{y} onto Col⁡X\operatorname{Col} X, in the orthonormal basis (q1,…,qn)(\mathbf{q}_1, \dots, \mathbf{q}_n), so

p=QQ⊤y=(q1⋅y) q1+⋯+(qn⋅y) qn.\begin{aligned} \mathbf{p} &= QQ^{\top}\mathbf{y} \\[0.4em] &= (\mathbf{q}_1 \cdot \mathbf{y})\,\mathbf{q}_1 + \cdots + (\mathbf{q}_n \cdot \mathbf{y})\,\mathbf{q}_n . \end{aligned}

They are coordinates along the columns of QQ, not along the design columns, so in general they differ from the model coefficients. Since p=Xc^=Q(Rc^)\mathbf{p} = X\hat{\mathbf{c}} = Q(R\hat{\mathbf{c}}) and the columns of QQ are independent, the two are related by Q⊤y=Rc^Q^{\top}\mathbf{y} = R\hat{\mathbf{c}}.

In plain words Each entry of Q⊤yQ^{\top}\mathbf{y} measures how far the data reach along one perpendicular unit direction of the model. Together the entries locate the best prediction inside the column space, but in those new directions, not in terms of intercept and slope.

Example For the three points, y=(1,2,2)\mathbf{y} = (1, 2, 2) and

q1⋅y=1+2+23=53,q2⋅y=−1+0+22=12.\begin{gathered} \mathbf{q}_1 \cdot \mathbf{y} = \frac{1 + 2 + 2}{\sqrt{3}} = \frac{5}{\sqrt{3}}, \\[0.4em] \mathbf{q}_2 \cdot \mathbf{y} = \frac{-1 + 0 + 2}{\sqrt{2}} = \frac{1}{\sqrt{2}}. \end{gathered}

Then p=53(1,1,1)+12(−1,0,1)=(76,53,136)\mathbf{p} = \tfrac53(1, 1, 1) + \tfrac12(-1, 0, 1) = (\tfrac76, \tfrac53, \tfrac{13}{6}), the same fitted vector that the normal equations give.

Triangular least-squares equation

Definition With X=QRX = QR as above, the triangular least-squares equation is

Rc=Q⊤y.R\mathbf{c} = Q^{\top}\mathbf{y}.

A vector c\mathbf{c} is a least-squares solution of Xc=yX\mathbf{c} = \mathbf{y} if and only if it solves this equation, and since RR is invertible there is exactly one. To derive it, note that the columns of QQ span Col⁡X\operatorname{Col} X, so the residual is orthogonal to Col⁡X\operatorname{Col} X exactly when Q⊤(y−QRc)=0Q^{\top}(\mathbf{y} - QR\mathbf{c}) = \mathbf{0}, and Q⊤Q=InQ^{\top}Q = I_n reduces this to Q⊤y−Rc=0Q^{\top}\mathbf{y} - R\mathbf{c} = \mathbf{0}. The normal equations lead to the same place, because X⊤X=R⊤RX^{\top}X = R^{\top}R and X⊤y=R⊤Q⊤yX^{\top}\mathbf{y} = R^{\top}Q^{\top}\mathbf{y}, and the invertible factor R⊤R^{\top} can be canceled. The equation does not claim QRc=yQR\mathbf{c} = \mathbf{y}, which has no solution when the data are inconsistent.

In plain words Multiplying by Q⊤Q^{\top} keeps only the part of the data that lies along the model’s column space. What remains is a small square triangular system, solved from the last row upward.

Example For the three points the equation reads

3 a+3 b=53,2 b=12.\begin{aligned} \sqrt{3}\,a + \sqrt{3}\,b &= \frac{5}{\sqrt{3}}, \\[0.4em] \sqrt{2}\,b &= \frac{1}{\sqrt{2}}. \end{aligned}

The last row gives b=12b = \tfrac12. Dividing the first row by 3\sqrt{3} gives a+b=53a + b = \tfrac53, so a=53−12=76a = \tfrac53 - \tfrac12 = \tfrac76. This is the line y=76+12ty = \tfrac76 + \tfrac12 t, the same answer the normal equations give, with fitted values 76\tfrac76, 53\tfrac53 and 136\tfrac{13}{6}.

Residual outside the model space

Definition The residual outside the model space is

y−QQ⊤y=(I−QQ⊤)y,\mathbf{y} - QQ^{\top}\mathbf{y} = (I - QQ^{\top})\mathbf{y},

the component of y\mathbf{y} orthogonal to Col⁡X\operatorname{Col} X. It equals the least-squares residual r=y−Xc^\mathbf{r} = \mathbf{y} - X\hat{\mathbf{c}}. Since Q⊤(I−QQ⊤)y=0Q^{\top}(I - QQ^{\top})\mathbf{y} = \mathbf{0}, neither Q⊤yQ^{\top}\mathbf{y} nor the triangular equation sees it. In particular, adding to y\mathbf{y} any vector w\mathbf{w} orthogonal to Col⁡X\operatorname{Col} X leaves c^\hat{\mathbf{c}} unchanged and changes the residual to r+w\mathbf{r} + \mathbf{w}.

In plain words Applying Q⊤Q^{\top} sets aside the part of the data that is perpendicular to every prediction the model can make. That part does not disappear. It is the residual, and solving the triangular system exactly does not make inconsistent data consistent.

Example For the three points the residual is (−16,13,−16)(-\tfrac16, \tfrac13, -\tfrac16). The vector w=(1,−2,1)\mathbf{w} = (1, -2, 1) is orthogonal to both design columns, since 1−2+1=01 - 2 + 1 = 0 and 0−2+2=00 - 2 + 2 = 0. Subtracting it from the data gives (0,4,1)(0, 4, 1). This leaves Q⊤yQ^{\top}\mathbf{y} and the fitted line y=76+12ty = \tfrac76 + \tfrac12 t unchanged, but the residual becomes r−w=(−76,73,−76)\mathbf{r} - \mathbf{w} = (-\tfrac76, \tfrac73, -\tfrac76), and the sum of squared errors grows from 16\tfrac16 to 496\tfrac{49}{6}.

Split of the squared error

Definition With X=QRX = QR as above, for every c\mathbf{c} in Rn\mathbb{R}^n,

∥Xc−y∥2=∥Rc−Q⊤y∥2+∥(I−QQ⊤)y∥2.\begin{aligned} \lVert X\mathbf{c} - \mathbf{y} \rVert^2 &= \lVert R\mathbf{c} - Q^{\top}\mathbf{y} \rVert^2 \\[0.4em] &\quad + \lVert (I - QQ^{\top})\mathbf{y} \rVert^2 . \end{aligned}

The reason is the decomposition

Xc−y=Q(Rc−Q⊤y)−(I−QQ⊤)y,\begin{aligned} X\mathbf{c} - \mathbf{y} &= Q(R\mathbf{c} - Q^{\top}\mathbf{y}) \\[0.4em] &\quad - (I - QQ^{\top})\mathbf{y}, \end{aligned}

in which the first term lies in Col⁡X\operatorname{Col} X and the second is orthogonal to it, together with ∥Qz∥=∥z∥\lVert Q\mathbf{z} \rVert = \lVert \mathbf{z} \rVert for every z\mathbf{z} in Rn\mathbb{R}^n, which holds because Q⊤Q=InQ^{\top}Q = I_n. The first term is zero exactly when Rc=Q⊤yR\mathbf{c} = Q^{\top}\mathbf{y}, and the second does not depend on c\mathbf{c}. So the smallest sum of squared errors is

∥(I−QQ⊤)y∥2=∥y∥2−∥Q⊤y∥2.\lVert (I - QQ^{\top})\mathbf{y} \rVert^2 = \lVert \mathbf{y} \rVert^2 - \lVert Q^{\top}\mathbf{y} \rVert^2 .

In plain words The error of any coefficient choice has two perpendicular parts. One measures how far RcR\mathbf{c} is from the orthonormal coordinates of the data, and solving the triangular equation removes it. The other is the residual outside the model space, which no coefficients can remove.

Example For the three points and the line y=1+ty = 1 + t, so c=(1,1)\mathbf{c} = (1, 1),

Rc−Q⊤y=[23−5/32−1/2]=[1/31/2],\begin{aligned} R\mathbf{c} - Q^{\top}\mathbf{y} &= \begin{bmatrix} 2\sqrt{3} - 5/\sqrt{3} \\ \sqrt{2} - 1/\sqrt{2} \end{bmatrix} \\[0.4em] &= \begin{bmatrix} 1/\sqrt{3} \\ 1/\sqrt{2} \end{bmatrix}, \end{aligned}

whose squared length is 13+12=56\tfrac13 + \tfrac12 = \tfrac56. The fixed term is ∥y∥2−∥Q⊤y∥2=9−253−12=16\lVert \mathbf{y} \rVert^2 - \lVert Q^{\top}\mathbf{y} \rVert^2 = 9 - \tfrac{25}{3} - \tfrac12 = \tfrac16. Their sum is 11, which matches the direct computation from the residuals 00, 00 and −1-1 of this line.

Check yourself

  1. A reduced QR factorization of a design matrix has RR with rows [31]\begin{bmatrix} 3 & 1 \end{bmatrix} and [02]\begin{bmatrix} 0 & 2 \end{bmatrix}, and the data give Q⊤y=(7,4)Q^{\top}\mathbf{y} = (7, 4). Find the least-squares coefficients.
Show the answer

The last row says 2c2=42c_2 = 4, so c2=2c_2 = 2. The first row says 3c1+c2=73c_1 + c_2 = 7, so 3c1=53c_1 = 5 and c1=53c_1 = \tfrac53. The coefficients are (53,2)(\tfrac53, 2). The numbers 77 and 44 are coordinates along the columns of QQ, not coefficients of the original design columns.

  1. Use QR to fit the line y=a+bty = a + bt to the points (−1,1)(-1, 1), (0,0)(0, 0) and (1,2)(1, 2), and find the smallest sum of squared errors.
Show the answer

The design columns (1,1,1)(1, 1, 1) and (−1,0,1)(-1, 0, 1) are already orthogonal, so Gram-Schmidt only normalizes them. That gives q1=(1,1,1)/3\mathbf{q}_1 = (1, 1, 1)/\sqrt{3}, q2=(−1,0,1)/2\mathbf{q}_2 = (-1, 0, 1)/\sqrt{2} and

R=[3002].R = \begin{bmatrix} \sqrt{3} & 0 \\ 0 & \sqrt{2} \end{bmatrix}.

With y=(1,0,2)\mathbf{y} = (1, 0, 2), the orthonormal coordinates are Q⊤y=(3/3,1/2)=(3,1/2)Q^{\top}\mathbf{y} = (3/\sqrt{3}, 1/\sqrt{2}) = (\sqrt{3}, 1/\sqrt{2}). The triangular system 3 a=3\sqrt{3}\,a = \sqrt{3}, 2 b=1/2\sqrt{2}\,b = 1/\sqrt{2} gives a=1a = 1 and b=12b = \tfrac12. The smallest sum of squared errors is ∥y∥2−∥Q⊤y∥2=5−3−12=32\lVert \mathbf{y} \rVert^2 - \lVert Q^{\top}\mathbf{y} \rVert^2 = 5 - 3 - \tfrac12 = \tfrac32. As a check, the fitted values are 12\tfrac12, 11, 32\tfrac32, the residual is (12,−1,12)(\tfrac12, -1, \tfrac12), and 14+1+14=32\tfrac14 + 1 + \tfrac14 = \tfrac32.

  1. A model has the design columns (1,1,1)(1, 1, 1) and (2,2,2)(2, 2, 2), and the data are y=(1,2,6)\mathbf{y} = (1, 2, 6). What goes wrong with the QR method, and what is the least-squares fit?
Show the answer

Gram-Schmidt gives q1=(1,1,1)/3\mathbf{q}_1 = (1, 1, 1)/\sqrt{3}. The projection of the second column onto q1\mathbf{q}_1 is 23 q1=(2,2,2)2\sqrt{3}\,\mathbf{q}_1 = (2, 2, 2), the whole column, so the remainder is 0\mathbf{0} and cannot be normalized. The columns are dependent, so there is no reduced QR factorization with two orthonormal columns and an invertible RR, and the triangular method does not apply as stated. Least squares still has an answer. The column space is the line spanned by (1,1,1)(1, 1, 1), and the projection of y\mathbf{y} onto it is (3,3,3)(3, 3, 3), since the mean of 11, 22 and 66 is 33. The fitted vector (3,3,3)(3, 3, 3) is unique, with residual (−2,−1,3)(-2, -1, 3) and sum of squared errors 1414. The coefficients are not unique, because every coefficient vector (c1,c2)(c_1, c_2) with c1+2c2=3c_1 + 2c_2 = 3 gives that fitted vector.

How this lesson connects