Tutorials › Linear Algebra › Polynomial Regression Through QR

Least Squares and QR Factorization · Tutorial 703 of 1000

Polynomial Regression Through QR

Use reduced QR to convert polynomial least-squares fitting into a triangular system, then check the fit and its residuals.

Intermediate 10 min read

What You'll Learn

  • Build a polynomial design matrix with columns ordered by powers of the input.
  • Identify when the polynomial design matrix has full column rank.
  • Use a reduced QR factorization to obtain the least-squares coefficients by back substitution.
  • Compute fitted values and residuals from the resulting polynomial.
  • Calculate and verify the residual sum of squares.
  • Recognize why the QR triangular solve is not the same as taking \(Q^{\top}b\) for the coefficients.

Polynomial Fits as Least-Squares Problems

Polynomial regression uses a polynomial to summarize measured data. Given input values \(x_1,\dots,x_m\) and responses \(y_1,\dots,y_m\), a degree-\(d\) model has the form \(p(x)=c_0+c_1x+\cdots+c_dx^d\). The coefficients enter linearly, even though the model uses powers of the input. This makes fitting the polynomial a least-squares problem in a design matrix, and reduced QR gives a way to solve it without forming \(\mathbf{A}^{\top}\mathbf{A}\).

Throughout, the data vector is \(\mathbf{b}=(y_1,\dots,y_m)^{\top}\in\mathbb{R}^m\). The fitted values are the entries of \(\mathbf{A}\mathbf{c}\), and the residual vector is \(\mathbf{r}=\mathbf{b}-\mathbf{A}\mathbf{c}\). The residual sum of squares is \(\|\mathbf{r}\|^2\), the sum of the squares of its entries.

Definition: For input values \(x_1,\dots,x_m\) and a polynomial model of degree at most \(d\), the polynomial design matrix is the \(m\times(d+1)\) matrix \(\mathbf{A}\) whose \(i\)th row is \((1,x_i,x_i^2,\dots,x_i^d)\). The coefficient vector is \(\mathbf{c}=(c_0,c_1,\dots,c_d)^{\top}\), so the model's fitted-value vector is \(\mathbf{A}\mathbf{c}\).

The order of the columns fixes the order of the coefficients: the first column corresponds to the constant term, the next to \(x\), and so on. A reduced QR factorization of a full-column-rank matrix \(\mathbf{A}\in\mathbb{R}^{m\times n}\) writes \(\mathbf{A}=\mathbf{Q}\mathbf{R}\), where \(\mathbf{Q}\in\mathbb{R}^{m\times n}\) has orthonormal columns and \(\mathbf{R}\in\mathbb{R}^{n\times n}\) is upper triangular with nonzero diagonal entries. In the polynomial setting, \(n=d+1\).

When the Polynomial Coefficients Are Unique

For a degree-\(d\) model, if there are at least \(d+1\) distinct input values, then the design matrix has independent columns. Indeed, if \(\mathbf{A}\mathbf{c}=\mathbf{0}\), the polynomial \(p(x)=c_0+c_1x+\cdots+c_dx^d\) vanishes at every input value. A nonzero polynomial of degree at most \(d\) has at most \(d\) distinct roots, so \(p\) must be the zero polynomial and all its coefficients are zero. Thus the columns are independent. In particular, the full-column-rank condition required for the reduced QR solve holds for these data.

For a full-column-rank design matrix, the QR method obtains the coefficient vector by solving \(\mathbf{R}\mathbf{c}=\mathbf{Q}^{\top}\mathbf{b}\). Because \(\mathbf{R}\) is upper triangular, this can be done by back substitution. The following result explains why this gives the least-squares fit, rather than merely a solution of a convenient equation.

Theorem (Least squares through reduced QR): Suppose \(\mathbf{A}\in\mathbb{R}^{m\times n}\) has independent columns and \(\mathbf{A}=\mathbf{Q}\mathbf{R}\) is a reduced QR factorization, with \(\mathbf{Q}^{\top}\mathbf{Q}=\mathbf{I}_n\) and \(\mathbf{R}\) invertible. Then the unique vector \(\mathbf{c}\) minimizing \(\|\mathbf{b}-\mathbf{A}\mathbf{c}\|^2\) is the solution of \(\mathbf{R}\mathbf{c}=\mathbf{Q}^{\top}\mathbf{b}\). Its fitted-value vector is \(\mathbf{Q}\mathbf{Q}^{\top}\mathbf{b}\), and its residual sum of squares is \(\|\mathbf{b}-\mathbf{Q}\mathbf{Q}^{\top}\mathbf{b}\|^2\).

Proof. Since \(\mathbf{Q}^{\top}\mathbf{Q}=\mathbf{I}_n\), for any \(\mathbf{z}\in\mathbb{R}^n\) we can write \(\mathbf{b}-\mathbf{A}\mathbf{z}=(\mathbf{b}-\mathbf{Q}\mathbf{Q}^{\top}\mathbf{b})+\mathbf{Q}(\mathbf{Q}^{\top}\mathbf{b}-\mathbf{R}\mathbf{z})\). The first summand is orthogonal to every column of \(\mathbf{Q}\), because \(\mathbf{Q}^{\top}(\mathbf{b}-\mathbf{Q}\mathbf{Q}^{\top}\mathbf{b})=\mathbf{Q}^{\top}\mathbf{b}-\mathbf{Q}^{\top}\mathbf{Q}\mathbf{Q}^{\top}\mathbf{b}=\mathbf{0}\). The second summand is a linear combination of those columns. By the Pythagorean identity, the squared residual therefore equals \(\|\mathbf{b}-\mathbf{Q}\mathbf{Q}^{\top}\mathbf{b}\|^2+\|\mathbf{Q}(\mathbf{Q}^{\top}\mathbf{b}-\mathbf{R}\mathbf{z})\|^2\). Orthonormal columns preserve the norm of coefficient vectors, so the second term is \(\|\mathbf{Q}^{\top}\mathbf{b}-\mathbf{R}\mathbf{z}\|^2\). It is nonnegative and is zero exactly when \(\mathbf{R}\mathbf{z}=\mathbf{Q}^{\top}\mathbf{b}\). Since \(\mathbf{R}\) is invertible, this equation has exactly one solution, which is the unique minimizer. Substituting that solution gives \(\mathbf{A}\mathbf{c}=\mathbf{Q}\mathbf{R}\mathbf{c}=\mathbf{Q}\mathbf{Q}^{\top}\mathbf{b}\); the first term in the decomposition is its residual, giving the stated residual sum of squares. \(\square\)

A Quadratic Fit by Reduced QR

Worked Example: A Quadratic Model with a Nonzero Residual

Fit a degree-two polynomial to the data with inputs \(-2,-1,1,2\) and responses \(2,-2,6,8\). The design matrix uses columns for \(1,x,x^2\), in that order:

$$ \mathbf{A}= \begin{bmatrix} 1&-2&4\\ 1&-1&1\\ 1&1&1\\ 1&2&4 \end{bmatrix}, \qquad \mathbf{b}= \begin{bmatrix}2\\-2\\6\\8\end{bmatrix}. $$

Take the following reduced QR factors:

$$ \mathbf{Q}= \begin{bmatrix} \frac12&-\frac{2}{\sqrt{10}}&\frac12\\ \frac12&-\frac{1}{\sqrt{10}}&-\frac12\\ \frac12&\frac{1}{\sqrt{10}}&-\frac12\\ \frac12&\frac{2}{\sqrt{10}}&\frac12 \end{bmatrix}, \qquad \mathbf{R}= \begin{bmatrix} 2&0&5\\ 0&\sqrt{10}&0\\ 0&0&3 \end{bmatrix}. $$

The columns of \(\mathbf{Q}\) have norm \(1\): their squared norms are respectively \(4(1/4)=1\), \((4+1+1+4)/10=1\), and \(4(1/4)=1\). Their pairwise dot products are \(0\): for columns 1 and 2 the numerator is \(-2-1+1+2=0\); for columns 1 and 3 the sum is \(1/2-1/2-1/2+1/2=0\); for columns 2 and 3 the numerator is \(-2+1-1+2=0\). To check the factorization, its columns multiply as follows:

$$ 2\mathbf{q}_1= \begin{bmatrix}1\\1\\1\\1\end{bmatrix}, \qquad \sqrt{10}\mathbf{q}_2= \begin{bmatrix}-2\\-1\\1\\2\end{bmatrix}, \qquad 5\mathbf{q}_1+3\mathbf{q}_3= \begin{bmatrix}4\\1\\1\\4\end{bmatrix}. $$

These are exactly the columns of \(\mathbf{A}\), so \(\mathbf{A}=\mathbf{Q}\mathbf{R}\). Next compute the right-hand side of the triangular system:

$$ \mathbf{Q}^{\top}\mathbf{b} = \begin{bmatrix} (2-2+6+8)/2\\ (-4+2+6+16)/\sqrt{10}\\ (2+2-6+8)/2 \end{bmatrix} = \begin{bmatrix}7\\2\sqrt{10}\\3\end{bmatrix}. $$

Back substitution in \(\mathbf{R}\mathbf{c}=\mathbf{Q}^{\top}\mathbf{b}\) gives \(3c_2=3\), hence \(c_2=1\); then \(\sqrt{10}c_1=2\sqrt{10}\), hence \(c_1=2\); and \(2c_0+5c_2=7\), hence \(2c_0+5=7\) and \(c_0=1\). Thus the fitted polynomial is \(p(x)=1+2x+x^2\). Multiplication back checks the coefficients and fitted values:

$$ \mathbf{A}\mathbf{c} = \begin{bmatrix} 1-4+4\\ 1-2+1\\ 1+2+1\\ 1+4+4 \end{bmatrix} = \begin{bmatrix}1\\0\\4\\9\end{bmatrix}. $$

The residual is \(\mathbf{r}=\mathbf{b}-\mathbf{A}\mathbf{c}=(1,-2,2,-1)^{\top}\). Therefore the residual sum of squares is \(1^2+(-2)^2+2^2+(-1)^2=10\). As a further check on the fit, the residual is orthogonal to each design column: its dot products with \((1,1,1,1)^{\top}\), \((-2,-1,1,2)^{\top}\), and \((4,1,1,4)^{\top}\) are respectively \(1-2+2-1=0\), \(-2+2+2-2=0\), and \(4-2+2-4=0\).

Two More Polynomial Fits

Worked Example: A Linear Polynomial Fit

Fit a degree-one polynomial to the inputs \(-1,0,1\) and responses \(0,1,3\). Here the design matrix has columns for \(1,x\):

$$ \mathbf{A}= \begin{bmatrix}1&-1\\1&0\\1&1\end{bmatrix}, \qquad \mathbf{b}= \begin{bmatrix}0\\1\\3\end{bmatrix}. $$

A reduced QR factorization is \(\mathbf{Q}=\begin{bmatrix}1/\sqrt{3}&-1/\sqrt{2}\\1/\sqrt{3}&0\\1/\sqrt{3}&1/\sqrt{2}\end{bmatrix}\) and \(\mathbf{R}=\begin{bmatrix}\sqrt{3}&0\\0&\sqrt{2}\end{bmatrix}\). The columns of \(\mathbf{Q}\) have squared norms \(3(1/3)=1\) and \(1/2+1/2=1\), and their dot product is \((-1+0+1)/\sqrt{6}=0\). Multiplying \(\mathbf{Q}\mathbf{R}\) gives columns \((1,1,1)^{\top}\) and \((-1,0,1)^{\top}\), so the factorization reproduces \(\mathbf{A}\).

Now \(\mathbf{Q}^{\top}\mathbf{b}=(4/\sqrt{3},3/\sqrt{2})^{\top}\). The triangular equations give \(\sqrt{2}c_1=3/\sqrt{2}\), so \(c_1=3/2\), and \(\sqrt{3}c_0=4/\sqrt{3}\), so \(c_0=4/3\). The fitted polynomial is \(p(x)=4/3+(3/2)x\), and its fitted values are \(\mathbf{A}\mathbf{c}=(-1/6,4/3,17/6)^{\top}\). Subtracting from the data gives \(\mathbf{r}=(1/6,-1/3,1/6)^{\top}\), so the residual sum of squares is \(1/36+1/9+1/36=1/6\). The residual is orthogonal to both design columns: \(1/6-1/3+1/6=0\), and \((-1)(1/6)+1/6=0\).

Worked Example: A Different Response on the Same Quadratic Design

Keep the inputs \(-2,-1,1,2\) and the quadratic design matrix from the first example, but use \(\mathbf{b}=(1,0,0,0)^{\top}\). Since the design matrix is unchanged, use the same verified \(\mathbf{Q}\) and \(\mathbf{R}\). The transformed response is

$$ \mathbf{Q}^{\top}\mathbf{b} = \begin{bmatrix} 1/2\\ -2/\sqrt{10}\\ 1/2 \end{bmatrix}. $$

Solving \(\mathbf{R}\mathbf{c}=\mathbf{Q}^{\top}\mathbf{b}\), the last equation gives \(3c_2=1/2\), so \(c_2=1/6\). The middle equation gives \(\sqrt{10}c_1=-2/\sqrt{10}\), so \(c_1=-1/5\). The first gives \(2c_0+5/6=1/2\), so \(c_0=-1/6\). Thus \(p(x)=-1/6-(1/5)x+(1/6)x^2\). Evaluating at the four inputs gives \(\mathbf{A}\mathbf{c}=(9/10,1/5,-1/5,1/10)^{\top}\). The residual is consequently \((1/10,-1/5,1/5,-1/10)^{\top}\), and its squared norm is \(1/100+1/25+1/25+1/100=1/10\). Its dot products with the three columns of the design matrix are \(0\), \(0\), and \(0\), respectively, confirming residual orthogonality.

Why the Triangular Solve Matters

The QR computation has three distinct stages: build the polynomial design matrix, transform the data to \(\mathbf{Q}^{\top}\mathbf{b}\), then solve the triangular system \(\mathbf{R}\mathbf{c}=\mathbf{Q}^{\top}\mathbf{b}\). The transformed data are not themselves the polynomial coefficients; the triangular solve converts them to coefficients in the original design columns. Once \(\mathbf{c}\) is known, \(\mathbf{A}\mathbf{c}\) gives fitted values, \(\mathbf{b}-\mathbf{A}\mathbf{c}\) gives residuals, and summing the squared residual entries gives the residual sum of squares.

One advantage of this route is that it does not require forming \(\mathbf{A}^{\top}\mathbf{A}\). Instead, the method uses the orthonormal columns of \(\mathbf{Q}\) and a triangular system in \(\mathbf{R}\). Full column rank matters: if the polynomial design matrix does not have independent columns, the invertible triangular solve in the theorem is not available as stated, and the least-squares coefficients need not be unique.

Common Mistake: Treating \(Q^{\top}b\) as the Coefficients

In the first quadratic example, a tempting but incorrect step would be to set the coefficient vector equal to \(\mathbf{Q}^{\top}\mathbf{b}=(7,2\sqrt{10},3)^{\top}\). These are the correct transformed-response entries, but they are not the coefficients of \(1,x,x^2\). Substitution into the required triangular equation exposes the error:

$$ \mathbf{R} \begin{bmatrix}7\\2\sqrt{10}\\3\end{bmatrix} = \begin{bmatrix}29\\20\\9\end{bmatrix} \ne \begin{bmatrix}7\\2\sqrt{10}\\3\end{bmatrix} =\mathbf{Q}^{\top}\mathbf{b}. $$

The correction is to solve \(\mathbf{R}\mathbf{c}=\mathbf{Q}^{\top}\mathbf{b}\), which gives \(\mathbf{c}=(1,2,1)^{\top}\) in that example. A second setup error is to put the data values \(y_i\) into the design matrix: they belong in \(\mathbf{b}\), while the design matrix entries are formed from the inputs and their powers.

Check Your Understanding

For each question, keep the coefficient order aligned with the design-matrix columns and check numerical answers by substitution.

  1. Write the design matrix for a degree-three polynomial using input values \(-1,0,2,3\).
  2. For a degree-two polynomial and four distinct inputs, explain why the design matrix has independent columns.
  3. In a reduced QR factorization \(\mathbf{A}=\mathbf{Q}\mathbf{R}\), what triangular system must be solved to obtain the least-squares coefficient vector?
  4. For the linear fit above, verify the residual sum of squares directly from the three residual entries.
  5. Explain why \(\mathbf{Q}^{\top}\mathbf{b}\) is generally not the coefficient vector in the original polynomial basis.

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