Skip to content

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.

002244668810100.51.51.50.54 in all
Four points and the line y = 0.5x + 1.5. Each yellow band is a miss, its size beside it. Laid end to end in the bar, the misses make 4.

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.

acrossupguessmiss
232.50.5
423.5-1.5
664.51.5
855.5-0.5
Each point, the line's guess above it, and the miss, its height minus the guess. Below the line, the miss is a minus number.

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.

002244668810100.252.252.250.255 in all
The same line with the square on each miss, 0.25, 2.25, 2.25 and 0.25. The bar adds them 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.

missesplain losssquared loss
one miss of 224
two misses of 122
One big miss against two small ones. Plain, both make 2. Squared, the big miss makes 4, twice as much.

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.

0022446688101012216 in all

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.

The same four points, starting from the flat line y = 4, which misses by 6 plain and 10 squared. The whole bar stands for the worst loss the sliders reach. The misses, or their squares, fill it end to end, so less of it filled is a smaller loss. Move the line to find the least squared loss, 5, at slope 0.5 and height 1.5.

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 nn, 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.

question 1A flat line guesses the same height, cc, everywhere, so only the points' heights matter: 3, 2, 6 and 5. Call its squared loss LL. Which cc makes LL least?
  1. the move: add the squared misses

    the line:
    L=(3−c)2+(2−c)2+(6−c)2+(5−c)2\begin{gathered} L = (3 - c)^2 + (2 - c)^2 \\ + (6 - c)^2 + (5 - c)^2 \end{gathered}

    why it is allowed: Each miss is a point's height minus the guess, cc. Square each one and add. When cc is 4, the squares 1, 4, 4 and 1 make 10.

  2. the move: multiply out each square

    the line:
    L=(9−6c+c2)+(4−4c+c2)+(36−12c+c2)+(25−10c+c2)\begin{gathered} L = (9 - 6c + c^2) \\ + (4 - 4c + c^2) \\ + (36 - 12c + c^2) \\ + (25 - 10c + c^2) \end{gathered}
    times3minus c
    39minus 3c
    minus cminus 3cc 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 (3−c)2(3 - c)^2, the bracket 3 minus cc times itself. 3 times 3 is 9. Two boxes each take away 3 lots of cc, 6c6c in all. Minus cc times minus cc is c2c^2. The other three go the same way.

  3. the move: add the like parts

    the line:
    L=4c2−32c+74L = 4c^2 - 32c + 74

    why it is allowed: The four c2c^2 make 4c24c^2. The heights add to 16, so the middle parts take away twice that: 6, 4, 12 and 10 lots of cc, 32 in all. The squares of 3, 2, 6 and 5 make 74.

  4. the move: split 74 as 64 + 10

    the line:
    L=4c2−32c+64+10L = 4c^2 - 32c + 64 + 10
    timescminus 4
    cc squaredminus 4c
    minus 4minus 4c16
    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 cc, which is 4 times 8 lots, and half of 8 is 4. So try c−4c - 4 times itself: c2c^2, take away 8 lots of cc, plus 16. Four times 16 is 64, so 74 splits into 64 and 10.

  5. the move: take out the 4

    the line:
    L=4(c2−8c+16)+10L = 4(c^2 - 8c + 16) + 10

    why it is allowed: 4c24c^2, 32 lots of cc and 64 are each 4 times c2c^2, 8 lots of cc and 16.

  6. the move: spot the square

    the line:
    L=4(c−4)2+10L = 4(c - 4)^2 + 10
    cplain losssquared loss
    2826
    3614
    4610
    5614
    6826
    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 (c−4)2(c - 4)^2 out to c2c^2, take away 8 lots of cc, plus 16. Read it backwards.

  7. the move: a square is never below 0

    the line:
    L≥10L \ge 10

    why it is allowed: (c−4)2(c - 4)^2 is never below 0, so neither is 4 times it. LL is that plus 10, so L≥10L \ge 10: LL is at least 10. It is 10 only when c−4c - 4 is 0, so when cc is 4, the mean of 3, 2, 6 and 5. Here the best flat line stands at the mean.

try it with a small cc: at c=4c = 4, line 1 gives L=10L = 10, which you can work out by hand

question 2Pick any whole number nn. Line A misses two points by nn each and the rest by 0. Line B misses one point by 2n2n and the rest by 0. Both plain losses are 2n2n. Call B's squared loss SS. How does it compare with A's?
  1. the move: square the one miss

    the line:
    S=(2n)2S = (2n)^2

    why it is allowed: The other misses are 0, so they add nothing.

  2. the move: multiply out

    the line:
    S=4n2S = 4n^2

    why it is allowed: (2n)2(2n)^2 is 2n2n times 2n2n.

  3. the move: write it as twice the two small misses' loss

    the line:
    S=2(n2+n2)S = 2(n^2 + n^2)

    why it is allowed: Line A's squared loss is n2+n2n^2 + n^2, twice n2n^2. Twice that again is 4n24n^2, so B's squared loss is twice A's, however big nn is. When nn is 1, that is 4 against 2.

try it with a small nn: at n=1n = 1, line 1 gives S=4S = 4, 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?

  1. 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.
  2. Squared, A's misses give 4, 4 and 4, which add to 12. B's give 0, 0 and 36, which add to 36.
  3. The squared loss prefers A, which is never far off. Use squares when one big miss is worse than several small ones.
dayA off byB off by
120
220
326
plain loss66
squared loss1236
Each app's misses over 3 days. Plain, they tie at 6. Squared, A's 12 beats B's 36.

the trap

check yourself

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

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

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