Tutorials › Linear Algebra › Comparing Polynomial Fits with Design Matrices

Applications in Networks, Economics and Data · Tutorial 951 of 1000

Comparing Polynomial Fits with Design Matrices

Learn to build polynomial design matrices, compute and check least-squares fits, and interpret what changes—and what may not—when the polynomial degree increases.

Intermediate 13 min read

What You'll Learn

  • Build a degree-d polynomial design matrix with one column for each power from zero through d.
  • Solve least-squares problems using the normal equations and check the residuals.
  • Distinguish the number of observations from the number of polynomial coefficients.
  • Identify when different coefficient vectors give the same fitted values.
  • Compare residual sizes for nested polynomial models and interpret equality.
  • Recognise how repeated input values can affect a design matrix.

From Polynomial Models to Design Matrices

Suppose measurements are recorded as pairs \((x_i,y_i)\), where \(x_i\) is an input and \(y_i\) is its observed response. A polynomial model predicts the response using powers of the input. The design matrix organises those powers into columns, so fitting the model becomes a least-squares problem in linear algebra. Comparing polynomial degrees then means comparing the column spaces of different design matrices—not simply counting observations or comparing the number of coefficients.

Throughout, assume the inputs and responses are real. There are \(m\) observations, with input vector \(\mathbf{x}=(x_1,\dots,x_m)^\top\) and response vector \(\mathbf{y}=(y_1,\dots,y_m)^\top\). A polynomial of degree at most \(d\) has the form \(p(x)=\beta_0+\beta_1x+\cdots+\beta_dx^d\). The fitted values at the observed inputs are \(p(x_1),\dots,p(x_m)\). The degree is an upper bound: the coefficient \(\beta_d\) may be zero, so the resulting polynomial may have lower degree.

Definition: The degree-\(d\) polynomial design matrix for inputs \(x_1,\dots,x_m\) is the \(m\times(d+1)\) matrix \(\mathbf{A}_d\) whose entry in row \(i\), column \(j+1\) is \(x_i^j\), for \(j=0,\dots,d\). Thus row \(i\) is \((1,x_i,x_i^2,\dots,x_i^d)\). A least-squares coefficient vector \(\boldsymbol{\beta}\) minimises \(\|\mathbf{y}-\mathbf{A}_d\boldsymbol{\beta}\|^2\); its fitted-value vector is \(\widehat{\mathbf{y}}=\mathbf{A}_d\boldsymbol{\beta}\), and its residual vector is \(\mathbf{r}=\mathbf{y}-\widehat{\mathbf{y}}\).

The number of observations is the number of rows, \(m\). The number of coefficient positions is the number of columns, \(d+1\). These counts answer different questions. For instance, four observations and a quadratic model give a \(4\times3\) design matrix: there are four data points but three coefficients.

Computing and Checking a Fit

For any real matrix \(\mathbf{A}\), its column space \(\operatorname{Col}\mathbf{A}\) is the set of all vectors \(\mathbf{A}\boldsymbol{\beta}\). Least squares seeks the vector in this column space closest to \(\mathbf{y}\). The projection property of least squares says that the residual at a fit is orthogonal to every column of the design matrix. Consequently, a least-squares coefficient vector satisfies the normal equations \(\mathbf{A}^{\top}\mathbf{A}\boldsymbol{\beta}=\mathbf{A}^{\top}\mathbf{y}\). If the columns are dependent, the coefficient vector need not be unique; the fitted values are still unique.

Worked Example: Comparing Linear and Quadratic Fits

Fit polynomials of degree at most one and at most two to the observations \((-1,2),(0,0),(1,4),(2,4)\). Compare the residuals.

In the order \(x=-1,0,1,2\), the response vector is \(\mathbf{y}=(2,0,4,4)^\top\). The two design matrices are

$$ \mathbf{A}_1= \begin{bmatrix} 1&-1\\ 1&0\\ 1&1\\ 1&2 \end{bmatrix}, \qquad \mathbf{A}_2= \begin{bmatrix} 1&-1&1\\ 1&0&0\\ 1&1&1\\ 1&2&4 \end{bmatrix}. $$

For the linear fit, the normal equations are

$$ \begin{bmatrix}4&2\\2&6\end{bmatrix} \begin{bmatrix}\beta_0\\\beta_1\end{bmatrix} = \begin{bmatrix}10\\10\end{bmatrix}, $$

because the column sums are \(4\) and \(2\), their squared and cross-products give \(4,2,6\), and \(\mathbf{A}_1^\top\mathbf{y}=(10,10)^\top\). The equations are \(4\beta_0+2\beta_1=10\) and \(2\beta_0+6\beta_1=10\). Subtracting the second equation from twice the first gives \(6\beta_0-2\beta_1=10\); solving together gives \(\beta_0=2\), \(\beta_1=1\). Thus the fitted values are \((1,2,3,4)^\top\), and the residual is \((1,-2,1,0)^\top\). Its dot products with the two design columns are \(1-2+1+0=0\) and \(-1+0+1+0=0\), as required.

For the quadratic fit, the normal equations are

$$ \begin{bmatrix} 4&2&6\\ 2&6&8\\ 6&8&18 \end{bmatrix} \begin{bmatrix}\beta_0\\\beta_1\\\beta_2\end{bmatrix} = \begin{bmatrix}10\\10\\22\end{bmatrix}. $$

Here the columns are the observed values of \(1,x,x^2\); for example, the \(x^2\) column has sum \(6\), and its dot product with \(\mathbf{y}\) is \(22\). The coefficients \((\beta_0,\beta_1,\beta_2)=(\tfrac32,\tfrac12,\tfrac12)\) solve the equations: the left sides are \(6+1+3=10\), \(3+3+4=10\), and \(9+4+9=22\). The fitted polynomial is \(\tfrac32+\tfrac12x+\tfrac12x^2\), giving fitted values \((\tfrac32,\tfrac32,\tfrac52,\tfrac92)^\top\) and residual \((\tfrac12,-\tfrac32,\tfrac32,-\tfrac12)^\top\).

Check: the quadratic residual has dot products \( \tfrac12-\tfrac32+\tfrac32-\tfrac12=0\) with the constant column, \(-\tfrac12+0+\tfrac32-1=0\) with the \(x\) column, and \(\tfrac12+0+\tfrac32-2=0\) with the \(x^2\) column. The sums of squared residuals are \(1+4+1+0=6\) for the linear fit and \(\tfrac14+\tfrac94+\tfrac94+\tfrac14=5\) for the quadratic fit. Both fits satisfy the least-squares orthogonality check, and the quadratic fit has the smaller residual norm.

Why Increasing the Degree Cannot Worsen the Fit

For degrees \(d_1\le d_2\), every degree-\(d_1\) polynomial is also a degree-\(d_2\) polynomial: its additional coefficients can be set to zero. The design-matrix version of this fact is that the first \(d_1+1\) columns of \(\mathbf{A}_{d_2}\) are exactly the columns of \(\mathbf{A}_{d_1}\). Thus \(\operatorname{Col}\mathbf{A}_{d_1}\subseteq\operatorname{Col}\mathbf{A}_{d_2}\). The following result makes the consequence for residuals precise.

Theorem: Let \(\mathbf{y}\in\mathbb{R}^m\), and let \(\widehat{\mathbf{y}}_1,\widehat{\mathbf{y}}_2\) be least-squares fitted-value vectors from two design matrices with nested column spaces \(S_1\subseteq S_2\). Then \(\|\mathbf{y}-\widehat{\mathbf{y}}_2\|^2\le\|\mathbf{y}-\widehat{\mathbf{y}}_1\|^2\). Equality holds exactly when \(\widehat{\mathbf{y}}_1=\widehat{\mathbf{y}}_2\).

Proof. A least-squares fitted-value vector is the orthogonal projection of \(\mathbf{y}\) onto its design matrix's column space. Hence \(\mathbf{y}-\widehat{\mathbf{y}}_2\) is orthogonal to every vector in \(S_2\). Both fitted vectors belong to \(S_2\), because \(S_1\subseteq S_2\); therefore \(\widehat{\mathbf{y}}_2-\widehat{\mathbf{y}}_1\in S_2\). Write \(\mathbf{y}-\widehat{\mathbf{y}}_1=(\mathbf{y}-\widehat{\mathbf{y}}_2)+(\widehat{\mathbf{y}}_2-\widehat{\mathbf{y}}_1)\). The two terms on the right are orthogonal, so expanding the squared norm gives \(\|\mathbf{y}-\widehat{\mathbf{y}}_1\|^2=\|\mathbf{y}-\widehat{\mathbf{y}}_2\|^2+\|\widehat{\mathbf{y}}_2-\widehat{\mathbf{y}}_1\|^2\). The last term is nonnegative, proving the inequality. It equals zero exactly when the two fitted-value vectors are equal, which proves the equality condition as well. \(\square\)

Polynomial design matrices have nested column spaces as degree increases, so this theorem applies directly to their least-squares fits. A larger degree cannot increase the sum of squared residuals on the same observations. It may leave that sum unchanged: this happens precisely when the added columns do not change the fitted values for the response being considered. This is a statement about the observed data and fitted values, not a guarantee that a higher-degree model is preferable for other purposes.

Coefficient Uniqueness and the Number of Observations

A fitted-value vector can be unique even when several coefficient vectors produce it. The distinction is governed by the null space \(\operatorname{Nul}\mathbf{A}\), the set of coefficient vectors \(\mathbf{z}\) satisfying \(\mathbf{A}\mathbf{z}=\mathbf{0}\).

Theorem: For a fixed response vector and design matrix \(\mathbf{A}\), the least-squares coefficient vector is unique if and only if \(\operatorname{Nul}\mathbf{A}=\{\mathbf{0}\}\). If the null space is nontrivial, there are multiple coefficient vectors giving the same least-squares fitted values.

Proof. Least squares has a fitted-value vector \(\widehat{\mathbf{y}}\) in \(\operatorname{Col}\mathbf{A}\), the projection of \(\mathbf{y}\) onto that finite-dimensional column space. If \(\operatorname{Nul}\mathbf{A}=\{\mathbf{0}\}\), any two minimizing coefficient vectors \(\boldsymbol{\beta}\) and \(\boldsymbol{\gamma}\) must give this same fitted vector, so \(\mathbf{A}\boldsymbol{\beta}=\mathbf{A}\boldsymbol{\gamma}\). Thus \(\mathbf{A}(\boldsymbol{\beta}-\boldsymbol{\gamma})=\mathbf{0}\); the null-space assumption implies \(\boldsymbol{\beta}-\boldsymbol{\gamma}=\mathbf{0}\), so the coefficients are unique. Conversely, if \(\mathbf{z}\ne\mathbf{0}\) and \(\mathbf{A}\mathbf{z}=\mathbf{0}\), then for any minimizing coefficient vector \(\boldsymbol{\beta}\), the distinct vector \(\boldsymbol{\beta}+\mathbf{z}\) gives \(\mathbf{A}(\boldsymbol{\beta}+\mathbf{z})=\mathbf{A}\boldsymbol{\beta}\). It therefore gives the same fitted values and the same least-squares error, so the minimizing coefficients are not unique. \(\square\)

A degree-\(d\) polynomial model has \(d+1\) coefficient positions, regardless of the number of observations. If there are fewer than \(d+1\) observations, the matrix has more columns than rows, so its columns cannot be independent; a nonzero null vector exists, and the coefficients cannot be unique. Even with at least \(d+1\) observations, repeated inputs can make columns dependent. The next example shows why a high-degree fit can have exact fitted values without identifying all its coefficients.

Worked Example: Three Observations and a Cubic Model

Use the observations \((-1,1),(0,0),(1,1)\). For degree at most three, the design matrix and response are

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

On these three inputs, the \(x\) and \(x^3\) columns have the same entries. Thus, for any real \(t\), choose \((\beta_0,\beta_1,\beta_2,\beta_3)=(0,t,1,-t)\). The fitted value at \(x=-1\) is \(-t+1+t=1\); at \(x=0\) it is \(0\); and at \(x=1\) it is \(t+1-t=1\). These coefficients fit all observations exactly, so the residual is \(\mathbf{0}\) and its squared norm is \(0\). Taking different values of \(t\) gives different coefficients but the same fitted values. For instance, \(t=0\) gives \(p(x)=x^2\), while \(t=1\) gives \(p(x)=x+x^2-x^3\).

Check: substituting \(x=-1,0,1\) into either polynomial gives \(1,0,1\), respectively. There are three observations but four coefficient positions in the cubic design matrix. Exact fitting here does not imply that the coefficients are uniquely determined.

Repeated Inputs and an Unchanged Fit

Worked Example: Adding a Column Without Improving the Fit

Fit a line to the observations \((0,1),(0,3),(1,2)\). The design matrix and response are

$$ \mathbf{A}_1= \begin{bmatrix} 1&0\\ 1&0\\ 1&1 \end{bmatrix}, \qquad \mathbf{y}= \begin{bmatrix}1\\3\\2\end{bmatrix}. $$

The normal equations are \(\begin{bmatrix}3&1\\1&1\end{bmatrix}\begin{bmatrix}\beta_0\\\beta_1\end{bmatrix}=\begin{bmatrix}6\\2\end{bmatrix}\), since \(\mathbf{A}_1^\top\mathbf{A}_1=\begin{bmatrix}3&1\\1&1\end{bmatrix}\) and \(\mathbf{A}_1^\top\mathbf{y}=(6,2)^\top\). They give \(3\beta_0+\beta_1=6\) and \(\beta_0+\beta_1=2\). Subtracting the second equation from the first gives \(2\beta_0=4\), so \(\beta_0=2\) and \(\beta_1=0\).

The fitted values are \((2,2,2)^\top\), so the residual is \((-1,1,0)^\top\) and its squared norm is \(1+1+0=2\). The residual's dot products with the constant and input columns are \(-1+1+0=0\) and \(0+0+0=0\), verifying the least-squares condition. A constant model also fits the constant vector \((2,2,2)^\top\), so increasing the degree from zero to one does not improve this fit. The extra input column is present, but its fitted coefficient is zero.

Common Pitfall: Confusing Degree with Data Count

A common mistake is to say that a quadratic model has two coefficients because its degree is two, or that three observations automatically mean a quadratic model. Both statements confuse different counts. A polynomial of degree at most \(d\) has \(d+1\) coefficient positions, including the constant term. The number of observations determines the number of rows, not the degree.

For example, with three observations, the degree-three design matrix has four columns, not three. It cannot have four independent columns, and as the cubic example shows, its coefficients may not be unique even when the observations are fit exactly. Conversely, four observations can be fitted with a degree-one model, which still has just two coefficient positions. Before solving, write down the powers included in the model and count the columns. When comparing degrees, keep the response vector and observations fixed, and compare the fitted values or residuals rather than assuming the higher-degree coefficients must all be identifiable.

Polynomial design matrices are useful because they translate curved trends into linear combinations of columns: the unknown coefficients enter linearly even though the inputs appear as powers. The projection viewpoint then gives a reliable comparison. Higher degree enlarges the available column space, so the best residual cannot become worse; whether it becomes smaller depends on whether the enlarged space changes the projection of the observed response.

Check Your Understanding

For each question, identify the design-matrix columns or use the least-squares residual condition to justify your answer.

  1. How many rows and columns does a degree-four design matrix have for seven observations?
  2. Write the degree-two design matrix for inputs \(x_1=-1\), \(x_2=1\), and \(x_3=2\).
  3. Why can two distinct coefficient vectors give the same fitted values? State the condition using the null space.
  4. If the degree-two and degree-three models use the same observations, what can be said about their minimum squared residuals? When are they equal?
  5. For inputs \(0,0,1\), explain why the degree-one design matrix has three rows but only two columns.

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