Math for AILoading progress…

Bias and variance

Connect likelihood, estimation and uncertainty to entropy, cross-entropy, KL divergence and perplexity.

An estimator is a recipe, so judge it over many repetitions

An estimator is a rule that turns a data set into an estimate. One data set gives one estimate, and you cannot tell from one number whether the rule is good. To judge a rule, imagine repeating the whole experiment many times and look at the pile of estimates. Two properties matter. Bias: does the pile centre on the truth? (bias = average estimate − truth). Variance: how widely does the pile spread?

The classic example is estimating a variance. Draw n values; compute the mean; add up squared deviations from that sample mean. Divide by n, or by n − 1?

016truth 4average estimate

4000 repeated experiments, each draws 10 values from a normal distribution with true variance 4 and applies the estimator.

values per experiment n10
biasvarianceMSE
measured-0.452.7993.002
exact-0.42.883.04

MSE = bias² + variance. Truth: variance 4 (mean 10).

Each experiment draws values from a normal distribution with true variance 4. Switch between the estimators and watch the orange line (average estimate) against the green one (truth).

Why n - 1 is the unbiased choice

The sample mean is fitted to the same data, so the data sit closer to it than to the true mean. Squared deviations from the sample mean are too small on average, by exactly a factor (n − 1) / n. Dividing by n therefore underestimates: the exact bias is −σ² / n. With σ² = 4 and n = 5 that is -0.8; the simulation measures -0.84. Dividing by n − 1 undoes the factor: the exact bias is 0 and the simulation measures -0.05.

Predict first. You use the divide-by-n estimator and increase n from 5 to 40. What happens to its bias?

Unbiased does not mean best

Prefer a low bias? Not always. The total error of an estimator is its mean squared error, MSE = bias² + variance. At n = 5 the divide-by-n estimator has MSE 5.76, while the unbiased divide-by-(n−1) one has 8: the biased one is wrong in a steady direction but wobbles less, and it wins. At the other extreme, "always say 7" never varies at all, yet it is always wrong by 3.

Chapter context

Statistics estimates properties of a population from observations. Information measures describe how surprising those observations are under a probability model. Keep the true or empirical distribution distinct from the model used to describe it.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

H(p)=−∑ipilog⁡pi,H(p,q)=−∑ipilog⁡qi.H(p)=-\sum_i p_i\log p_i,\quad H(p,q)=-\sum_i p_i\log q_i. DKL(p∥q)=H(p,q)−H(p),PPL⁡=eH(p,q).D_{KL}(p\Vert q)=H(p,q)-H(p),\quad \operatorname{PPL}=e^{H(p,q)}.

These expressions use natural logarithms. If q assigns zero probability where p is positive, cross-entropy and KL are infinite.

For n Bernoulli observations with s successes, the maximum-likelihood probability estimate is s/n. For a sample with mean x-bar, the variance estimate dividing squared deviations by n is a different quantity from the usual unbiased sample-variance estimator dividing by n-1. State which one is being used.

Worked example

Use p=[0.5,0.5] and q=[0.25,0.75]. Source entropy is ln 2≈0.693147 nats. Cross-entropy is −0.5ln(0.25)−0.5ln(0.75)≈0.836988. KL≈0.143841 nats. Perplexity is exp(0.836988)≈2.309401, larger than the source's effective uncertainty of 2.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import log, exp, isclose
p, q = [.5,.5], [.25,.75]
h = -sum(a*log(a) for a in p)
ce = -sum(a*log(b) for a,b in zip(p,q))
assert isclose(h, 0.6931471805599453)
assert isclose(ce, 0.8369882167858358)
assert isclose(ce-h, 0.1438410362258905)
assert isclose(exp(ce), 2.309401076758503)

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

Likelihood is not the posterior probability of a model. Lower perplexity across different tokenizers is not a clean model comparison. Bias is not removed merely by computing more decimal places.