Statistics essentials
Estimators, error bars and the honest evaluation of models
Every number you will ever report — an accuracy, a benchmark score, a difference between two models — is an estimate from a finite sample, and it comes with uncertainty whether you report it or not. Statistics gives you the tools to size that uncertainty: the bias and variance of estimators, standard errors, confidence intervals, hypothesis tests and the bootstrap. These are what separate "my model is better" from a claim you can defend, and they return in model selection (3.5 Evaluating models: metrics, validation and the bias–variance trade-off), benchmark evaluation (8.7 Evaluating LLMs) and agent evals (10.9 Evaluating agents).
You'll be able to
- Define bias, variance and mean squared error of an estimator and derive the n − 1 correction for the sample variance
- State the law of large numbers and the central limit theorem and compute standard errors
- Build and correctly interpret confidence intervals for means and proportions, including for benchmark accuracies
- Use p-values, paired comparisons and the bootstrap to decide whether a difference between two models is real
Key ideas
- MSE of an estimator = bias² + variance; the sample mean is unbiased with variance σ²/n.
- The 1/n sample variance is biased by (n − 1)/n because the mean was fit to the same data; dividing by n − 1 (Bessel) fixes it.
- CLT: √n (x̄ − μ)/σ → N(0, 1) regardless of the data's distribution; standard error = s/√n; halving error bars needs 4× the data.
Show 4 more key ideas
- 95% CI: x̄ ± 1.96 SE, or p̂ ± 1.96 √(p̂(1 − p̂)/n) for accuracies; 80% on 500 questions is [76.5%, 83.5%].
- A p-value is P(data at least this extreme | H₀), not P(H₀ | data); watch out for multiple comparisons and effect size.
- Compare models on the same test set with paired statistics; the bootstrap (resample with replacement, take percentiles) gives intervals for any metric.
- Report mean ± std across seeds; differences inside the confidence interval are ties.
Suggested path through this lesson
Population, sample, estimator
A population quantity (the true accuracy of a model on all possible inputs, the true mean of a distribution) is unknowable; you observe a sample of draws and compute an estimator — a function of the data. Because the sample is random, so is : repeat the experiment and you get a different number. The distribution of across hypothetical repetitions is its sampling distribution, and everything in this lesson is about describing it.
Bias and variance of an estimator
Two properties summarize an estimator. Its bias is — is it right on average? Its variance is — how much does it wobble? They combine into the mean squared error:
Math check. Write and square. The cross term is . ✓ The same decomposition, applied to a model's predictions, is the bias–variance trade-off of 3.5 Evaluating models: metrics, validation and the bias–variance trade-off.
The sample mean is unbiased ( by linearity) with variance (independent variances add, then scale by ). The MLE of the variance from 1.7 Probability II: Bayes, independence, MLE and MAP, , is biased: because was fit to the same data, deviations from it are systematically smaller than deviations from . Precisely, ; taking expectations gives . Hence , and dividing by instead (Bessel's correction, ddof=1) makes the estimator unbiased. One degree of freedom was spent estimating the mean.
Consistency, the law of large numbers and the CLT
An estimator is consistent if it converges to as . The law of large numbers says the sample mean does: . The central limit theorem says how fast and in what shape: for i.i.d. data with finite variance,
whatever the distribution of the individual — coin flips, token losses, click-through indicators. The quantity (estimated by ) is the standard error. The square root is the cruel part: to halve your error bars you need four times the data.
Confidence intervals
Because is approximately Gaussian, the interval captures in about 95% of repeated experiments: that is a 95% confidence interval. Its interpretation is a statement about the procedure, not about this particular interval — it is not "a 95% probability that lies here" (that would be a Bayesian credible interval, 1.7 Probability II: Bayes, independence, MLE and MAP). For a proportion such as accuracy, , and
Worked example. A model scores 80% on a 500-question benchmark: , so the 95% interval is roughly . A competitor at 82% on the same 500 questions is well inside that interval. To decide between them you compare the difference: on independent test sets , so a 2-point gap is noise. On the same questions you should use the per-item differences (a paired comparison), whose standard error is often much smaller because both models get the easy questions right and the hard ones wrong together.
Hypothesis tests and p-values
A hypothesis test starts from a null hypothesis ("the two models have equal accuracy"), computes a test statistic, and reports a p-value: the probability, if were true, of a result at least as extreme as the one observed. A small p-value (conventionally below 0.05) is evidence against . Three things a p-value is not: the probability that is true, the probability that the result is a fluke, or a measure of effect size — a tiny improvement becomes "significant" with enough data. Beware multiple comparisons: evaluate on 20 benchmarks and you expect one to look significant at the 0.05 level by chance alone. For paired classification results, McNemar's test and the paired bootstrap below are the right tools.
Intuition. A p-value answers "how surprising is my data if nothing is going on?" — not "is something going on?". Both questions matter; only the first is being answered.
The bootstrap and Monte Carlo
When the statistic is complicated (F1, nDCG, a median, a win rate), skip the formulas and bootstrap: resample the data points with replacement times (say ), recompute the statistic each time, and read the 2.5th and 97.5th percentiles as a confidence interval. The sampling distribution is simulated instead of derived. For comparing two models, resample pairs of outcomes and bootstrap the difference — if the interval excludes zero, the gap is real at that confidence.
import numpy as np
rng = np.random.default_rng(0)
def bootstrap_ci(values, stat=np.mean, B=1000, alpha=0.05):
n = len(values)
boots = [stat(values[rng.integers(0, n, n)]) for _ in range(B)]
return np.percentile(boots, [100 * alpha / 2, 100 * (1 - alpha / 2)])
correct = rng.random(500) < 0.8 # 500 right/wrong outcomes
print(bootstrap_ci(correct)) # roughly [0.765, 0.835]
The bootstrap is one instance of Monte Carlo estimation: approximate an expectation by an average over random samples. Its error shrinks like in the number of samples — the CLT again — which is why variance-reduction tricks matter so much in reinforcement learning (4.11 Reinforcement learning essentials) and variational inference (8.8 Generative models I: VAEs and GANs).
Statistics in machine-learning evaluation
Three habits follow. Size your test set: for accuracy near 0.8 and a desired half-width of 1 percentage point you need examples; a 100-example eval set cannot resolve differences below about 8 points. Report variance across seeds: train with at least three random seeds and report mean ± standard deviation; many published "improvements" are within seed noise. Treat leaderboards statistically: differences smaller than the confidence interval are ties (8.7 Evaluating LLMs). Cross-validation (3.5 Evaluating models: metrics, validation and the bias–variance trade-off) is a resampling scheme that estimates both the mean and the variance of a model's performance, and the learning-rate sweeps of 4.8 Training dynamics and debugging are hypothesis tests you run informally — know what they can and cannot conclude.
Where this shows up later
- The bias–variance decomposition of a model's error — 3.5 Evaluating models: metrics, validation and the bias–variance trade-off; cross-validation and model selection — 3.5 Evaluating models: metrics, validation and the bias–variance trade-off, 3.6 Regularization and generalization.
- Benchmark error bars, LLM-as-judge noise and eval-set sizing — 8.7 Evaluating LLMs; agent evals with pass@k and compounding error — 10.9 Evaluating agents.
- Monte Carlo estimates and variance reduction — 4.11 Reinforcement learning essentials, 8.8 Generative models I: VAEs and GANs; retrieval metrics with confidence intervals — 9.8 Evaluating and observing LLM apps.
Common pitfalls (4)
- Reading a 95% confidence interval as a 95% probability that the parameter lies in it.
- Comparing two models with independent-sample error bars when they were scored on the same questions — paired comparisons are far more powerful.
- Declaring a 1-point gain on a 200-example benchmark a win; its standard error alone is about 3 points.
- Using np.std with the default ddof=0 for a sample standard deviation and reporting a slightly too-small number.
Recap: all 7 key points
- MSE of an estimator = bias² + variance; the sample mean is unbiased with variance σ²/n.
- The 1/n sample variance is biased by (n − 1)/n because the mean was fit to the same data; dividing by n − 1 (Bessel) fixes it.
- CLT: √n (x̄ − μ)/σ → N(0, 1) regardless of the data's distribution; standard error = s/√n; halving error bars needs 4× the data.
- 95% CI: x̄ ± 1.96 SE, or p̂ ± 1.96 √(p̂(1 − p̂)/n) for accuracies; 80% on 500 questions is [76.5%, 83.5%].
- A p-value is P(data at least this extreme | H₀), not P(H₀ | data); watch out for multiple comparisons and effect size.
- Compare models on the same test set with paired statistics; the bootstrap (resample with replacement, take percentiles) gives intervals for any metric.
- Report mean ± std across seeds; differences inside the confidence interval are ties.
Watch
The theorem that makes standard errors and confidence intervals work; pay attention to the √n scaling.
Read
Exercises
- By hand: intervals and a paired comparison
Model A gets 410/500 and model B gets 422/500 on the same test set; on 40 questions exactly one of them is right (B wins 26 of those). Compute both 95% intervals, the unpaired SE of the difference, and then reason about the paired data: under the 26-of-40 split is Binomial(40, 0.5). Expected: intervals overlap heavily; the paired analysis gives a p-value around 0.08 — suggestive, not conclusive.
- NumPy: see the CLT and Bessel's correction
Draw 10,000 samples of size from an exponential distribution with mean 1. Compute the mean of each sample and plot the histogram (compare with ). Compute the variance of each sample with
ddof=0andddof=1and average both across samples. Expected: the mean histogram is already roughly bell-shaped; the ddof=0 average is near 0.8, the ddof=1 average near 1.0. - Experiment: bootstrap a real metric
Take any classifier you have (or simulate predictions), compute F1 on 300 held-out points, and bootstrap a 95% interval with . Then compare two models with a paired bootstrap of the F1 difference. Expected: a 300-point interval several points wide, and an honest answer to 'is B better than A?'.
Quiz
Flashcards
These cards are now in your spaced-repetition queue. Review all due cards →