Three points, and no straight line passes through all three. Which line comes closest? Watch the answer appear twice.
Least squares is a shadow
Three points and their best line, and the same fit in the space of heights.
Three points, (0, 1), (1, 3) and (2, 2).
Describe this picture
Two panels (stacked on a phone). On the left, a plot of three points, , and . A line settles through them, from flat at their average height to , and three vertical segments join each point to the line, labelled with the misses, , and . On the right, a slowly turning view of space with axes , , . The three heights are one point there, . A tilted plane through the origin is spanned by the arrows and . A drop then grows from to the plane and lands at , the line’s three heights, which are marked on the line on the left at the same moment; a small square marks the right angle, and the drop is labelled . Readouts give the intercept and slope, , and the sum of the squared misses, 1.5. When the clip has finished, any of the three points on the left can be dragged up or down, or moved with the arrow keys in steps of 0.5. Both pictures follow.
No line passes through all three
A line through the three points , and would need
Three equations, two unknowns, and no solution: the first two give , , and then the third says . In the language of Two pictures of a linear system, is not a mix of the columns of .
So we ask for less. Every line misses each point by some amount, measured straight up or down. We choose the line that makes the sum of the squared misses as small as possible:
That is the least squares line. The misses are measured vertically, because the error is in the heights, not in . The fitted line is not the one with the shortest right-angle distances to the points.
The fit is a shadow
Now the right panel. The three heights are one point in space. Every line gives three heights , and all such points form a plane, the column space . The sum of the squared misses is the squared distance from to one of those points.
So the best line is the nearest point of the plane to . That is the projection of Projections: drop from at a right angle. Its weights solve the normal equations,
The weights are the intercept and the slope. No calculus is needed: the right angle does the work.
Fitting the line
Here : 3 points, the values add to 3, their squares add to 5. And : the heights add to 6, and . So
which gives and . The line is . Its heights are and the misses are .
Here is the list of misses, called the residuals. Signal processing writes an error signal ; this bold is the same idea as a vector.
After the clip, drag the middle point up by 1. The slope stays and the intercept rises to . The middle point sits at the mean of the values, so raising it lifts the line without tilting it.
The residuals
The residuals are at a right angle to every column, . The first column is all ones, so the residuals add up to zero: . The second column is , and . Any fit with a constant term has residuals that add up to zero.
The right angle also splits the lengths: . The fitted heights carry 12.5 and the residuals 1.5, the smallest sum of squared misses any line can reach.
The normal equations have exactly one answer when the columns of point in different directions. If every were the same, the two columns would lie on one line, would have no inverse, and the slope would be undefined: through points stacked on one vertical line, no line can pick a slope.
A parabola: add a column
The method does not care what the columns are. To fit a parabola , add a column of . Take four points, and , and compare a line with a parabola.
Line or parabola
Four points: the best line, then the best parabola.
Four points: (−1, 2), (0, 1), (1, 2) and (2, 6).
Describe this picture
A plot of four points, , , and . First the best line, , draws from left to right, and four vertical segments join the points to it, labelled with the misses , , and . A readout names the fit, “line”, and gives the sum of the squared misses, 6.30. Then the line bends into the best parabola, , leaving a faint dashed copy of itself behind; the misses shrink to , , and , and the readouts change to “parabola” and 0.05. The picture has no control.
For the line, the normal equations are , so . The residuals are : they add up to 0, and their squares add up to . As before, .
For the parabola, the normal equations are
The residuals are , and their squares add up to only . The extra column gave the fit room to bend.
More columns are not always better. With three points and three unknowns, the parabola passes through , and exactly: zero error. But a curve that bends to hit every point also follows every bit of noise in them.
Worked example
- A line through three points. , , : , , so .
- Residuals. ; they add up to 0; their squares add up to 1.5.
- A check from statistics. The slope is also , and the intercept is .
- Two points. A line through two points has error 0: two equations, two unknowns.
- Four points, line and parabola. Errors 6.30 and 0.05.
- Predict. Raise the middle of the three points by 1: slope , intercept .
Where you’ll meet this
A sensor is calibrated by feeding it known inputs and fitting a line to its raw readings; the slope is its gain and the intercept its offset. A satellite navigation receiver solves for four unknowns, its position in three directions and its clock error, from every satellite it can hear, often eight or more equations, by repeated least squares. The trend line a spreadsheet draws through a chart is this fit.
In signal processing · normal equations
Optimal FIR design designs a filter by least squares, with the normal equations of this page and a weight on each frequency. Special FIR filters fits a parabola to each window of samples by least squares to take a smooth slope.
Where this leads · regression
The fitted line is the regression line of Linear regression in the probability and statistics course, and item 3 above is its formula for the slope.
The normal equations can lose accuracy when the columns are nearly parallel. QR factorisation solves the same problem a safer way.
Reference card
| Quantity | Formula | Notes |
|---|---|---|
| Goal | vertical misses, squared | |
| Normal equations | one answer if the columns are independent | |
| Fit | the projection of | |
| Residuals | , | add up to 0 with a constant column |
| Line | columns and | weights: intercept, slope |
| Parabola | add the column | more columns, smaller error |
| Lengths | the right angle |