Tutorials › Linear Algebra › A Worked Best-Fit Line From Data

Least Squares and QR Factorization · Tutorial 692 of 1000

A Worked Best-Fit Line From Data

Work through exact datasets to find a best-fit line using two equivalent calculations and check the fit through residual orthogonality.

Intermediate 9 min read

What You'll Learn

  • Build a design matrix for fitting a line to data.
  • Solve the normal equations to obtain the line coefficients.
  • Apply the closed-form formulas using means and centered sums.
  • Compute residuals and test both orthogonality conditions.
  • Identify how omitting the intercept changes the fitting problem.

From Data to a Best-Fit Line

Given data points \((x_i,y_i)\), a line will not generally pass through every point. A best-fit line instead chooses its intercept and slope to make the total squared vertical error as small as possible. The calculation can be organized as a least-squares problem: the unknown coefficients form a vector, and the columns of a design matrix describe the constant and \(x\)-dependent parts of the line.

Here we work over \(\mathbb{R}\), with \(n\) data points and not all \(x_i\) equal. Write a candidate line as \(\widehat y=a+bx\), where \(a\) is the intercept and \(b\) is the slope. Its residual at data point \(i\) is \(r_i=y_i-(a+bx_i)\). The least-squares line minimizes the sum of squared residuals, \(\sum_{i=1}^n r_i^2\). In matrix form, the coefficient vector is \(\mathbf{c}=\begin{bmatrix}a\\b\end{bmatrix}\), and each row of the design matrix records the constant term and the corresponding \(x_i\).

Definition: For data \((x_i,y_i)\), the design matrix for fitting a line is \(\mathbf{X}=\begin{bmatrix}1&x_1\\ \vdots&\vdots\\1&x_n\end{bmatrix}\), the response vector is \(\mathbf{y}=(y_1,\dots,y_n)^\top\), and the residual vector for coefficients \(\mathbf{c}\) is \(\mathbf{r}=\mathbf{y}-\mathbf{X}\mathbf{c}\). A best-fit line is a choice of \(a,b\) that minimizes \(\|\mathbf{r}\|^2\).

The first column of \(\mathbf{X}\) represents the intercept; its second column represents the slope contribution. Thus \(\mathbf{X}\mathbf{c}\) contains the fitted values \(a+bx_i\). When the \(x_i\) are not all equal, these two columns are linearly independent: if \(\alpha+\beta x_i=0\) for every \(i\), subtracting the equations for two distinct \(x_i\) gives \(\beta=0\), and then \(\alpha=0\). This condition makes the least-squares coefficients unique.

Why the Normal Equations Identify the Best Fit

For a candidate coefficient vector \(\mathbf{c}\), the residual is \(\mathbf{r}=\mathbf{y}-\mathbf{X}\mathbf{c}\). The normal equations are \(\mathbf{X}^{\top}\mathbf{X}\mathbf{c}=\mathbf{X}^{\top}\mathbf{y}\), equivalently \(\mathbf{X}^{\top}\mathbf{r}=\mathbf{0}\). For a line, this says that the residual is orthogonal to each of the two design-matrix columns: the constant column and the \(x\)-values column.

Theorem (Residual orthogonality criterion): Let \(\mathbf{X}\in\mathbb{R}^{n\times p}\), \(\mathbf{y}\in\mathbb{R}^n\), and \(\mathbf{r}=\mathbf{y}-\mathbf{X}\mathbf{c}\). The vector \(\mathbf{c}\) minimizes \(\|\mathbf{y}-\mathbf{X}\mathbf{d}\|^2\) over all \(\mathbf{d}\in\mathbb{R}^p\) if and only if \(\mathbf{X}^{\top}\mathbf{r}=\mathbf{0}\). If the columns of \(\mathbf{X}\) are linearly independent, this minimizing \(\mathbf{c}\) is unique.

Proof. For any change \(\mathbf{h}\in\mathbb{R}^p\), the residual from the coefficients \(\mathbf{c}+\mathbf{h}\) is \(\mathbf{r}-\mathbf{X}\mathbf{h}\). Expanding its squared norm gives

$$ \|\mathbf{r}-\mathbf{X}\mathbf{h}\|^2 =\|\mathbf{r}\|^2-2\mathbf{h}^{\top}\mathbf{X}^{\top}\mathbf{r} +\|\mathbf{X}\mathbf{h}\|^2. $$

If \(\mathbf{X}^{\top}\mathbf{r}=\mathbf{0}\), the middle term vanishes, and the last term is nonnegative. Therefore no change \(\mathbf{h}\) can reduce the squared residual, so \(\mathbf{c}\) is a minimizer. Conversely, suppose \(\mathbf{c}\) is a minimizer. For any \(\mathbf{h}\), changing the coefficients to \(\mathbf{c}+t\mathbf{h}\), where \(t\in\mathbb{R}\), changes the squared residual by \(-2t\mathbf{h}^{\top}\mathbf{X}^{\top}\mathbf{r}+t^2\|\mathbf{X}\mathbf{h}\|^2\). If \(\mathbf{h}^{\top}\mathbf{X}^{\top}\mathbf{r}\ne0\), choosing \(t\) with the same sign as this quantity and sufficiently small magnitude makes this change negative: the linear term is negative and dominates the quadratic term. That contradicts minimality. Thus \(\mathbf{h}^{\top}\mathbf{X}^{\top}\mathbf{r}=0\) for every \(\mathbf{h}\); taking \(\mathbf{h}\) to be each standard basis vector gives \(\mathbf{X}^{\top}\mathbf{r}=\mathbf{0}\). Finally, if the columns are independent, then \(\mathbf{X}\mathbf{h}=\mathbf{0}\) implies \(\mathbf{h}=\mathbf{0}\). For a minimizer, the displayed expansion reduces to \(\|\mathbf{r}\|^2+\|\mathbf{X}\mathbf{h}\|^2\), which is strictly larger whenever \(\mathbf{h}\ne\mathbf{0}\). Hence the minimizer is unique. \(\square\)

Worked Example: Solve the Normal Equations

Worked Example: Fit a Line to Four Data Points

Find the least-squares line for \((0,1),(1,2),(2,2),(3,4)\) using the design matrix and normal equations.

The design matrix and response vector are

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

The matrix products are \(\mathbf{X}^{\top}\mathbf{X}=\begin{bmatrix}4&6\\6&14\end{bmatrix}\) and \(\mathbf{X}^{\top}\mathbf{y}=\begin{bmatrix}9\\18\end{bmatrix}\): the entries come from \(4\) ones, \(\sum x_i=6\), \(\sum x_i^2=14\), \(\sum y_i=9\), and \(\sum x_i y_i=18\). Thus the normal equations are

$$ \begin{bmatrix}4&6\\6&14\end{bmatrix} \begin{bmatrix}a\\b\end{bmatrix} = \begin{bmatrix}9\\18\end{bmatrix}. $$

Row-reduce the augmented matrix. First apply \(R_2\to R_2-\tfrac32R_1\), then \(R_2\to\tfrac15R_2\), \(R_1\to R_1-6R_2\), and \(R_1\to\tfrac14R_1\):

$$ \begin{bmatrix}4&6&9\\6&14&18\end{bmatrix} \sim \begin{bmatrix}4&6&9\\0&5&\tfrac92\end{bmatrix} \sim \begin{bmatrix}4&6&9\\0&1&\tfrac9{10}\end{bmatrix} \sim \begin{bmatrix}4&0&\tfrac{18}{5}\\0&1&\tfrac9{10}\end{bmatrix} \sim \begin{bmatrix}1&0&\tfrac9{10}\\0&1&\tfrac9{10}\end{bmatrix}. $$

Therefore \(a=\tfrac9{10}\), \(b=\tfrac9{10}\), and the best-fit line is \(\widehat y=\tfrac9{10}+\tfrac9{10}x\). The design-matrix columns are independent because the \(x_i\) are not all equal, so the theorem guarantees this line is the unique least-squares fit.

Check: The fitted values are \(\tfrac9{10},\tfrac{18}{10},\tfrac{27}{10},\tfrac{36}{10}\), so the residual vector is \(\mathbf{r}=(\tfrac1{10},\tfrac15,-\tfrac7{10},\tfrac25)^\top\). Its entries sum to \(\tfrac1{10}+\tfrac2{10}-\tfrac7{10}+\tfrac4{10}=0\), and their \(x\)-weighted sum is \(0+\tfrac2{10}-\tfrac{14}{10}+\tfrac{12}{10}=0\). Hence \(\mathbf{X}^{\top}\mathbf{r}=\mathbf{0}\), as required.

The Closed-Form Coefficients

The same calculation can be written using averages and centered data. Define \(\bar x=\frac1n\sum_{i=1}^n x_i\) and \(\bar y=\frac1n\sum_{i=1}^n y_i\). Provided the \(x_i\) are not all equal, the best-fit line has slope and intercept

Formula: \(b=\dfrac{\sum_{i=1}^n(x_i-\bar x)(y_i-\bar y)}{\sum_{i=1}^n(x_i-\bar x)^2}\) and \(a=\bar y-b\bar x\). The denominator is positive when the \(x_i\) are not all equal.

These formulas follow by writing the two normal equations as \(na+b\sum x_i=\sum y_i\) and \(a\sum x_i+b\sum x_i^2=\sum x_i y_i\). The first gives \(a=\bar y-b\bar x\). Substituting this into the second gives \(b\bigl(\sum x_i^2-(\sum x_i)^2/n\bigr)=\sum x_i y_i-(\sum x_i)(\sum y_i)/n\). The parenthesized expression equals \(\sum(x_i-\bar x)^2\), and the right side equals \(\sum(x_i-\bar x)(y_i-\bar y)\), yielding the stated slope. The denominator is a sum of squares; it is zero only if every \(x_i=\bar x\).

Worked Example: Use the Centered-Sum Formulas

Recalculate the line for \((0,1),(1,2),(2,2),(3,4)\) using the closed-form formulas.

The means are \(\bar x=(0+1+2+3)/4=3/2\) and \(\bar y=(1+2+2+4)/4=9/4\). The centered \(x\)-values are \(-3/2,-1/2,1/2,3/2\), and the centered \(y\)-values are \(-5/4,-1/4,-1/4,7/4\). The numerator of the slope is

$$ \left(-\tfrac32\right)\left(-\tfrac54\right) +\left(-\tfrac12\right)\left(-\tfrac14\right) +\left(\tfrac12\right)\left(-\tfrac14\right) +\left(\tfrac32\right)\left(\tfrac74\right) =\tfrac{15}{8}+\tfrac18-\tfrac18+\tfrac{21}{8} =\tfrac92. $$

The denominator is \(\tfrac94+\tfrac14+\tfrac14+\tfrac94=5\). Therefore \(b=(9/2)/5=9/10\), and \(a=9/4-(9/10)(3/2)=45/20-27/20=9/10\). This agrees with the normal-equation calculation.

Check: The residuals are again \(\tfrac1{10},\tfrac15,-\tfrac7{10},\tfrac25\). Their sum is \(0\), and their weighted sum \(0(\tfrac1{10})+1(\tfrac15)+2(-\tfrac7{10})+3(\tfrac25)=0\). Thus both the intercept-column and slope-column orthogonality conditions hold.

Worked Example: A Second Dataset

Worked Example: Compare Both Methods on Three Points

Fit a line to \((1,3),(2,5),(4,6)\), first by normal equations and then by the closed-form coefficients.

Here \(\sum x_i=7\), \(\sum x_i^2=21\), \(\sum y_i=14\), and \(\sum x_i y_i=37\). Hence the normal equations are

$$ \begin{bmatrix}3&7\\7&21\end{bmatrix} \begin{bmatrix}a\\b\end{bmatrix} = \begin{bmatrix}14\\37\end{bmatrix}. $$

Apply \(R_2\to R_2-\tfrac73R_1\), \(R_2\to\tfrac{3}{14}R_2\), \(R_1\to R_1-7R_2\), and \(R_1\to\tfrac13R_1\):

$$ \begin{bmatrix}3&7&14\\7&21&37\end{bmatrix} \sim \begin{bmatrix}3&7&14\\0&\tfrac{14}{3}&\tfrac{13}{3}\end{bmatrix} \sim \begin{bmatrix}3&7&14\\0&1&\tfrac{13}{14}\end{bmatrix} \sim \begin{bmatrix}3&0&\tfrac{15}{2}\\0&1&\tfrac{13}{14}\end{bmatrix} \sim \begin{bmatrix}1&0&\tfrac52\\0&1&\tfrac{13}{14}\end{bmatrix}. $$

Thus \(a=5/2\) and \(b=13/14\). For the formula check, \(\bar x=7/3\), \(\bar y=14/3\). The centered sums are \(\sum(x_i-\bar x)^2=14/3\) and \(\sum(x_i-\bar x)(y_i-\bar y)=13/3\), so \(b=(13/3)/(14/3)=13/14\). Then \(a=14/3-(13/14)(7/3)=5/2\).

Check: The fitted values at \(x=1,2,4\) are \(24/7,61/14,87/14\). Subtracting them from the observations gives residuals \(-3/7,9/14,-3/14\). Their sum is \(-6/14+9/14-3/14=0\), and their \(x\)-weighted sum is \(-6/14+18/14-12/14=0\). The residual is orthogonal to both design columns, verifying the fit.

Common Pitfall: Omitting the Intercept

A frequent setup error is using the single-column design matrix \(\begin{bmatrix}0&1&2&3\end{bmatrix}^{\top}\) for the first dataset. That matrix fits lines of the form \(\widehat y=bx\), which are forced through the origin; it does not fit the general line \(a+bx\). For that restricted model the least-squares slope is \(\sum x_i y_i/\sum x_i^2=18/14=9/7\), not \(9/10\). Its residuals are \(1,5/7,-4/7,1/7\), whose sum is \(1+5/7-4/7+1/7=9/7\ne0\). This fails the constant-column orthogonality condition because the constant column was omitted. The correction is to include a column of ones whenever the model includes an intercept.

For a correctly formed line-fitting design matrix, the two orthogonality checks have a direct interpretation: residuals sum to zero, and their \(x\)-weighted sum is zero. They are not separate rules to memorize; together they say \(\mathbf{X}^{\top}\mathbf{r}=\mathbf{0}\), which certifies the least-squares minimum. When the \(x\)-values vary, the fit is unique, and either the normal equations or the centered-sum formulas produces the same coefficients.

Check Your Understanding

For each question, keep the intercept in the model unless the question explicitly says otherwise.

  1. For \((0,2),(1,3),(2,5)\), write the design matrix and response vector.
  2. For those data, compute \(\bar x\), \(\bar y\), and the closed-form slope and intercept.
  3. State the two residual sums that must be zero for a line fit, and explain which design-matrix column each corresponds to.
  4. Why does a dataset with all \(x_i\) equal fail to determine a unique slope and intercept?
  5. For the first worked dataset, compute the slope obtained by forcing the line through the origin and check whether its residuals sum to zero.

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