Skip to content

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.

Let cc be your guess and YY the output. A loss function assigns a cost to the error. Squared loss charges (Yc)2(Y-c)^2. Its expected cost is

E[(Yc)2].\mathbb E[(Y-c)^2].

Hold cc fixed. For each possible YY, subtract cc, square the difference, and average using the probabilities of the outputs. The expectation averages over outputs, not over guesses.

Suppose YY is equally likely to be 1, 3, or 5. Its mean is 3. Compare guessing 2 with guessing 3:

outputsquared error if you guess 2squared error if you guess 3
114
310
594
average11/311/38/38/3

Guessing the mean costs 1 less. That is the squared distance between the two guesses: (32)2=1(3-2)^2=1. The same relationship holds for every guess. Let’s derive it.

The left graph shows individual errors. The right graph shows their average squared cost for different guesses.

Write m=E[Y]m=\mathbb E[Y]. We want to separate variation in the output from the effect of choosing the wrong guess. Adding and subtracting mm lets us make that split without changing the error:

Yc=(Ym)output minus mean+(mc)mean minus guess.Y-c=\underbrace{(Y-m)}_{\text{output minus mean}}+\underbrace{(m-c)}_{\text{mean minus guess}}.

In the example, guessing 2 gives Y2=(Y3)+1Y-2=(Y-3)+1. Now use (a+b)2=a2+2ab+b2(a+b)^2=a^2+2ab+b^2:

(Yc)2=(Ym)2+2(Ym)(mc)+(mc)2.\begin{aligned} (Y-c)^2&=(Y-m)^2+2(Y-m)(m-c)\\ &\quad +(m-c)^2. \end{aligned}

Average each term. This is linearity of expectation.

termits averagereason
(Ym)2(Y-m)^2Var(Y)\operatorname{Var}(Y)definition of variance
2(Ym)(mc)2(Y-m)(m-c)00mcm-c is fixed; E[Ym]=0\mathbb E[Y-m]=0
(mc)2(m-c)^2(mc)2(m-c)^2it 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 8/38/3 and 1.

E[(Yc)2]=Var(Y)+(mc)2.\mathbb E[(Y-c)^2]=\operatorname{Var}(Y)+(m-c)^2.

The variance does not depend on your guess. The remaining term is nonnegative, and becomes zero at c=mc=m. The mean minimizes expected squared loss, assuming the second moment is finite.

Suppose the three equally weighted outputs are instead 0, 0, 9. Their mean is 3 and their median is 0.

scoring ruleguess 0guess 3best prediction
average squared error2718mean: 3
average absolute error34median: 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.

Apply the same reasoning to the conditional distribution at each input. The best predictor under squared loss is

f(x)=E[YX=x].f(x)=\mathbb E[Y\mid X=x].

When a conditional density exists, this average can be written

f(x)=yfYX(yx)dy.f(x)=\int_{-\infty}^{\infty} y\,f_{Y\mid X}(y\mid x)\,dy.

Here f(x)f(x) is a prediction, while fYXf_{Y\mid X} names a density. They use the same letter for different objects; the subscript distinguishes them. In the integral, xx stays fixed and yy runs through the outputs. Multiply each output by its probability density and add over output values.

A compact statement is

f=arg mingE[(Yg(X))2].f=\operatorname*{arg\,min}_{g}\mathbb E[(Y-g(X))^2].

Argmin returns the choice that achieves the lowest score. Here the choices are functions gg. At every input, choosing the conditional mean minimizes the conditional score; averaging those minimal scores over inputs gives the smallest overall expected score.

For a fixed predictor hh and a fixed input xx, under our constant conditional noise variance assumption:

E[(Yh(x))2X=x]=(f(x)h(x))2+σ2.\mathbb E[(Y-h(x))^2\mid X=x]=(f(x)-h(x))^2+\sigma^2.

The first term is error from missing the conditional mean. The second is the variation of the output around that mean. Even knowing ff exactly leaves expected squared error σ2\sigma^2. 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 Y=X2+εY=X^2+\varepsilon, with independent εUnif[1,1]\varepsilon\sim\operatorname{Unif}[-1,1]. At x=1/2x=1/2:

  • Y=1/4+εY=1/4+\varepsilon: uniform on [3/4,5/4][-3/4,5/4].
  • Its mean is 1/41/4 and its variance is 1/31/3.
  • A rival h(x)=x3h(x)=x^3 guesses 1/81/8.
E[(Yh(x))2X=1/2]=(1/41/8)2+1/3=1/64+1/3=67/192.\begin{aligned} \mathbb E[(Y-h(x))^2\mid X=1/2] &=(1/4-1/8)^2+1/3\\ &=1/64+1/3=67/192. \end{aligned}

Work through this style of question, from the original notation onward.

You know what the best predictor is in theory. How do you estimate it when you only have a finite dataset?

Definition

Read the full glossary entry →