Tutorials › Linear Algebra › Comparing Linear And Quadratic Fits

Least Squares and QR Factorization · Tutorial 694 of 1000

Comparing Linear And Quadratic Fits

Learn to compare linear and quadratic least-squares fits by checking their fitted values and residual sums of squares.

Intermediate 10 min read

What You'll Learn

  • Set up line and quadratic models for the same observations.
  • Calculate fitted values and residual sums of squares exactly.
  • Explain why adding the squared-input column cannot increase the minimum residual sum of squares.
  • Identify when two nested fits have equal residual sums of squares.
  • Distinguish a smaller residual sum of squares from evidence that a quadratic model is appropriate.

What a Fit Comparison Measures

For a fixed set of observations, a line and a quadratic curve generally produce different fitted values. Comparing the two fits asks how closely each model can match the observed data, as measured by the sum of squared residuals. The comparison has a useful mathematical guarantee: a quadratic model includes every line as a special case, so its best possible residual sum of squares cannot be larger. That guarantee is about the models’ available choices; by itself, it does not establish that the observations follow a quadratic pattern.

Write the observations as \((x_i,y_i)\), for \(i=1,\dots,m\), and collect the outputs in the column vector \(\mathbf{y}=(y_1,\dots,y_m)^{\top}\). A line \(a+bx\) has design matrix \(\mathbf{A}_{L}\), whose \(i\)th row is \((1,x_i)\). A quadratic \(a+bx+cx^2\) has design matrix \(\mathbf{A}_{Q}\), whose \(i\)th row is \((1,x_i,x_i^2)\). The fitted values are the entries of the vector \(\widehat{\mathbf{y}}\) produced by a least-squares fit. The residual vector is \(\mathbf{r}=\mathbf{y}-\widehat{\mathbf{y}}\), and the residual sum of squares is \(\operatorname{RSS}=\mathbf{r}^{\top}\mathbf{r}=\sum_{i=1}^{m}r_i^2\).

The line and quadratic model spaces are the column spaces of their design matrices. Since the first two columns of \(\mathbf{A}_{Q}\) are the columns of \(\mathbf{A}_{L}\), every vector of fitted values available to a line is also available to a quadratic. In particular, the quadratic model can choose \(c=0\). The following result makes the consequence precise, including when the two fits tie.

Theorem (Comparison for nested model spaces): Let \(S_L\) and \(S_Q\) be subspaces of \(\mathbb{R}^m\) with \(S_L\subseteq S_Q\). Let \(\widehat{\mathbf{y}}_L\in S_L\) and \(\widehat{\mathbf{y}}_Q\in S_Q\) be least-squares fitted vectors for the same observation vector \(\mathbf{y}\). Then $$ \|\mathbf{y}-\widehat{\mathbf{y}}_L\|^2 = \|\mathbf{y}-\widehat{\mathbf{y}}_Q\|^2 + \|\widehat{\mathbf{y}}_Q-\widehat{\mathbf{y}}_L\|^2. $$ Consequently, the fit in \(S_Q\) has no larger residual sum of squares. The two residual sums of squares are equal if and only if the fitted vectors are equal.

Proof. By the least-squares residual orthogonality characterization, \(\mathbf{y}-\widehat{\mathbf{y}}_Q\) is orthogonal to every vector in \(S_Q\). The difference \(\widehat{\mathbf{y}}_Q-\widehat{\mathbf{y}}_L\) belongs to \(S_Q\), because both fitted vectors belong to \(S_Q\): \(\widehat{\mathbf{y}}_Q\in S_Q\), and \(\widehat{\mathbf{y}}_L\in S_L\subseteq S_Q\). Thus \(\langle\mathbf{y}-\widehat{\mathbf{y}}_Q,\widehat{\mathbf{y}}_Q-\widehat{\mathbf{y}}_L\rangle=0\). Now write \(\mathbf{y}-\widehat{\mathbf{y}}_L=(\mathbf{y}-\widehat{\mathbf{y}}_Q)+(\widehat{\mathbf{y}}_Q-\widehat{\mathbf{y}}_L)\). Expanding the squared norm, the cross term is zero by that orthogonality, giving the displayed identity. The final term is a squared norm, so it is nonnegative. It is zero exactly when \(\widehat{\mathbf{y}}_Q-\widehat{\mathbf{y}}_L=\mathbf{0}\), which is exactly equality of the fitted vectors. \(\square\)

For line and quadratic fits, take \(S_L=\operatorname{Col}\mathbf{A}_L\) and \(S_Q=\operatorname{Col}\mathbf{A}_Q\). The inclusion holds for any list of inputs. The theorem therefore compares the minimum residual sums of squares, even if the columns of a design matrix are dependent. In the worked examples, the fitted vectors and coefficients are shown directly, so there is no ambiguity about which fit is being compared.

Worked Comparisons on the Same Inputs

Worked Example: A Quadratic Fits Exactly

Use the four observations with inputs \(-1,0,1,2\) and outputs \(1,0,1,4\). Compare the best-fit line with the quadratic fit.

For the line, \(\mathbf{A}_L\) has columns \((1,1,1,1)^{\top}\) and \((-1,0,1,2)^{\top}\). The normal equations are \(\mathbf{A}_L^{\top}\mathbf{A}_L\begin{bmatrix}a\\b\end{bmatrix}=\mathbf{A}_L^{\top}\mathbf{y}\). Here the entries are \(\sum 1=4\), \(\sum x_i=2\), \(\sum x_i^2=6\), \(\sum y_i=6\), and \(\sum x_i y_i=8\), so the equations are

$$ \begin{bmatrix}4&2\\2&6\end{bmatrix} \begin{bmatrix}a\\b\end{bmatrix} = \begin{bmatrix}6\\8\end{bmatrix}. $$

From \(4a+2b=6\), \(a=\frac32-\frac12b\). Substituting into \(2a+6b=8\) gives \(3-b+6b=8\), so \(b=1\) and \(a=1\). The fitted line is \(1+x\). At the four inputs its fitted values are \((0,1,2,3)^{\top}\), so the residual vector and residual sum of squares are

$$ \mathbf{r}_L=(1,-1,-1,1)^{\top}, \qquad \operatorname{RSS}_L=1^2+(-1)^2+(-1)^2+1^2=4. $$

The outputs equal \(x_i^2\) at every input, so the quadratic \(x^2\), with coefficients \((a,b,c)=(0,0,1)\), has fitted values \((1,0,1,4)^{\top}\). Its residual vector is \(\mathbf{0}\) and \(\operatorname{RSS}_Q=0\). Check: evaluating \(x^2\) at \(-1,0,1,2\) gives \(1,0,1,4\), exactly the observed outputs. The decrease in RSS is \(4\), and the difference between the fitted vectors is \((1,-1,-1,1)^{\top}\), whose squared norm is \(4\), as the theorem requires.

Worked Example: A Smaller but Nonzero Quadratic RSS

Keep the same inputs, but use outputs \((1,2,0,3)^{\top}\). Find both fits and compare them.

For the line, the normal equations are \(\begin{bmatrix}4&2\\2&6\end{bmatrix}\begin{bmatrix}a\\b\end{bmatrix} =\begin{bmatrix}6\\5\end{bmatrix}\), since \(\sum y_i=6\) and \(\sum x_i y_i=5\). The first equation gives \(a=\frac32-\frac12b\). Substitution into the second gives \(3-b+6b=5\), hence \(b=\frac25\) and \(a=\frac{13}{10}\). The line’s fitted values are \((\frac9{10},\frac{13}{10},\frac{17}{10},\frac{21}{10})^{\top}\). Its residuals are \((\frac1{10},\frac7{10},-\frac{17}{10},\frac9{10})^{\top}\), and therefore

$$ \operatorname{RSS}_L =\frac{1+49+289+81}{100} =\frac{21}{5}. $$

For the quadratic fit, consider \(\widehat{\mathbf{y}}_Q=(\frac75,\frac45,\frac65,\frac{13}{5})^{\top}\). These are the values at \(-1,0,1,2\) of \(\frac45-\frac1{10}x+\frac12x^2\): at \(x=0\) the value is \(\frac45\); at \(x=1\) it is \(\frac45-\frac1{10}+\frac12=\frac65\); at \(x=-1\) it is \(\frac45+\frac1{10}+\frac12=\frac75\); at \(x=2\) it is \(\frac45-\frac15+2=\frac{13}{5}\).

The resulting residual vector is \(\mathbf{r}_Q=(-\frac25,\frac65,-\frac65,\frac25)^{\top}\). To check that this polynomial is a least-squares fit, verify residual orthogonality to the quadratic design columns \((1,1,1,1)^{\top}\), \((-1,0,1,2)^{\top}\), and \((1,0,1,4)^{\top}\): their respective dot products with \(\mathbf{r}_Q\) are \(\frac{-2+6-6+2}{5}=0\), \(\frac{2-6+4}{5}=0\), and \(\frac{-2-6+8}{5}=0\). Thus the residual is orthogonal to the quadratic model space, the least-squares characterization. Its RSS is

$$ \operatorname{RSS}_Q =\frac{4+36+36+4}{25} =\frac{16}{5}. $$

The quadratic RSS is smaller by \(\frac{21}{5}-\frac{16}{5}=1\). The difference between the fitted vectors is \((\frac12,-\frac12,-\frac12,\frac12)^{\top}\), whose squared norm is \(1\). Check: adding this difference to the line’s fitted values gives \((\frac75,\frac45,\frac65,\frac{13}{5})^{\top}\), and the theorem’s identity reads \(\frac{21}{5}=\frac{16}{5}+1\).

Worked Example: Equal Fits Despite Nonzero Residuals

Use the same inputs and outputs \((2,-1,6,3)^{\top}\). The line \(2+x\) has fitted values \((1,2,3,4)^{\top}\) and residuals \((1,-3,3,-1)^{\top}\). Its residual is orthogonal to the constant and input columns because \(1-3+3-1=0\) and \(-1+0+3-2=0\). Its RSS is \(1+9+9+1=20\).

It is also orthogonal to the squared-input column: \(1+0+3-4=0\). Hence it is orthogonal to all three quadratic design columns. The same fitted vector, arising from the quadratic coefficients \((a,b,c)=(2,1,0)\), is therefore a least-squares fit for the quadratic model too. Check: at each input, \(2+x\) gives \(1,2,3,4\), and subtracting these values from the observations returns \((1,-3,3,-1)^{\top}\). Both models have RSS \(20\), not zero; the added quadratic term does not improve the fitted values for these data.

What the Comparison Does and Does Not Establish

The theorem explains the basic comparison: expanding the model space gives the quadratic fit additional choices, so its minimum RSS can stay the same or decrease, but cannot increase. If the RSS decreases, the fitted vector has changed. If the RSS is unchanged, the theorem shows that the fitted vectors are identical. This conclusion concerns fitted values; when a design matrix has dependent columns, different coefficient lists can produce the same fitted vector.

A lower RSS alone does not show that a quadratic model is a better explanation of the observations in every sense. It does not prove that the underlying relationship is quadratic, establish that the change is important, or guarantee accurate predictions at inputs not observed. The quadratic model has an extra adjustable coefficient, which can use that flexibility to reduce error on the data used for fitting. A mathematical comparison of RSS is not, by itself, a statistical test or a guarantee about future observations.

Common Pitfall: Adding Residuals Instead of Squaring Them

For the first example, the line’s residuals are \((1,-1,-1,1)^{\top}\). Adding them gives \(1-1-1+1=0\). A tempting but incorrect calculation is to call this zero RSS and conclude that the line fits perfectly. The RSS is the sum of squared residuals, not their signed sum: \(1^2+(-1)^2+(-1)^2+1^2=4\). The quadratic fit has RSS \(0\) for those observations. Positive and negative residuals can cancel in a sum, but squaring makes each contribution nonnegative.

Check Your Understanding

For each comparison, distinguish the residual sum of squares from the fitted values and state what the comparison establishes.

  1. For nested model spaces \(S_L\subseteq S_Q\), what identity relates their residual sums of squares to the difference between their fitted vectors?
  2. In the first worked example, calculate the line’s RSS from its residual vector and explain why the signed residual sum is not the RSS.
  3. In the second worked example, verify that the quadratic residual is orthogonal to each of the three quadratic design columns.
  4. In the third worked example, why can both models have the same nonzero RSS?
  5. Does a lower RSS by itself prove that the data-generating relationship is quadratic? Explain what the comparison alone establishes.

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