error and loss
learning from examples
what it is
A line guesses a height for each point, straight above or below it. A miss is the point's height minus that guess. For a point below the line, it is a minus number. A loss drops the signs or squares the misses, then adds them into one number. Smaller is better.
A line guesses a height for every point. Take four points, each written across then up: (2, 3), (4, 2), (6, 6) and (8, 5). The line y = 0.5x + 1.5 rises 0.5 for each 1 across, its slope. It crosses the up line at 1.5, its height where across is 0. Above 2 it guesses 0.5 times 2, plus 1.5, which is 2.5. The point there stands at 3. The miss is the point's height minus the guess: 3 minus 2.5 is 0.5. Statistics books call this miss the residual. Many other books call it the error.
A point under the line misses by a minus number. Above 4 the line guesses 3.5 and the point stands at 2, so the miss is 2 minus 3.5, which is minus 1.5. The four misses are 0.5, minus 1.5, 1.5 and minus 0.5.
| across | up | guess | miss |
|---|---|---|---|
| 2 | 3 | 2.5 | 0.5 |
| 4 | 2 | 3.5 | -1.5 |
| 6 | 6 | 4.5 | 1.5 |
| 8 | 5 | 5.5 | -0.5 |
To score the whole line, add its misses into one number. With their signs they make 0, as if the line fitted perfectly. So drop each sign first: 0.5, 1.5, 1.5 and 0.5 add to 4. Or square each miss: 0.25, 2.25, 2.25 and 0.25 add to 5.
A total like this is a loss. The first is the plain loss, which books call the absolute loss. The second is the squared loss. 5 divided by the 4 points is 1.25, the mean squared error: the squared loss for each point, on average. The smaller the loss, the better the line fits.
Squares count a big miss far more. A miss of 1 counts 1, a miss of 2 counts 4 and a miss of 3 counts 9. Missing one point by 2 adds 2 to the plain loss and 4 to the squared. Missing two points by 1 each adds 2 to both.
| misses | plain loss | squared loss |
|---|---|---|
| one miss of 2 | 2 | 4 |
| two misses of 1 | 2 | 2 |
Squares also pick one best flat line. The plain loss can tie. The flat line y = 4 misses by minus 1, minus 2, 2 and 1: 6 plain, 10 squared. The flat line y = 3 misses by 0, minus 1, 3 and 2. The flat line y = 5 misses by minus 2, minus 3, 1 and 0. Both make 6 plain, but 14 squared. So the plain loss cannot choose among the three, and the squared loss picks y = 4. A sloping line does better still: with the sliders below, find the least squared loss, 5, at y = 0.5x + 1.5.
At slope 0 and height 4, the misses add to 6 and their squares to 10.
With scripts on, one slider sets the slope, another the height, and a switch draws each miss as its length or its square. The drawing, its bar and this line change with them.
k-means, a later page, sorts points into groups, each with a centre. A point's miss is its distance to its centre. Adding the squares of these misses gives a squared loss.
where the formulas come from
Each question is worked out one numbered line at a time. A line shows the move it makes, the line itself, and why the move is allowed. A line either follows from the lines above it, or brings in something the question needs, such as how the method works, a small fact about , or a result from another page, with its own reason. Press 'hide the lines, then guess each one' to cover the lines and uncover them one by one, guessing each before you see it.
the move: add the squared misses
the line:why it is allowed: Each miss is a point's height minus the guess, . Square each one and add. When is 4, the squares 1, 4, 4 and 1 make 10.
the move: multiply out each square
the line:times 3 minus c 3 9 minus 3c minus c minus 3c c squared 3 minus c times itself, box by box. 9, two boxes that each take away 3c, and c squared. why it is allowed: Multiply out , the bracket 3 minus times itself. 3 times 3 is 9. Two boxes each take away 3 lots of , in all. Minus times minus is . The other three go the same way.
guess it, thenthe move: add the like parts
the line:why it is allowed: The four make . The heights add to 16, so the middle parts take away twice that: 6, 4, 12 and 10 lots of , 32 in all. The squares of 3, 2, 6 and 5 make 74.
guess it, thenthe move: split 74 as 64 + 10
the line:times c minus 4 c c squared minus 4c minus 4 minus 4c 16 c minus 4 times itself, box by box. c squared, two boxes that each take away 4c, and 16. why it is allowed: We want 4 times a square, since a square is never below 0. Line 3 takes away 32 lots of , which is 4 times 8 lots, and half of 8 is 4. So try times itself: , take away 8 lots of , plus 16. Four times 16 is 64, so 74 splits into 64 and 10.
guess it, thenthe move: take out the 4
the line:why it is allowed: , 32 lots of and 64 are each 4 times , 8 lots of and 16.
guess it, thenthe move: spot the square
the line:c plain loss squared loss 2 8 26 3 6 14 4 6 10 5 6 14 6 8 26 Flat lines at heights 2 to 6. The plain loss ties at 6 for three of them. The squared loss is least at 4 alone, the mean. why it is allowed: Line 4 multiplied out to , take away 8 lots of , plus 16. Read it backwards.
guess it, thenthe move: a square is never below 0
the line:why it is allowed: is never below 0, so neither is 4 times it. is that plus 10, so : is at least 10. It is 10 only when is 0, so when is 4, the mean of 3, 2, 6 and 5. Here the best flat line stands at the mean.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: square the one miss
the line:why it is allowed: The other misses are 0, so they add nothing.
the move: multiply out
the line:why it is allowed: is times .
guess it, thenthe move: write it as twice the two small misses' loss
the line:why it is allowed: Line A's squared loss is , twice . Twice that again is , so B's squared loss is twice A's, however big is. When is 1, that is 4 against 2.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
a worked example
Two apps guess the top temperature for 3 days. App A is 2 degrees off every day. App B is right twice and 6 degrees off once. Which is better?
- Plain, A misses by 2, 2 and 2, which add to 6. B misses by 0, 0 and 6, which also add to 6. A tie.
- Squared, A's misses give 4, 4 and 4, which add to 12. B's give 0, 0 and 36, which add to 36.
- The squared loss prefers A, which is never far off. Use squares when one big miss is worse than several small ones.
| day | A off by | B off by |
|---|---|---|
| 1 | 2 | 0 |
| 2 | 2 | 0 |
| 3 | 2 | 6 |
| plain loss | 6 | 6 |
| squared loss | 12 | 36 |
the trap
check yourself
A line guesses 7 where the point stands at 4. What is the miss, and its square?
the answer
4 minus 7 is minus 3. Its square is 9, the same as for a miss of 3.
A line misses by 1, minus 1, 2 and minus 2. What are its plain and squared losses?
the answer
Plain, 1, 1, 2 and 2 add to 6. Squared, 1, 1, 4 and 4 add to 10.
Which flat line has the least squared loss for the points at 1, 2 and 6?
the answer
Call the flat line's height c. Worked as in the flat line question above, the squares add to 3c squared, minus 18c, plus 41. (c minus 3) squared is c squared, minus 6c, plus 9, so 3 times it ends in 27, and 41 is 27 and 14. So the sum is 3 times (c minus 3) squared, plus 14. It is least, 14, when c is 3, the mean. Its misses, minus 2, minus 1 and 3, square to 4, 1 and 9. At 2 or at 4 they add to 17.