Skip to content

Least squares

Fit a line to points no line passes through, see the fit as a shadow, and add a column to fit a parabola.

Helps to know firstProjections

Helps to know first
This topic

First, the picture

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).

intercept, slope
–
sum of squared misses
–
0.00 / 10.50 s
Describe this picture

Two panels (stacked on a phone). On the left, a plot of three points, (0,1)(0,1), (1,3)(1,3) and (2,2)(2,2). A line settles through them, from flat at their average height to y=1.5+0.5ty=1.5+0.5t, and three vertical segments join each point to the line, labelled with the misses, −0.5-0.5, 11 and −0.5-0.5. On the right, a slowly turning view of space with axes y1y_1, y2y_2, y3y_3. The three heights are one point there, y=(1,3,2)\mathbf{y}=(1,3,2). A tilted plane through the origin is spanned by the arrows (1,1,1)(1,1,1) and (0,1,2)(0,1,2). A drop then grows from y\mathbf{y} to the plane and lands at p=(1.5,2,2.5)\mathbf{p}=(1.5,2,2.5), 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 e\mathbf{e}. Readouts give the intercept and slope, (1.5,0.5)(1.5,0.5), 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 y=c0+c1ty=c_0+c_1t through the three points (0,1)(0,1), (1,3)(1,3) and (2,2)(2,2) would need

c0+0c1=1,c0+1c1=3,c0+2c1=2.that isAx=y,A=[101112], x=[c0c1], y=[132].\begin{gathered} \begin{aligned} c_0+0c_1&=1,\\ c_0+1c_1&=3,\\ c_0+2c_1&=2. \end{aligned} \qquad\text{that is}\qquad \mathbf{A}\mathbf{x}=\mathbf{y},\\ \mathbf{A}=\begin{bmatrix}1&0\\1&1\\1&2\end{bmatrix},\ \mathbf{x}=\begin{bmatrix}c_0\\c_1\end{bmatrix},\ \mathbf{y}=\begin{bmatrix}1\\3\\2\end{bmatrix}. \end{gathered}

Three equations, two unknowns, and no solution: the first two give c0=1c_0=1, c1=2c_1=2, and then the third says 5=25=2. In the language of Two pictures of a linear system, y\mathbf{y} is not a mix of the columns of A\mathbf{A}.

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:

min⁡x ∥y−Ax∥2.\min_{\mathbf{x}}\ \lVert\mathbf{y}-\mathbf{A}\mathbf{x}\rVert^2 .

That is the least squares line. The misses are measured vertically, because the error is in the heights, not in tt. 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 y\mathbf{y} in space. Every line c0+c1tc_0+c_1t gives three heights Ax=c0 (1,1,1)+c1 (0,1,2)\mathbf{A}\mathbf{x}=c_0\,(1,1,1)+c_1\,(0,1,2), and all such points form a plane, the column space C(A)C(\mathbf{A}). The sum of the squared misses is the squared distance from y\mathbf{y} to one of those points.

So the best line is the nearest point of the plane to y\mathbf{y}. That is the projection of Projections: drop from y\mathbf{y} at a right angle. Its weights x^\hat{\mathbf{x}} solve the normal equations,

A⊤A x^=A⊤y.\mathbf{A}^\top\mathbf{A}\,\hat{\mathbf{x}}=\mathbf{A}^\top\mathbf{y}.

The weights are the intercept and the slope. No calculus is needed: the right angle does the work.

Fitting the line

Here A⊤A=[3335]\mathbf{A}^\top\mathbf{A}=\begin{bmatrix}3&3\\3&5\end{bmatrix}: 3 points, the tt values add to 3, their squares add to 5. And A⊤y=(6,7)\mathbf{A}^\top\mathbf{y}=(6,7): the heights add to 6, and 0⋅1+1⋅3+2⋅2=70\cdot1+1\cdot3+2\cdot2=7. So

3c0+3c1=6,3c0+5c1=7,\begin{aligned} 3c_0+3c_1&=6,\\ 3c_0+5c_1&=7, \end{aligned}

which gives c1=0.5c_1=0.5 and c0=1.5c_0=1.5. The line is y=1.5+0.5ty=1.5+0.5t. Its heights are p=(1.5,2,2.5)\mathbf{p}=(1.5,2,2.5) and the misses are e=y−p=(−0.5,1,−0.5)\mathbf{e}=\mathbf{y}-\mathbf{p}=(-0.5,1,-0.5).

Here e\mathbf{e} is the list of misses, called the residuals. Signal processing writes an error signal e[n]e[n]; this bold e\mathbf{e} is the same idea as a vector.

After the clip, drag the middle point up by 1. The slope stays 0.50.5 and the intercept rises to 11/6=1.83311/6=1.833. The middle point sits at the mean of the tt values, so raising it lifts the line without tilting it.

The residuals

The residuals are at a right angle to every column, A⊤e=0\mathbf{A}^\top\mathbf{e}=\mathbf{0}. The first column is all ones, so the residuals add up to zero: −0.5+1−0.5=0-0.5+1-0.5=0. The second column is t=(0,1,2)\mathbf{t}=(0,1,2), and t⋅e=0+1−1=0\mathbf{t}\cdot\mathbf{e}=0+1-1=0. Any fit with a constant term has residuals that add up to zero.

The right angle also splits the lengths: ∥y∥2=14=12.5+1.5\lVert\mathbf{y}\rVert^2=14=12.5+1.5. 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 A\mathbf{A} point in different directions. If every tt were the same, the two columns would lie on one line, A⊤A\mathbf{A}^\top\mathbf{A} 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 c0+c1t+c2t2c_0+c_1t+c_2t^2, add a column of t2t^2. Take four points, t=(−1,0,1,2)t=(-1,0,1,2) and y=(2,1,2,6)\mathbf{y}=(2,1,2,6), 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).

fit
–
sum of squared misses
–
0.00 / 9.80 s
Describe this picture

A plot of four points, (−1,2)(-1,2), (0,1)(0,1), (1,2)(1,2) and (2,6)(2,6). First the best line, y=2.1+1.3ty=2.1+1.3t, draws from left to right, and four vertical segments join the points to it, labelled with the misses 1.21.2, −1.1-1.1, −1.4-1.4 and 1.31.3. A readout names the fit, “line”, and gives the sum of the squared misses, 6.30. Then the line bends into the best parabola, y=0.85+0.05t+1.25t2y=0.85+0.05t+1.25t^2, leaving a faint dashed copy of itself behind; the misses shrink to −0.05-0.05, 0.150.15, −0.15-0.15 and 0.050.05, and the readouts change to “parabola” and 0.05. The picture has no control.

For the line, the normal equations are [4226]x^=(11,12)\begin{bmatrix}4&2\\2&6\end{bmatrix}\hat{\mathbf{x}}=(11,12), so x^=(2.1,1.3)\hat{\mathbf{x}}=(2.1,1.3). The residuals are (1.2,−1.1,−1.4,1.3)(1.2,-1.1,-1.4,1.3): they add up to 0, and their squares add up to 6.306.30. As before, ∥y∥2=45=38.7+6.3\lVert\mathbf{y}\rVert^2=45=38.7+6.3.

For the parabola, the normal equations are

[4262686818]x^=[111228],x^=(0.85, 0.05, 1.25).\begin{bmatrix}4&2&6\\2&6&8\\6&8&18\end{bmatrix}\hat{\mathbf{x}}=\begin{bmatrix}11\\12\\28\end{bmatrix}, \qquad \hat{\mathbf{x}}=(0.85,\ 0.05,\ 1.25).

The residuals are (−0.05,0.15,−0.15,0.05)(-0.05,0.15,-0.15,0.05), and their squares add up to only 0.050.05. The extra column gave the fit room to bend.

More columns are not always better. With three points and three unknowns, the parabola 1+3.5t−1.5t21+3.5t-1.5t^2 passes through (0,1)(0,1), (1,3)(1,3) and (2,2)(2,2) exactly: zero error. But a curve that bends to hit every point also follows every bit of noise in them.

Worked example

  1. A line through three points. (0,1)(0,1), (1,3)(1,3), (2,2)(2,2): A⊤A=[3335]\mathbf{A}^\top\mathbf{A}=\begin{bmatrix}3&3\\3&5\end{bmatrix}, A⊤y=(6,7)\mathbf{A}^\top\mathbf{y}=(6,7), so y=1.5+0.5ty=1.5+0.5t.
  2. Residuals. (−0.5,1,−0.5)(-0.5,1,-0.5); they add up to 0; their squares add up to 1.5.
  3. A check from statistics. The slope is also ∑(t−tˉ)(y−yˉ)/∑(t−tˉ)2=1/2\sum(t-\bar t)(y-\bar y)\big/\sum(t-\bar t)^2=1/2, and the intercept is yˉ−0.5 tˉ=2−0.5=1.5\bar y-0.5\,\bar t=2-0.5=1.5.
  4. Two points. A line through two points has error 0: two equations, two unknowns.
  5. Four points, line and parabola. Errors 6.30 and 0.05.
  6. Predict. Raise the middle of the three points by 1: slope 0.50.5, intercept 1.8331.833.

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

QuantityFormulaNotes
Goalmin⁡x∥y−Ax∥2\min_{\mathbf{x}}\lVert\mathbf{y}-\mathbf{A}\mathbf{x}\rVert^2vertical misses, squared
Normal equationsA⊤A x^=A⊤y\mathbf{A}^\top\mathbf{A}\,\hat{\mathbf{x}}=\mathbf{A}^\top\mathbf{y}one answer if the columns are independent
Fitp=Ax^\mathbf{p}=\mathbf{A}\hat{\mathbf{x}}the projection of y\mathbf{y}
Residualse=y−p\mathbf{e}=\mathbf{y}-\mathbf{p}, A⊤e=0\mathbf{A}^\top\mathbf{e}=\mathbf{0}add up to 0 with a constant column
Linecolumns 1\mathbf{1} and t\mathbf{t}weights: intercept, slope
Parabolaadd the column t2t^2more columns, smaller error
Lengths∥y∥2=∥p∥2+∥e∥2\lVert\mathbf{y}\rVert^2=\lVert\mathbf{p}\rVert^2+\lVert\mathbf{e}\rVert^2the right angle

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look