Learning from dataLesson 2 of 6
The best guess is the average
Squared loss and the conditional mean
You must predict one number before seeing an output. To decide what counts as a good prediction, first decide how to score a mistake.
Score a guess
Section titled “Score a guess”Let be your guess and the output. A loss function assigns a cost to the error. Squared loss charges . Its expected cost is
Hold fixed. For each possible , subtract , square the difference, and average using the probabilities of the outputs. The expectation averages over outputs, not over guesses.
First, calculate with three numbers
Section titled “First, calculate with three numbers”Suppose is equally likely to be 1, 3, or 5. Its mean is 3. Compare guessing 2 with guessing 3:
| output | squared error if you guess 2 | squared error if you guess 3 |
|---|---|---|
| 1 | 1 | 4 |
| 3 | 1 | 0 |
| 5 | 9 | 4 |
| average |
Guessing the mean costs 1 less. That is the squared distance between the two guesses: . The same relationship holds for every guess. Let’s derive it.
Why the mean wins
Section titled “Why the mean wins”Write . We want to separate variation in the output from the effect of choosing the wrong guess. Adding and subtracting lets us make that split without changing the error:
In the example, guessing 2 gives . Now use :
Average each term. This is linearity of expectation.
| term | its average | reason |
|---|---|---|
| definition of variance | ||
| is fixed; | ||
| it is already a fixed number |
For the three-number example, the middle term takes values −4, 0, and 4: they cancel. The other terms average to and 1.
The variance does not depend on your guess. The remaining term is nonnegative, and becomes zero at . The mean minimizes expected squared loss, assuming the second moment is finite.
Best depends on the loss
Section titled “Best depends on the loss”Suppose the three equally weighted outputs are instead 0, 0, 9. Their mean is 3 and their median is 0.
| scoring rule | guess 0 | guess 3 | best prediction |
|---|---|---|---|
| average squared error | 27 | 18 | mean: 3 |
| average absolute error | 3 | 4 | median: 0 |
Absolute loss is a useful alternative when you want less sensitivity to large errors. Squared loss gives large errors extra weight and leads to the decomposition above. The claim “the mean is best” always needs its scoring rule.
Now at every x
Section titled “Now at every x”Apply the same reasoning to the conditional distribution at each input. The best predictor under squared loss is
When a conditional density exists, this average can be written
Here is a prediction, while names a density. They use the same letter for different objects; the subscript distinguishes them. In the integral, stays fixed and runs through the outputs. Multiply each output by its probability density and add over output values.
A compact statement is
Argmin returns the choice that achieves the lowest score. Here the choices are functions . At every input, choosing the conditional mean minimizes the conditional score; averaging those minimal scores over inputs gives the smallest overall expected score.
The floor, and the part above it
Section titled “The floor, and the part above it”For a fixed predictor and a fixed input , under our constant conditional noise variance assumption:
The first term is error from missing the conditional mean. The second is the variation of the output around that mean. Even knowing exactly leaves expected squared error . This is irreducible error, relative to the inputs you have measured. It is a floor for expected error; an individual test sample can score below it by chance.
Take , with independent . At :
- : uniform on .
- Its mean is and its variance is .
- A rival guesses .
Work through this style of question, from the original notation onward.
Practice prediction error
Section titled “Practice prediction error”You know what the best predictor is in theory. How do you estimate it when you only have a finite dataset?