Basis Lab

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.

FIG 1The twelve measurements
stepinput xmeasurement y
116
227
3310
4415
5518
6619
7722
8827
9930
101031
111134
121239
12 steps
The twelve pairs, and the only data the course ever uses. They rise by about three for every one of x, which is the whole reason a straight line is worth fitting, and they do not rise by exactly three, which is the whole reason there is an error to measure at all.

The twelve measurements

Write xix_i for the iith input and yiy_i for the measurement taken at it, with

ii running from 1 to nn and n=12n = 12. 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

1+2+⋯+12=781 + 2 + \cdots + 12 = 78. Their squares add to

1+4+9+16+25+36+49+64+81+100+121+144=6501 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100 + 121 + 144 = 650. The

measurements add to

6+7+10+15+18+19+22+27+30+31+34+39=2586 + 7 + 10 + 15 + 18 + 19 + 22 + 27 + 30 + 31 + 34 + 39 = 258. The twelve

products xiyix_i y_i 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 xˉ=78/12=6.5\bar{x} = 78/12 = 6.5 and the mean measurement

yˉ=258/12=21.5\bar{y} = 258/12 = 21.5. 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 ww for its slope and bb for its

height where the input is zero, so the line's prediction at input xx is

wx+bw x + b. The prediction at the iith input is written y^i=wxi+b\hat{y}_i = w x_i + b,

and the amount the line misses that measurement by is the residual

ri=yi−y^i=yi−(wxi+b).r_i = y_i - \hat{y}_i = y_i - (w x_i + b).

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 w=0w = 0, b=21.5b = 21.5, which

sits at the height of the mean measurement. Its residuals are

6−21.5=−15.56 - 21.5 = -15.5 at the first point and 39−21.5=17.539 - 21.5 = 17.5 at the last, and

they add to 258−12×21.5=258−258=0258 - 12 \times 21.5 = 258 - 258 = 0 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.

FIG 2The total squared error of a candidate line
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
E is a function of the two numbers that define the line, not of the data. The twelve measurements have become fixed coefficients inside it, which is what makes the search for the best line a question about two unknowns rather than about twelve points.

The squared total for the flat line can now be done properly. The squares of

the measurements add to

36+49+100+225+324+361+484+729+900+961+1156+1521=684636 + 49 + 100 + 225 + 324 + 361 + 484 + 729 + 900 + 961 + 1156 + 1521 = 6846,

and expanding each squared residual gives

E(0,21.5)=∑i(yi−21.5)2=6846−2×21.5×258+12×21.52E(0, 21.5) = \sum_i (y_i - 21.5)^2 = 6846 - 2 \times 21.5 \times 258 + 12 \times 21.5^2,

which is 6846−11094+5547=12996846 - 11094 + 5547 = 1299.

Two more names, both needed later. The mean squared error is E/nE/n, 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 w=3w = 3, b=2b = 2, the line y=3x+2y = 3x + 2. The second is

drawn the way a person with a ruler draws one, through the first and last

measurements. Its slope is (39−6)/(12−1)=33/11=3(39 - 6)/(12 - 1) = 33/11 = 3, and matching the

first point gives 6=3×1+b6 = 3 \times 1 + b, so b=3b = 3 and the line is

y=3x+3y = 3x + 3. It is a defensible candidate, and it turns out to pass exactly

through six of the twelve measurements rather than only two.

FIG 3The twelve residuals under both candidate lines
stepxmeasured yresidual under y = 3x + 2residual under y = 3x + 3what happened
11610the second line was built to be exact here
227-1-2the first line is one high, the second two high
3310-1-2
441510exact for the second line, and not by design
551810
6619-1-2
7722-1-2
882710
993010
101031-1-2
111134-1-2
12123910exact for the second line, by construction again
12 steps
The first line is never exact and never off by more than one. The second is exact at six points and two short at the other six. Squaring and adding gives twelve ones, which is 12, against six fours, which is 24, so the line through half the measurements is twice as wrong as the line through none of them.

The arithmetic is worth seeing written out, because it is the whole of the

score. For y=3x+2y = 3x + 2 every squared residual is (±1)2=1(\pm 1)^2 = 1 and there are

twelve of them, so E(3,2)=12E(3, 2) = 12 and the mean squared error is

12/12=112/12 = 1. For y=3x+3y = 3x + 3 six residuals are zero and six are −2-2, so

E(3,3)=6×0+6×4=24E(3, 3) = 6 \times 0 + 6 \times 4 = 24 and the mean squared error is

24/12=224/12 = 2. The typical miss is 1 for the first line and

2≈1.41\sqrt{2} \approx 1.41 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

2+c2 + c subtracts cc from every residual at once, so

E(3,2+c)=∑i(ri−c)2=∑iri2−2c∑iri+12c2=12−0+12c2,E(3, 2 + c) = \sum_i (r_i - c)^2 = \sum_i r_i^2 - 2c \sum_i r_i + 12 c^2 = 12 - 0 + 12 c^2,

using the two facts that the first line's squared residuals add to 12 and its

raw residuals add to zero. At c=1c = 1 that gives 12+12=2412 + 12 = 24, 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 12×1=1212 \times 1 = 12. The second has six of size

zero and six of size two, giving 6×0+6×2=126 \times 0 + 6 \times 2 = 12. 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.

FIG 4Two ways of scoring the same family of lines
0.0017.5035.0052.5070.000.01.02.03.04.0the intercept b, with the slope held at 3
total squared errortotal absolute error
Both curves score the same lines, and they disagree about the answer. The squared total is a parabola with one lowest point, at an intercept of 2. The absolute total has a flat bottom running from 1 to 3, where every intercept scores 12 and nothing distinguishes them. A measure that ties a whole interval of candidates cannot be the thing a method follows downhill.

The flat stretch can be read straight off the residuals. With the slope at 3,

six residuals are +1+1 and six are −1-1, so moving the intercept to 2+c2 + c

makes the absolute total 6∣1−c∣+6∣1+c∣6|1 - c| + 6|1 + c|, which for any cc between −1-1

and 11 is 6(1−c)+6(1+c)=126(1 - c) + 6(1 + c) = 12, with the cc 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 y=3x+2y = 3x + 2 predicts 38 there, so that residual becomes

51−38=1351 - 38 = 13 and its square is 169, while the other eleven squared residuals

are still 1 each.

FIG 5Where the blame goes when one reading of twelve is mistyped
The squared total becomes 11 + 169 = 180, and the one bad reading owns 169 of it, which is 94 per cent of the blame for one twelfth of the data. Under absolute error the same reading contributes 13 of 11 + 13 = 24, which is 54 per cent. Any line chosen to make the squared total small will tilt towards that one reading, and that is a property of the score and not a mistake in the arithmetic.

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 y=3x+2y = 3x + 2

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 EE 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 ww and bb 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

NextSolving It Exactly, Once →

The rest of this course

  1. 01Measuring How Wrong a Straight Line Is, on Twelve Pointsyou are here
  2. 02The Two Equations That Pin Down the Best Lineopening only
  3. 03When the Derivative Equations Stop Being Solvableopening only
  4. 04The Largest Step This Data Allowsopening only
  5. 05Putting a Bend in the Model, and Differentiating Through Itopening only
  6. 06Every Derivative of a Two Unit Network, by Handopening only
  7. 07One Sweep Back Instead of Seven Chainsopening only
  8. 08Running It, and Reading the Curve It Producesopening only

Read alongside