From a Straight Line to Backpropagation
Measuring How Wrong a Straight Line Is, on Twelve Points
Twelve measurements, two candidate lines and the arithmetic that scores them, with the totals 12 and 24 worked out point by point and the choice of squaring argued rather than assumed.
Twelve measurements arrive, one output for each input from one to twelve, and
the question is which straight line fits them. As it stands the question cannot
be answered, because fitting is not a quantity. Two people can draw different
lines through the same twelve points, both be satisfied, and have no way of
arguing with each other.
So the first piece of work is not a line. It is a number, a score that any
candidate line can be fed into, which comes out larger when the line misses by
more. Once that number exists the question is arithmetic, and everything built
later is machinery for making it small.
| step | input x | measurement y |
|---|---|---|
| 1 | 1 | 6 |
| 2 | 2 | 7 |
| 3 | 3 | 10 |
| 4 | 4 | 15 |
| 5 | 5 | 18 |
| 6 | 6 | 19 |
| 7 | 7 | 22 |
| 8 | 8 | 27 |
| 9 | 9 | 30 |
| 10 | 10 | 31 |
| 11 | 11 | 34 |
| 12 | 12 | 39 |
The twelve measurements
Write for the th input and for the measurement taken at it, with
running from 1 to and . These twelve pairs are the only data in
play, and every number below is computed from them.
Five quantities built out of the table get used again and again, so they are
worth computing once and carefully. The inputs add to
. Their squares add to
. The
measurements add to
. The twelve
products are 6, 14, 30, 60, 90, 114, 154, 216, 270, 310, 374 and 468,
and they add to 2,106. Dividing the two plain sums by twelve gives the mean
input and the mean measurement
. Those five sums are the whole of what is ever needed
about the twelve points, since two different sets of measurements sharing them
are scored identically by everything below, and that is the first hint that the
method is cheap: a thirteenth measurement costs five additions and no further
work at all.
A line, and what it misses
A candidate line is two numbers. Write for its slope and for its
height where the input is zero, so the line's prediction at input is
. The prediction at the th input is written ,
and the amount the line misses that measurement by is the residual
The residual is signed. It is positive where the measurement sits above the
line and negative where it sits below, and that sign is the reason the twelve
residuals cannot simply be added. Take the flat line , , which
sits at the height of the mean measurement. Its residuals are
at the first point and at the last, and
they add to exactly. A line that is
17.5 away from one of the points has scored zero on the raw sum. The squared
total for that same flat line is 1,299, computed below, which is over a hundred
times the best line's score, so the raw sum of residuals is not a weak measure
of wrongness but a measure of something else entirely.
Destroying the sign before adding is therefore compulsory, and squaring does
it.
- every measurement contributes, so a line cannot be made to look good by ignoring the points it misses
- the residual at one point, the measurement minus the prediction, still carrying its sign
- the choice the rest of this page argues about. Squaring throws the sign away, and it makes a gap of two cost four times a gap of one rather than twice as much
The squared total for the flat line can now be done properly. The squares of
the measurements add to
,
and expanding each squared residual gives
,
which is .
Two more names, both needed later. The mean squared error is , the
total divided by twelve, which makes scores comparable when the number of
points changes. Its square root is the typical size of a single miss, in the
same units as the measurements.
Two candidate lines, scored
The first candidate is , , the line . The second is
drawn the way a person with a ruler draws one, through the first and last
measurements. Its slope is , and matching the
first point gives , so and the line is
. It is a defensible candidate, and it turns out to pass exactly
through six of the twelve measurements rather than only two.
| step | x | measured y | residual under y = 3x + 2 | residual under y = 3x + 3 | what happened |
|---|---|---|---|---|---|
| 1 | 1 | 6 | 1 | 0 | the second line was built to be exact here |
| 2 | 2 | 7 | -1 | -2 | the first line is one high, the second two high |
| 3 | 3 | 10 | -1 | -2 | |
| 4 | 4 | 15 | 1 | 0 | exact for the second line, and not by design |
| 5 | 5 | 18 | 1 | 0 | |
| 6 | 6 | 19 | -1 | -2 | |
| 7 | 7 | 22 | -1 | -2 | |
| 8 | 8 | 27 | 1 | 0 | |
| 9 | 9 | 30 | 1 | 0 | |
| 10 | 10 | 31 | -1 | -2 | |
| 11 | 11 | 34 | -1 | -2 | |
| 12 | 12 | 39 | 1 | 0 | exact for the second line, by construction again |
The arithmetic is worth seeing written out, because it is the whole of the
score. For every squared residual is and there are
twelve of them, so and the mean squared error is
. For six residuals are zero and six are , so
and the mean squared error is
. The typical miss is 1 for the first line and
for the second.
The two candidates share a slope and differ only in their intercept, which
makes the comparison sharper than it looks. Moving the intercept from 2 up to
subtracts from every residual at once, so
using the two facts that the first line's squared residuals add to 12 and its
raw residuals add to zero. At that gives , which agrees
with the point by point total. One unit of intercept costs 12, and the cost
grows as the square of the error in the intercept rather than in proportion
to it.
Why squared and not absolute
Nothing above forced the square. Taking absolute values destroys the sign just
as well, so it has to be argued rather than assumed, and the argument is not
all one way.
Score both candidates by total absolute error. The first line has twelve
residuals of size one, giving . The second has six of size
zero and six of size two, giving . The two lines
tie. The measure that was supposed to choose between them has no opinion, and
the tie is not a coincidence of these two candidates.
The flat stretch can be read straight off the residuals. With the slope at 3,
six residuals are and six are , so moving the intercept to
makes the absolute total , which for any between
and is , with the terms cancelling. The
absolute total is constant across that whole interval, and it starts to rise
only once the intercept leaves it.
So squaring buys three things at once. The score is smooth, which absolute
error is not at zero. The score has one lowest point rather than a valley of
equally good answers. And the derivative of a square is linear in the unknowns,
which is what lets the best line be solved for rather than searched for.
What squaring costs is proportion, and the cost is easiest to see in a single
bad reading. Suppose the twelfth measurement were mistyped as 51 instead of 39.
The line predicts 38 there, so that residual becomes
and its square is 169, while the other eleven squared residuals
are still 1 each.
The honest summary is that squaring is chosen for its arithmetic and for one
statistical reason, not because it is the truest account of wrongness. The
statistical reason is worth stating precisely: if each measurement is the truth
plus an independent error drawn from the same normal distribution, then the
line minimising the squared total is the line under which the observed
measurements are most likely. Change that assumption about the errors and a
different score becomes the right one, which is why absolute error and its
relatives are reached for when a few readings are expected to be wild.
What has not been shown
A score now exists, two candidates have been fed through it, and one of them is
twice as good as the other. That is all. Nothing above shows that
is the best line, only that it beats a particular rival, and there is a
continuum of candidates that have not been tried.
What makes the next step possible is the shape of rather than any cleverness
about lines. It is a smooth function of two numbers with one lowest point, and
at that lowest point its two partial derivatives are both zero. Writing those
two conditions out, with the five sums from the table in place of the
measurements, gives two equations in and that can be solved exactly.
Recap
- The total squared error of a line is obtained by subtracting each prediction from each measurement, squaring the twelve differences and adding them, and on these twelve points the line y = 3x + 2 scores 12 while the line drawn through the first and last measurements scores 24.
- Residuals carry signs and cancel, so a flat line at the mean of the measurements has residuals adding to exactly zero and a squared total of 1,299, which is why the sign has to be destroyed before anything is added up.
- Squaring is a choice and not a law, since total absolute error gives both of these candidate lines the same score of 12 and leaves every intercept between 1 and 3 tied, while a squared total singles one line out and lets a single mistyped reading take 94 per cent of the blame.
This is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents