Tutorials › Linear Algebra › Why QR Solves Least-Squares Problems

Least Squares and QR Factorization · Tutorial 701 of 1000

Why QR Solves Least-Squares Problems

See how orthogonal decomposition proves that the least-squares coefficients are found by solving \(R\mathbf{x}=Q^{\top}\mathbf{b}\).

Intermediate 10 min read

What You'll Learn

  • Decompose the data vector into a part in the column space of \(\mathbf{A}\) and an orthogonal residual.
  • Prove the least-squares criterion for a reduced QR factorization.
  • Explain why a nonsingular triangular factor gives a unique minimizer.
  • Distinguish the triangular solve for full column rank from the system in rank-deficient QR.
  • Check QR least-squares calculations by substitution and residual orthogonality.

Why Orthogonality Simplifies Least Squares

In a least-squares problem, the vector \(\mathbf{b}\) need not equal \(\mathbf{A}\mathbf{x}\) for any choice of \(\mathbf{x}\). Instead, we choose \(\mathbf{x}\) to make the residual \(\mathbf{b}-\mathbf{A}\mathbf{x}\) as short as possible. A QR factorization makes this minimization transparent: it separates the part of \(\mathbf{b}\) that can be represented by the columns of \(\mathbf{A}\) from the part that cannot.

Work over \(\mathbb{R}\). The norm is the Euclidean norm, so \(\|\mathbf{v}\|^2=\mathbf{v}^{\top}\mathbf{v}\), and two vectors are orthogonal when their dot product is zero. Suppose \(\mathbf{A}\in\mathbb{R}^{m\times n}\) has full column rank, with \(m\ge n\), and has a reduced QR factorization \(\mathbf{A}=\mathbf{Q}\mathbf{R}\). Here \(\mathbf{Q}\in\mathbb{R}^{m\times n}\) has orthonormal columns and \(\mathbf{R}\in\mathbb{R}^{n\times n}\) is nonsingular and upper triangular. Orthonormality means that the columns have length one and are pairwise orthogonal; in matrix form, \(\mathbf{Q}^{\top}\mathbf{Q}=\mathbf{I}_n\).

Definition: A least-squares solution of \(\mathbf{A}\mathbf{x}\approx\mathbf{b}\) is a vector \(\widehat{\mathbf{x}}\) that minimizes \(\|\mathbf{A}\mathbf{x}-\mathbf{b}\|\) over all \(\mathbf{x}\in\mathbb{R}^n\). A residual is \(\mathbf{r}=\mathbf{b}-\mathbf{A}\widehat{\mathbf{x}}\). The column space \(\operatorname{Col}\mathbf{A}\) is the set of all linear combinations of the columns of \(\mathbf{A}\).

The columns of \(\mathbf{A}=\mathbf{Q}\mathbf{R}\) are combinations of the columns of \(\mathbf{Q}\). When \(\mathbf{R}\) is nonsingular, both matrices have the same column space. Thus the component of \(\mathbf{b}\) in \(\operatorname{Col}\mathbf{A}\) can be written using \(\mathbf{Q}\), while the leftover component is orthogonal to that space. The key is that changing \(\mathbf{x}\) changes only the first component; it cannot remove the orthogonal leftover.

The Orthogonal Decomposition Behind the QR Solve

For any \(\mathbf{b}\in\mathbb{R}^m\), define \(\mathbf{y}=\mathbf{Q}^{\top}\mathbf{b}\) and \(\mathbf{z}=\mathbf{b}-\mathbf{Q}\mathbf{y}\). Then \(\mathbf{b}=\mathbf{Q}\mathbf{y}+\mathbf{z}\). Moreover,

$$ \mathbf{Q}^{\top}\mathbf{z} =\mathbf{Q}^{\top}\mathbf{b}-\mathbf{Q}^{\top}\mathbf{Q}\mathbf{y} =\mathbf{Q}^{\top}\mathbf{b}-\mathbf{y} =\mathbf{0}. $$

So \(\mathbf{z}\) is orthogonal to every column of \(\mathbf{Q}\), and hence to every vector in \(\operatorname{Col}\mathbf{Q}\). For any \(\mathbf{x}\), the residual can now be split into two orthogonal parts:

$$ \mathbf{b}-\mathbf{A}\mathbf{x} =\mathbf{b}-\mathbf{Q}\mathbf{R}\mathbf{x} =\mathbf{z}+\mathbf{Q}(\mathbf{y}-\mathbf{R}\mathbf{x}). $$

The second term lies in \(\operatorname{Col}\mathbf{Q}\); the first is orthogonal to that space. For orthogonal vectors \(\mathbf{u}\) and \(\mathbf{v}\), expanding the dot product gives \(\|\mathbf{u}+\mathbf{v}\|^2=\|\mathbf{u}\|^2+\|\mathbf{v}\|^2\), because the two cross terms are zero. Also, \(\|\mathbf{Q}\mathbf{c}\|^2=\mathbf{c}^{\top}\mathbf{Q}^{\top}\mathbf{Q}\mathbf{c}=\|\mathbf{c}\|^2\). Together these facts give the exact objective:

$$ \|\mathbf{b}-\mathbf{A}\mathbf{x}\|^2 =\|\mathbf{z}\|^2+\|\mathbf{y}-\mathbf{R}\mathbf{x}\|^2. $$
Theorem (QR least-squares criterion): Let \(\mathbf{A}=\mathbf{Q}\mathbf{R}\) be a reduced QR factorization in \(\mathbb{R}^{m\times n}\), where \(\mathbf{Q}\) has orthonormal columns and \(\mathbf{R}\) is nonsingular and upper triangular. For every \(\mathbf{b}\in\mathbb{R}^m\), the unique least-squares solution is the solution of \(\mathbf{R}\widehat{\mathbf{x}}=\mathbf{Q}^{\top}\mathbf{b}\). The minimum residual is \(\mathbf{b}-\mathbf{Q}\mathbf{Q}^{\top}\mathbf{b}\).

Proof. Put \(\mathbf{y}=\mathbf{Q}^{\top}\mathbf{b}\) and \(\mathbf{z}=\mathbf{b}-\mathbf{Q}\mathbf{y}\). The calculation above proves that \(\mathbf{z}\perp\operatorname{Col}\mathbf{Q}\) and that, for every \(\mathbf{x}\), \(\|\mathbf{b}-\mathbf{A}\mathbf{x}\|^2=\|\mathbf{z}\|^2+\|\mathbf{y}-\mathbf{R}\mathbf{x}\|^2\). The second summand is nonnegative, so the objective is at least \(\|\mathbf{z}\|^2\). Since \(\mathbf{R}\) is nonsingular, the system \(\mathbf{R}\widehat{\mathbf{x}}=\mathbf{y}\) has exactly one solution. At that solution the second summand is zero, so the lower bound is attained. Any minimizer must make that summand zero, which forces \(\mathbf{R}\mathbf{x}=\mathbf{y}\), and thus must equal \(\widehat{\mathbf{x}}\). The residual there is \(\mathbf{z}=\mathbf{b}-\mathbf{Q}\mathbf{Q}^{\top}\mathbf{b}\). \(\square\)

Because \(\mathbf{R}\) is upper triangular, the equation can be solved by back substitution, starting from its last row. The QR argument does not claim that the original equations \(\mathbf{A}\mathbf{x}=\mathbf{b}\) have an exact solution. It proves instead that matching the \(\mathbf{Q}\)-coordinates of \(\mathbf{b}\) removes every part of the residual that lies in the column space, leaving only the orthogonal component.

Worked QR Least-Squares Calculations

Worked Example: A Full-Rank Triangular Solve

Let

$$ \mathbf{Q}=\begin{bmatrix}1&0\\0&1\\0&0\end{bmatrix}, \qquad \mathbf{R}=\begin{bmatrix}2&1\\0&3\end{bmatrix}, \qquad \mathbf{b}=\begin{bmatrix}5\\6\\4\end{bmatrix}. $$

The columns of \(\mathbf{Q}\) are orthonormal, and multiplying gives

$$ \mathbf{A}=\mathbf{Q}\mathbf{R} =\begin{bmatrix}2&1\\0&3\\0&0\end{bmatrix}. $$

Compute the right-hand side of the triangular system:

$$ \mathbf{Q}^{\top}\mathbf{b} =\begin{bmatrix}1&0&0\\0&1&0\end{bmatrix} \begin{bmatrix}5\\6\\4\end{bmatrix} =\begin{bmatrix}5\\6\end{bmatrix}. $$

Now solve \(\mathbf{R}\mathbf{x}=\mathbf{Q}^{\top}\mathbf{b}\). The second row gives \(3x_2=6\), so \(x_2=2\). The first row gives \(2x_1+x_2=5\), so \(2x_1+2=5\) and \(x_1=\tfrac32\). Therefore \(\widehat{\mathbf{x}}=(\tfrac32,2)^{\top}\). Check the multiplication:

$$ \mathbf{A}\widehat{\mathbf{x}} =\begin{bmatrix}2&1\\0&3\\0&0\end{bmatrix} \begin{bmatrix}\tfrac32\\2\end{bmatrix} =\begin{bmatrix}5\\6\\0\end{bmatrix}, \qquad \mathbf{r}=\mathbf{b}-\mathbf{A}\widehat{\mathbf{x}} =\begin{bmatrix}0\\0\\4\end{bmatrix}. $$

The residual is orthogonal to both columns of \(\mathbf{A}\), and its squared norm is \(16\). Also, \(\mathbf{b}-\mathbf{Q}\mathbf{Q}^{\top}\mathbf{b}=(0,0,4)^{\top}\), agreeing with the residual predicted by the theorem.

Worked Example: An Orthonormal Basis Not Made of Standard Coordinate Vectors

Take the columns of \(\mathbf{Q}\) to be \(\mathbf{q}_1=(1,1,0,0)^{\top}/\sqrt{2}\) and \(\mathbf{q}_2=(0,0,1,1)^{\top}/\sqrt{2}\), and let \(\mathbf{R}=\begin{bmatrix}2&1\\0&1\end{bmatrix}\). The columns of \(\mathbf{Q}\) each have squared length \(1\), and their dot product is \(0\). Choose \(\mathbf{b}=3\mathbf{q}_1+2\mathbf{q}_2+(1,-1,0,0)^{\top}\). The last vector is orthogonal to both columns of \(\mathbf{Q}\). In coordinates,

$$ \mathbf{b}=\begin{bmatrix}3/\sqrt{2}+1\\3/\sqrt{2}-1\\2/\sqrt{2}\\2/\sqrt{2}\end{bmatrix}. $$

Since the added vector is orthogonal to the columns, \(\mathbf{Q}^{\top}\mathbf{b}=(3,2)^{\top}\). Solve the triangular system: the second row gives \(x_2=2\), and the first gives \(2x_1+x_2=3\), hence \(x_1=\tfrac12\). Thus \(\widehat{\mathbf{x}}=(\tfrac12,2)^{\top}\). To check, \(\mathbf{R}\widehat{\mathbf{x}}=(3,2)^{\top}\), so \(\mathbf{A}\widehat{\mathbf{x}}=\mathbf{Q}(3,2)^{\top}=3\mathbf{q}_1+2\mathbf{q}_2\). Subtracting from \(\mathbf{b}\) leaves \((1,-1,0,0)^{\top}\), whose dot products with \(\mathbf{q}_1,\mathbf{q}_2\) are both zero. This verifies both the solve and the residual decomposition.

What Changes When the Columns Are Dependent?

A rank-deficient matrix cannot have a nonsingular square \(\mathbf{R}\) in a reduced factorization with \(n\) columns in \(\mathbf{Q}\). A rank-\(r\) factorization instead has \(\mathbf{Q}\in\mathbb{R}^{m\times r}\) with orthonormal columns and \(\mathbf{R}\in\mathbb{R}^{r\times n}\). In this case \(\mathbf{R}\) has full row rank, but may have more columns than rows. The same orthogonal decomposition proves that the least-squares minimizers are exactly the solutions of \(\mathbf{R}\mathbf{x}=\mathbf{Q}^{\top}\mathbf{b}\); the system may have multiple solutions rather than one triangular back-substitution solution.

Result: Suppose \(\mathbf{A}=\mathbf{Q}\mathbf{R}\), where the \(r\) columns of \(\mathbf{Q}\) are orthonormal, \(\operatorname{Col}\mathbf{A}=\operatorname{Col}\mathbf{Q}\), and \(\mathbf{R}\in\mathbb{R}^{r\times n}\). If \(\mathbf{R}\) has full row rank, then a vector \(\mathbf{x}\) is a least-squares solution exactly when \(\mathbf{R}\mathbf{x}=\mathbf{Q}^{\top}\mathbf{b}\).

Proof. Set \(\mathbf{y}=\mathbf{Q}^{\top}\mathbf{b}\) and \(\mathbf{z}=\mathbf{b}-\mathbf{Q}\mathbf{y}\). As before, \(\mathbf{Q}^{\top}\mathbf{z}=\mathbf{0}\), and for every \(\mathbf{x}\), \(\mathbf{b}-\mathbf{A}\mathbf{x}=\mathbf{z}+\mathbf{Q}(\mathbf{y}-\mathbf{R}\mathbf{x})\), a sum of orthogonal vectors. Therefore the squared residual norm is \(\|\mathbf{z}\|^2+\|\mathbf{y}-\mathbf{R}\mathbf{x}\|^2\). Full row rank means that the columns of \(\mathbf{R}\) span \(\mathbb{R}^r\), so the equation \(\mathbf{R}\mathbf{x}=\mathbf{y}\) has a solution. Every solution attains the lower bound \(\|\mathbf{z}\|^2\), and any minimizer must attain that bound, forcing \(\mathbf{R}\mathbf{x}=\mathbf{y}\). \(\square\)

Worked Example: A Rank-Deficient QR Factorization

Let \(\mathbf{q}=(1,0,1)^{\top}/\sqrt{2}\), \(\mathbf{Q}=\mathbf{q}\), \(\mathbf{R}=\begin{bmatrix}2&4\end{bmatrix}\), and \(\mathbf{b}=(3,5,1)^{\top}\). Then \(\mathbf{A}=\mathbf{Q}\mathbf{R}=\begin{bmatrix}\sqrt{2}&2\sqrt{2}\\0&0\\\sqrt{2}&2\sqrt{2}\end{bmatrix}\), whose second column is twice its first. The right-hand side for the reduced system is

$$ \mathbf{Q}^{\top}\mathbf{b} =\frac{1}{\sqrt{2}}(3+1) =2\sqrt{2}. $$

The equation \(2x_1+4x_2=2\sqrt{2}\) has many solutions. For example, choose \(x_1=\sqrt{2}\), \(x_2=0\). Then \(\mathbf{A}\mathbf{x}=(2,0,2)^{\top}\), and the residual is \((1,5,-1)^{\top}\). Its dot product with \(\mathbf{q}\) is \((1-1)/\sqrt{2}=0\), so it is orthogonal to \(\operatorname{Col}\mathbf{A}\). The minimum squared residual is \(1^2+5^2+(-1)^2=27\). The other solutions to the reduced equation also give the same fitted vector and residual.

Common Mistake: Requiring an Exact Fit

In the first worked example, a tempting but incorrect step is to solve \(\mathbf{A}\mathbf{x}=\mathbf{b}\) and conclude that the least-squares problem has no solution because the third row says \(0=4\). That calculation only proves that no exact fit exists. It does not rule out a vector that minimizes the residual. The QR calculation gives \(\widehat{\mathbf{x}}=(\tfrac32,2)^{\top}\), with residual \((0,0,4)^{\top}\); every other choice adds a nonzero component in the column space to this orthogonal residual, increasing the squared norm.

Another error is to replace \(\mathbf{Q}^{\top}\mathbf{b}\) with \(\mathbf{Q}\mathbf{b}\). The matrix \(\mathbf{Q}\) is generally rectangular, so \(\mathbf{Q}\mathbf{b}\) may not even be defined. The coordinate vector in the orthonormal columns is \(\mathbf{Q}^{\top}\mathbf{b}\); the corresponding vector in the original space is \(\mathbf{Q}\mathbf{Q}^{\top}\mathbf{b}\). Keeping those two roles separate makes the decomposition and dimensions consistent.

Check Your Understanding

Use the orthogonal decomposition, not an assumption that \(\mathbf{A}\mathbf{x}=\mathbf{b}\) has an exact solution.

  1. For a reduced QR factorization with full column rank, explain why \(\mathbf{b}-\mathbf{Q}\mathbf{Q}^{\top}\mathbf{b}\) is orthogonal to every column of \(\mathbf{Q}\).
  2. Let \(\mathbf{Q}=\begin{bmatrix}1&0\\0&1\\0&0\end{bmatrix}\), \(\mathbf{R}=\begin{bmatrix}1&2\\0&1\end{bmatrix}\), and \(\mathbf{b}=(4,3,5)^{\top}\). Solve the triangular system and compute the residual.
  3. Why does nonsingularity of \(\mathbf{R}\) guarantee that the full-column-rank QR least-squares solution is unique?
  4. In rank-deficient QR, what condition must \(\mathbf{x}\) satisfy to be a least-squares minimizer?
  5. Explain why solving the least-squares problem is not the same as requiring \(\mathbf{A}\mathbf{x}=\mathbf{b}\).

Test yourself: Linear Algebra final exam practice

Ready to check what you know? Work a full-length practice final, then open the complete worked solutions.

Want all 1,114 practice problems?

This tutorial covers one topic. Calc Pass covers the full Calculus AB & BC curriculum (the core of Calculus I and II): full review, every practice question, and step-by-step solutions to each one.

QR code to download the Calc Pass app

Get the Calc Pass App

Scan to download and practice on the go, with instant scoring.

Calc Pass Volume 1: 1,114 Practice Questions book cover

Vol. 1: 1,114 Questions

The complete practice question bank, organized by AB & BC topic.

View on Amazon
Calc Pass Volume 2: Solutions Manual book cover

Vol. 2: Solutions Manual

Fully worked solutions to every question in Volume 1.

View on Amazon