Tutorials › Linear Algebra › Closed-Form Least-Squares Line Coefficients

Least Squares and QR Factorization · Tutorial 691 of 1000

Closed-Form Least-Squares Line Coefficients

Derive the intercept and slope formulas from the normal equations, and understand how centering the data simplifies the calculation.

Intermediate 10 min read

What You'll Learn

  • Write the two normal equations for a line with an intercept and slope.
  • Derive the slope from centered input and output values.
  • Recover the intercept using the sample means.
  • Identify exactly when the least-squares line coefficients are unique.
  • Check fitted coefficients using residual orthogonality.
  • Recognize why an uncentered slope formula can give the wrong answer.

From a Best-Fit Line to Two Equations

Suppose we have \(n\) data points \((x_1,y_1),\dots,(x_n,y_n)\), where each \(x_i\) and \(y_i\) is real. We want a line of the form \(y=a+bx\), with intercept \(a\) and slope \(b\), that fits the observed values as closely as possible in the least-squares sense. That means choosing \(a\) and \(b\) to minimize the sum of squared residuals, where a residual is the observed value minus its fitted value.

In matrix form, the design matrix has a column of ones for the intercept and a column of input values for the slope. The least-squares normal equations state that the residual vector is orthogonal to both columns. Here this gives two scalar equations. We will solve them and express the result using the sample means and centered data. The key condition is that the inputs cannot all be equal if the slope and intercept are to be determined uniquely.

Definition: For \(n\ge 1\) observations \((x_i,y_i)\), a least-squares line is a line \(y=a+bx\) whose coefficients minimize \(\sum_{i=1}^{n}(y_i-a-bx_i)^2\) over all real \(a,b\). Its residual at observation \(i\) is \(r_i=y_i-a-bx_i\). The sample means are \(\bar{x}=\frac{1}{n}\sum_{i=1}^{n}x_i\) and \(\bar{y}=\frac{1}{n}\sum_{i=1}^{n}y_i\).

Write the design matrix as \(\mathbf{A}=\begin{bmatrix}1&x_1\\ \vdots&\vdots\\1&x_n\end{bmatrix}\), the coefficient vector as \(\begin{bmatrix}a\\b\end{bmatrix}\), and the data vector as \(\begin{bmatrix}y_1\\ \vdots\\y_n\end{bmatrix}\). The two columns of \(\mathbf{A}\) are the vector of ones and the input vector. The normal equations say that \(\mathbf{A}^{\top}\mathbf{r}=\mathbf{0}\). In terms of the residuals, this is \(\sum_i r_i=0\) and \(\sum_i x_i r_i=0\).

$$ \begin{aligned} na+b\sum_{i=1}^{n}x_i&=\sum_{i=1}^{n}y_i,\\ a\sum_{i=1}^{n}x_i+b\sum_{i=1}^{n}x_i^2&=\sum_{i=1}^{n}x_i y_i. \end{aligned} $$

The first equation expresses that the residuals sum to zero; the second expresses that the residuals have zero dot product with the input values. Both are needed: solving only the first equation chooses an intercept for a fixed slope, but does not by itself determine the least-squares slope.

Why the Normal Equations Characterize a Least-Squares Fit

The normal equations are useful because they characterize minimizers, not merely possible coefficients. The following argument also explains why checking residual orthogonality at the end of a calculation is a meaningful check.

Theorem: Let \(\mathbf{A}\in\mathbb{R}^{n\times 2}\), let \(\mathbf{y}\in\mathbb{R}^n\), and define the residual \(\mathbf{r}=\mathbf{y}-\mathbf{A}\mathbf{c}\) for a coefficient vector \(\mathbf{c}\in\mathbb{R}^2\). Then \(\mathbf{c}\) minimizes \(\|\mathbf{y}-\mathbf{A}\mathbf{d}\|^2\) over all \(\mathbf{d}\in\mathbb{R}^2\) if and only if \(\mathbf{A}^{\top}\mathbf{r}=\mathbf{0}\).

Proof. First suppose \(\mathbf{A}^{\top}\mathbf{r}=\mathbf{0}\). For any other coefficient vector \(\mathbf{d}\), write \(\mathbf{h}=\mathbf{d}-\mathbf{c}\). Its residual is \(\mathbf{y}-\mathbf{A}\mathbf{d}=\mathbf{r}-\mathbf{A}\mathbf{h}\). Expanding the squared norm gives

$$ \|\mathbf{r}-\mathbf{A}\mathbf{h}\|^2 =\|\mathbf{r}\|^2-2\mathbf{r}^{\top}\mathbf{A}\mathbf{h} +\|\mathbf{A}\mathbf{h}\|^2 =\|\mathbf{r}\|^2+\|\mathbf{A}\mathbf{h}\|^2 \ge \|\mathbf{r}\|^2, $$

because \(\mathbf{r}^{\top}\mathbf{A}\mathbf{h}=(\mathbf{A}^{\top}\mathbf{r})^{\top}\mathbf{h}=0\). Thus \(\mathbf{c}\) minimizes the squared residual norm. Conversely, suppose \(\mathbf{c}\) is a minimizer. If there were an \(\mathbf{h}\) with \(z=\mathbf{r}^{\top}\mathbf{A}\mathbf{h}\ne0\), then \(\mathbf{A}\mathbf{h}\ne\mathbf{0}\). For any real \(t\), the coefficient vector \(\mathbf{c}+t\mathbf{h}\) has squared residual norm \(\|\mathbf{r}\|^2-2tz+t^2\|\mathbf{A}\mathbf{h}\|^2\). Choosing \(t=z/\|\mathbf{A}\mathbf{h}\|^2\) makes this \(\|\mathbf{r}\|^2-z^2/\|\mathbf{A}\mathbf{h}\|^2\), which is strictly smaller than \(\|\mathbf{r}\|^2\). This contradicts minimality. Therefore \(\mathbf{r}^{\top}\mathbf{A}\mathbf{h}=0\) for every \(\mathbf{h}\in\mathbb{R}^2\), which means \(\mathbf{A}^{\top}\mathbf{r}=\mathbf{0}\). \(\square\)

Centering the Data Reveals the Slope

To solve the two normal equations, first rewrite their right sides using the means. The first equation gives \(a=\bar{y}-b\bar{x}\). Substitute this expression for \(a\) into the second equation. The resulting equation isolates the slope and shows why deviations from the mean, rather than the raw inputs alone, govern its value.

$$ \begin{aligned} (\bar{y}-b\bar{x})\sum_{i=1}^{n}x_i +b\sum_{i=1}^{n}x_i^2 &=\sum_{i=1}^{n}x_i y_i,\\ n\bar{x}\bar{y} +b\left(\sum_{i=1}^{n}x_i^2-n\bar{x}^{\,2}\right) &=\sum_{i=1}^{n}x_i y_i. \end{aligned} $$

Since \(\sum_i x_i=n\bar{x}\), the quantity multiplying \(b\) is \(\sum_i(x_i-\bar{x})^2\). The remaining difference on the right is \(\sum_i(x_i-\bar{x})(y_i-\bar{y})\). These identities follow by expanding the products and using \(\sum_i(x_i-\bar{x})=0\) and \(\sum_i(y_i-\bar{y})=0\).

Theorem: If the inputs \(x_1,\dots,x_n\) are not all equal, the unique least-squares line coefficients are $$ b=\frac{\sum_{i=1}^{n}(x_i-\bar{x})(y_i-\bar{y})} {\sum_{i=1}^{n}(x_i-\bar{x})^2}, \qquad a=\bar{y}-b\bar{x}. $$ If all inputs equal a value \(c\), the least-squares fitted value at every observation is \(\bar{y}\), and the coefficient pairs that attain it are exactly those satisfying \(a+bc=\bar{y}\).

Proof. Set \(S_{xx}=\sum_i(x_i-\bar{x})^2\) and \(S_{xy}=\sum_i(x_i-\bar{x})(y_i-\bar{y})\). The second normal equation minus \(\bar{x}\) times the first is \(bS_{xx}=S_{xy}\), by the identities above. Each square in \(S_{xx}\) is nonnegative, and \(S_{xx}=0\) exactly when every \(x_i=\bar{x}\), which is exactly when all inputs are equal. If they are not all equal, \(S_{xx}>0\), so \(b=S_{xy}/S_{xx}\) is forced, and the first normal equation then forces \(a=\bar{y}-b\bar{x}\). These coefficients satisfy both equations, so the preceding theorem shows they give a least-squares fit. The equations force both coefficients, proving uniqueness. If instead every \(x_i=c\), then every fitted value is the same number \(q=a+bc\). For any such \(q\), direct expansion gives \(\sum_i(y_i-q)^2=\sum_i(y_i-\bar{y})^2+n(q-\bar{y})^2\), because \(\sum_i(y_i-\bar{y})=0\). This is minimized exactly when \(q=\bar{y}\). Thus the minimizing coefficient pairs are precisely those with \(a+bc=\bar{y}\); there are multiple such pairs, and the slope is not uniquely determined. \(\square\)

The numerator in the slope formula is a centered cross-product sum: it compares each input’s deviation from its mean with the corresponding output’s deviation from its mean. The denominator measures the spread of the inputs about their mean. The intercept then places the fitted line through the point \((\bar{x},\bar{y})\), since \(a+b\bar{x}=\bar{y}\). This does not mean the line passes through every data point; it identifies a particular point the least-squares line must pass through when the coefficients are unique.

Worked Calculations

Worked Example: Solving the Normal Equations Directly

Find the least-squares line for the observations \((0,1),(1,2),(2,2)\). First compute the sums needed in the normal equations: \(n=3\), \(\sum x_i=0+1+2=3\), \(\sum y_i=1+2+2=5\), \(\sum x_i^2=0+1+4=5\), and \(\sum x_i y_i=0+2+4=6\). Thus the equations are

$$ 3a+3b=5,\qquad 3a+5b=6. $$

Subtract the first equation from the second to get \(2b=1\), so \(b=\frac12\). Substitute into the first: \(3a+\frac32=5\), hence \(3a=\frac72\) and \(a=\frac76\). Therefore the line is \(y=\frac76+\frac12x\).

Check: The coefficients satisfy the two equations: \(3\left(\frac76\right)+3\left(\frac12\right)=\frac72+\frac32=5\), and \(3\left(\frac76\right)+5\left(\frac12\right)=\frac72+\frac52=6\). The residuals are \(-\frac16,\frac13,-\frac16\). Their sum is \(0\), and their input-weighted sum is \(0(-\frac16)+1(\frac13)+2(-\frac16)=0\), as required.

Worked Example: Using Centered Data

Consider \((-2,1),(-1,2),(1,5),(2,4)\). The input mean is \(\bar{x}=(-2-1+1+2)/4=0\), and the output mean is \(\bar{y}=(1+2+5+4)/4=3\). The centered input deviations are \(-2,-1,1,2\), and the centered output deviations are \(-2,-1,2,1\). Therefore

$$ S_{xy}=(-2)(-2)+(-1)(-1)+(1)(2)+(2)(1)=9, \qquad S_{xx}=4+1+1+4=10. $$

It follows that \(b=\frac9{10}\) and \(a=3-\frac9{10}\cdot0=3\). The least-squares line is \(y=3+\frac9{10}x\).

Check: Here \(\sum x_i=0\), \(\sum y_i=12\), \(\sum x_i^2=10\), and \(\sum x_i y_i=-2-2+5+8=9\). The normal equations become \(4a=12\) and \(10b=9\), which the stated coefficients satisfy. The residuals are \(-\frac15,-\frac1{10},\frac{11}{10},-\frac45\); their sum is \(0\), and their input-weighted sum is \(\frac25+\frac1{10}+\frac{11}{10}-\frac85=0\).

Worked Example: Equal Inputs Do Not Determine a Slope

Take the observations \((3,1),(3,2),(3,4)\). The means are \(\bar{x}=3\) and \(\bar{y}=\frac73\). Since every input equals \(3\), the centered denominator is \(S_{xx}=0\), so the slope formula with a denominator cannot be used. The normal-equation sums are \(\sum x_i=9\), \(\sum y_i=7\), \(\sum x_i^2=27\), and \(\sum x_i y_i=21\). Hence

$$ 3a+9b=7,\qquad 9a+27b=21. $$

The second equation is three times the first, so the equations determine only \(a+3b=\frac73\), not a unique pair. For instance, \(a=\frac73,b=0\) gives fitted values \(\frac73,\frac73,\frac73\). Any other pair satisfying \(a+3b=\frac73\) gives the same fitted values for these three inputs.

Check: For the displayed choice, the residuals are \(-\frac43,-\frac13,\frac53\). They sum to \(0\), and their input-weighted sum is \(3(-\frac43-\frac13+\frac53)=0\). The fitted value is the sample mean, as the theorem predicts.

A Common Mistake: Using Raw Inputs in the Slope

When a line includes an intercept, the slope is not generally \(\sum_i x_i y_i/\sum_i x_i^2\). That uncentered ratio omits the effect of the intercept. In the first worked example, it would give \(b=6/5\), since \(\sum_i x_i y_i=6\) and \(\sum_i x_i^2=5\), rather than the correct slope \(1/2\). If we then use the mean-based intercept formula, we obtain \(a=5/3-(6/5)(1)=7/15\). The residuals for this incorrect line are \(\frac8{15},\frac5{15},-\frac{13}{15}\). They sum to zero, but their input-weighted sum is \(\frac5{15}-\frac{26}{15}=-\frac75\ne0\). Thus the second normal equation fails: fitting the intercept correctly for a chosen slope does not make that slope least-squares.

Use the centered formula when the model includes an intercept. The denominator is positive precisely when the inputs vary, and the numerator uses deviations of both variables from their sample means. If the inputs do not vary, report that the slope and intercept are not individually determined; the fitted value at the observed input is determined, but infinitely many coefficient pairs can represent it.

Check Your Understanding

Use the normal equations and centered formulas where applicable. For each proposed fit, check the residual sums.

  1. For \((0,0),(1,1),(2,3)\), calculate \(\bar{x}\), \(\bar{y}\), \(S_{xx}\), and \(S_{xy}\), then find \(a\) and \(b\).
  2. Explain why the slope formula cannot be applied when every input is the same value.
  3. For inputs \(1,2,4\) and outputs \(3,2,5\), write the two normal equations before solving them.
  4. State the two residual orthogonality equations that a least-squares line must satisfy.
  5. For data with \(\bar{x}=0\) and \(\bar{y}=4\), what does the intercept formula imply about \(a\)?

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