Least squares: which line, and why squares
The question
Six points, one straight line. Where does the line go so that the squared vertical misses add up to as little as possible?
Drag the line yourself; the page keeps score.
- 1 SeeDrag a line to shrink the squares.
- 2 TryPredict what the squares decide.
- 3 ProveBuild the proof, line by line.
- 4 ApplyProblems to check yourself.
Before you start · 3 quick checks on a mean, the slope of a line and reading a correlation, about 3 min
Two ideas before you start
2 ideasA residual is a vertical miss
Figure: the four points (1, 1), (2, 3), (3, 2) and (4, 4), a dashed line, and on each point a shaded square whose side is the vertical miss from the point to the line. It needs JavaScript.
Line: a = 1 b = 0.5 misses −0.5, 1, −0.5, 1
SSE = 0.25 + 1 + 0.25 + 1 = 2.5
The miss of a point is how far it lies above the line, measured straight up (negative when it lies below). Least squares scores a line by adding up the squared misses, the areas of the squares.
Exact definition
Points and lines. Take \(n \ge 2\) points \((x_1, y_1), \dots, (x_n, y_n)\) whose \(x_i\) are not all equal. Sums \(\sum_i\) run over the points, \(i = 1, \dots, n\). For a line \(y = a + bx\), the residual of point \(i\) is \(e_i = y_i - a - bx_i\), and the sum of squared residuals is
\[\operatorname{SSE}(a, b) = \sum_i e_i^2.\]A least-squares line is a line with the smallest \(\operatorname{SSE}\) among all lines.
Means and deviations. The means are \(\bar{x} = \tfrac{1}{n}\sum_i x_i\) and \(\bar{y} = \tfrac{1}{n}\sum_i y_i\), and the deviations are \(u_i = x_i - \bar{x}\) and \(v_i = y_i - \bar{y}\). Three sums of products of deviations appear below:
\[\begin{aligned} S_{uu} &= \sum_i u_i^2, \\ S_{uv} &= \sum_i u_iv_i, \\ S_{vv} &= \sum_i v_i^2. \end{aligned}\]\(S_{uu} \gt 0\) because the \(x_i\) are not all equal. The correlation of the points, from the lesson on covariance and correlation, is \(r = S_{uv}/\sqrt{S_{uu}S_{vv}}\) when also \(S_{vv} \gt 0\).
Squares cannot cancel
Figure: the same four points, a dashed line and the vertical misses as segments. It needs JavaScript.
Sum of misses = 0 + 1 − 1 + 0 = 0
Sum of squares = 2
Misses above and below a line can cancel: all three lines have misses that add up to 0, even the one that falls while the points rise. Squares are never negative, so a bad line always pays.
Exact definition
Why a sum of squares. The plain sum of the residuals \(\sum_i e_i\) can be 0 while the residuals themselves are large, as for the three lines of this card. Each square \(e_i^2\) is at least 0, so \(\operatorname{SSE}(a, b) \ge 0\), and \(\operatorname{SSE}(a, b) = 0\) only when every point lies on the line.
Other scores exist, such as the sum of the plain distances \(\sum_i \lvert e_i\rvert\). Stage 2 compares the two, and stage 3 shows that the sum of squares has exactly one best line, given by a formula.
Fine print: names and notation
The least-squares line is also called the fitted line or the regression line of \(y\) on \(x\), and its slope and intercept are written \(\hat{b}\) and \(\hat{a}\). Statistics software reports \(\operatorname{SSE}\) under names such as the residual sum of squares.
The points are data, not random variables: nothing on this page assumes anything about how the data arose. Questions such as how far \(\hat{b}\) would move with new data need a model for the data and are not treated here.
Fit a line by hand
- Drag the two round handles up or down to move the ends of your line, or focus a handle and use the arrow keys.
- Each shaded square has a point's vertical miss as its side. Make the total area as small as you can, then press Check my line.
How the game works
Your line runs from \(x = 0\) to \(x = 10\); each handle sets its height at one end, in steps of 0.1 from 0 to 6. The tiles show the intercept \(a\), the slope \(b\) and the sum of squares \(\operatorname{SSE}(a, b)\) of the line as it is now. Check my line locks it and draws the least-squares line, dashed, with its sum of squares. There are two rounds; after the second a table compares your lines with the best ones. Start over clears everything.
The figure uses the same scale on both axes, so a miss of 1 is drawn as a 1 by 1 square.
| Round | Your sum | Smallest sum | Yours is larger by |
|---|
One best line. Every line gets a score, \(\operatorname{SSE}(a, b)\), and for each cloud exactly one line has the smallest score. Stage 3 proves this and finds the line with a formula.
A flat bottom. Near the best line the score changes slowly: lifting the whole best line by a small amount \(s\) adds only \(ns^2\) to its score, for example \(6 \cdot 0.1^2 = 0.06\) for the six points of round 1. So lines that look different can score almost the same, and the eye cannot find the best line exactly; the formula can.
What the squares decide
A prediction first. The explorer below it opens once it is answered.
Explorer: one flat line through five numbers
Move the flat line \(y = c\) and switch the last number between 13 and 23. The tiles show both scores for the current \(c\).
Goals
For numbers \(y_1, \dots, y_n\) with average \(\bar{y}\) and every number \(c\),
\[\begin{aligned} &\sum_i (y_i - c)^2 \\ &= \sum_i (y_i - \bar{y})^2 + n(\bar{y} - c)^2, \end{aligned}\]so the sum of squared misses is smallest exactly at the average. The sum of plain distances \(\sum_i \lvert y_i - c\rvert\) is smallest at a middle value instead: for an odd \(n\), the middle number of the sorted list; for an even \(n\), every \(c\) between the two middle numbers. For the five numbers of the explorer, moving the largest from 13 to 23 leaves the middle number at 3 and moves the average from 5 to 7. Squares give a formula and one answer, at the price of a strong pull from far values.
Which misses?
Why exactly one line is best
Assumptions. \(n \ge 2\) points \((x_1, y_1), \dots, (x_n, y_n)\) whose \(x_i\) are not all equal. As in the setup, \(u_i = x_i - \bar{x}\), \(v_i = y_i - \bar{y}\), \(S_{uu} = \sum_i u_i^2\), \(S_{uv} = \sum_i u_iv_i\), \(S_{vv} = \sum_i v_i^2\), and for a line \(y = a + bx\), \(e_i = y_i - a - bx_i\) and \(\operatorname{SSE}(a, b) = \sum_i e_i^2\).
Conclusion. Let \(\hat{b} = S_{uv}/S_{uu}\) and \(\hat{a} = \bar{y} - \hat{b}\bar{x}\). The line \(y = \hat{a} + \hat{b}x\) has a smaller \(\operatorname{SSE}\) than every other line, and
\[\begin{aligned} &\operatorname{SSE}(\hat{a}, \hat{b}) \\ &= S_{vv} - \frac{S_{uv}^2}{S_{uu}}. \end{aligned}\]Facts the proof uses.
- the deviations add up to 0: \(\sum_i u_i = \sum_i x_i - n\bar{x} = 0\), and likewise \(\sum_i v_i = 0\);
- \(S_{uu} \gt 0\), because the \(x_i\) are not all equal;
- ordinary algebra of numbers, in each point and for sums;
- a square of a number is at least 0, and it is 0 only when the number is 0.
Each option is one step about the points of the theorem. Pick the one that is true for every set of points the theorem covers and every line \(y = a + bx\). An equation that holds only for some lines cannot be a step of the proof.
- The residuals of the best line. With \(\hat{e}_i = y_i - \hat{a} - \hat{b}x_i\), line 1 with \(c = 0\) gives \(\hat{e}_i = v_i - \hat{b}u_i\), so \(\sum_i \hat{e}_i = 0\) and \(\sum_i u_i\hat{e}_i = S_{uv} - \hat{b}S_{uu} = 0\). The best line passes through the point of means: \(\hat{a} + \hat{b}\bar{x} = \bar{y}\).
- In terms of the correlation. When also \(S_{vv} \gt 0\), the correlation \(r = S_{uv}/\sqrt{S_{uu}S_{vv}}\) of the lesson on covariance and correlation gives \(\hat{b} = r\sqrt{S_{vv}/S_{uu}}\) and \(\operatorname{SSE}(\hat{a}, \hat{b}) = (1 - r^2)S_{vv}\): the slope is \(r\) times the ratio of the standard deviations of \(y\) and \(x\).
- A flat line. With \(b = 0\) fixed, lines 2 and 3 give \(\operatorname{SSE}(a, 0) = S_{vv} + n(\bar{y} - a)^2\), smallest at \(a = \bar{y}\): the average of stage 2.
- A line through 0. With \(a = 0\) fixed and not all \(x_i\) equal to 0, \[\begin{aligned} &\operatorname{SSE}(0, b) \\ &= \sum_i y_i^2 - 2b\sum_i x_iy_i \\ &\quad {} + b^2\sum_i x_i^2, \end{aligned}\] and the same completing of the square gives the best \(b = \sum_i x_iy_i / \sum_i x_i^2\). Write the \(x_i\) and the \(y_i\) as two vectors: this \(b\) is the number that gives the point of the line through 0 along the first vector closest to the second, as in the lesson on the dot product and projection.
Problems
Each problem has hints and a full solution.
Further reading
- D. Freedman, R. Pisani and R. Purves, Statistics, 4th ed. (W. W. Norton, 2007), part III, "Correlation and Regression," chapters 10 to 12 on the regression line and its errors.
- G. Strang, Introduction to Linear Algebra, 5th ed. (Wellesley-Cambridge Press, 2016), section 4.3, "Least Squares Approximations."
- S. M. Stigler, The History of Statistics (Belknap Press, 1986), ch. 1, on how least squares was first proposed and used.