Hexa Scholar

Least squares: which line, and why squares

Six gold pegs on a navy board, three above and three below a slanted wooden rod, each connected to it by a vertical spring.
Vertical springs connect six pegs to a slanted wooden rod, like vertical misses connecting points to a line.

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. 1 SeeDrag a line to shrink the squares.
  2. 2 TryPredict what the squares decide.
  3. 3 ProveBuild the proof, line by line.
  4. 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 ideas

A 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:

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.

Line:

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.

1 See

Fit a line by hand

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.

2 Try

What the squares decide

A prediction first. The explorer below it opens once it is answered.

Which misses?

3 Prove

Why exactly one line is best

Theorem

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.

What else follows
  • 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.
4 Apply

Problems

Each problem has hints and a full solution.

Key point For a least-squares line, compute the means, then \(S_{uu}\) and \(S_{uv}\): \(\hat{b} = S_{uv}/S_{uu}\), and the line passes through \((\bar{x}, \bar{y})\). From summary numbers, \(\hat{b} = r \cdot \operatorname{SD}(y)/\operatorname{SD}(x)\), and the prediction at \(x\) is \(\bar{y} + \hat{b}(x - \bar{x})\).
Level Intro Prerequisites covariance and correlation (the lesson on covariance and correlation), means and slopes (refreshed in Before you start) Time ~35 min

Further reading