12 / 22 · Limit Theorems
Inequalities and concentration
How far can a random quantity be from its mean? Markov's and Chebyshev's inequalities from the mean and the variance alone, Hoeffding's exponential bound for averages of bounded variables and where its exponent comes from, sample sizes for an accuracy estimate, and a first generalization bound: why the best of many models on a validation set looks better than it is.
On this page 11
Most measured numbers in machine learning are averages over a sample: a validation accuracy, a mean loss, a click-through rate. How far can such an average be from the expectation it estimates? The distribution is usually unknown, so the useful answers are inequalities that hold for every distribution with some property: a known mean, a known variance, a bounded range. This note derives the three that matter, Markov’s, Chebyshev’s and Hoeffding’s, compares them with the exact answer, and uses the last one with the union bound to get a first guarantee about a model chosen on a validation set.
Notation. are independent with mean ; is their average. is a deviation and a small failure probability.
Markov’s inequality#
For a non-negative random variable and any ,
The proof is one comparison: for every outcome, and taking expectations gives . A mean alone limits how much probability can sit far out. If requests take 0.2 s on average, at most 10% of them can take 2 s or more, whatever the distribution. The bound is often loose: for an exponential response time with that mean the true share is . It cannot be improved without more information, though: a variable equal to with probability and to 0 otherwise attains it.
Chebyshev’s inequality#
Applying Markov’s inequality to uses the variance as well:
Any distribution with a finite variance has at most 25% of its probability two standard deviations or more from the mean, and at most 11.1% three or more. A normal distribution has and ; again the bound holds for every distribution and is far from the typical one.
Applied to an average, whose variance is , Chebyshev’s inequality gives
which is already the weak law of large numbers: averages approach the mean. The rate, , is too pessimistic.
Hoeffding’s inequality#
If the variables are also bounded, , the probability of a deviation falls exponentially:
Losses in , correct-or-not indicators and clicks are all bounded, which is why this inequality is the standard tool of learning theory. For the share of heads in fair coin flips and a deviation of , the two bounds and the exact binomial probability compare as follows:
Two tail bounds and the exact probability
Turned around, Hoeffding’s inequality gives a sample size. To estimate an accuracy within with probability at least , set :
| Method | Examples needed | Assumes |
|---|---|---|
| Chebyshev, with | 12,500 | a finite variance |
| Hoeffding | 4,612 | independent, bounded |
| Normal approximation | 2,401 | large enough for the central limit theorem |
The normal approximation needs the fewest examples and is what confidence intervals use; Hoeffding’s bound costs about twice as many and in return is guaranteed for every .
Where the exponent comes from#
Optional depth: a proof sketch of Hoeffding’s inequality. The rest of the note does not depend on it.
The proof is Markov’s inequality applied to an exponential, the Chernoff method. For any ,
where independence turned the expectation of a product into a product of expectations, the moment generating functions of the expectation note. For a variable in , Hoeffding’s lemma bounds each factor by . Boundedness is what makes the exponent quadratic. Let . Then and , and is the variance of under its distribution reweighted by the factor and renormalized, which still lives on ; a variable on has variance at most . Taylor’s theorem with these three facts gives . The bound is smallest at , which gives ; the same argument for the lower tail supplies the factor 2. Markov’s inequality applied to gives polynomial tails; applied to it gives exponential ones, as long as the moment generating function exists.
A first generalization bound#
A model’s risk is its expected loss on new data, and its validation loss is the average over independent validation examples. For one model fixed in advance, Hoeffding’s inequality with losses in gives . But the model is usually chosen on the validation set, the best of candidates from a hyperparameter search. The union bound over the events “model deviates by ” gives, for all models simultaneously,
Setting the right-hand side to : with probability at least , every one of the models, and so also the chosen one, has
For models and validation examples at the margin is . The number of candidates enters only through , so a thousand times more candidates costs only a few times more data. The figure shows the margin next to the typical effect of selection when all models are in truth equally good:
How much better the best of K equally good models looks than it is
The best of 1,000 equally accurate models, with independent errors on the validation set, looks on average 1.3 accuracy points better than it is. That is the mechanism of overfitting to a validation set, and the reason a final model is reported on a test set that played no part in choosing it. The argument assumes that the candidates are fixed before the validation data are seen; a search that proposes new candidates from validation scores, such as Bayesian optimization, needs more care. For infinitely many candidates, such as all the weight settings of a network, is replaced by measures of the capacity of the model class; in practice the machine learning course estimates the error on held-out data instead.
What changed#
| Without inequalities | With them | |
|---|---|---|
| Error of an average | “small for large ” | Chebyshev: ; Hoeffding: |
| Sample size for at 95% | a guess | 4,612 guaranteed (Hoeffding), 2,401 approximately (normal) |
| A model chosen among | its validation score taken at face value | margin for all at once |
Limitations#
- The bounds are worst-case. They hold for every distribution with the stated property and so are loose for any particular one; Chebyshev’s is off by orders of magnitude for normal tails.
- Hoeffding needs boundedness and independence. Heavy-tailed losses, or examples that come in correlated groups (several from one user, near-duplicates), break the assumptions; then other tools are needed: median-of-means estimators for heavy tails, bounds over independent blocks for grouped data, and Bernstein’s inequality when the variance is much smaller than the range.
- The union bound ignores how similar the candidates are. Models from a hyperparameter search make highly correlated predictions, so the real selection effect is smaller than the bound suggests.
What you should remember#
- Markov: for . Chebyshev: .
- Hoeffding: for independent , , exponentially small in .
- All three are Markov’s inequality applied to a function of : itself, its square, its exponential.
- To estimate a proportion within at 95%: 4,612 examples by Hoeffding, about 2,401 by the normal approximation.
- With candidates, every validation score is within of the truth with probability at least ; the best of many looks better than it is.
Code#
The bounds against exact values, the sample sizes, and the selection effect by simulation:
import numpy as np, math
from scipy import stats
# Markov and Chebyshev against the truth
print(round(0.2 / 2, 2), f'{math.exp(-10):.1e}') # 0.1 4.5e-05: Markov's bound vs Exp(mean 0.2)
print([round(1 / k**2, 3) for k in (2, 3)], [round(float(2 * stats.norm.sf(k)), 4) for k in (2, 3)]) # [0.25, 0.111] [0.0455, 0.0027]
# the share of heads in n flips off by at least 0.1: exact, Chebyshev, Hoeffding
for n in (100, 1000):
S = stats.binom(n, 0.5)
exact = S.cdf(int(0.4 * n)) + S.sf(int(0.6 * n) - 1)
print(n, f'{exact:.1e}', 25 / n, f'{2 * math.exp(-0.02 * n):.1e}')
# 100 5.7e-02 0.25 2.7e-01
# 1000 2.7e-10 0.025 4.1e-09
# examples needed to estimate an accuracy within 0.02 with probability 0.95: Hoeffding, Chebyshev, normal approximation
print(math.ceil(math.log(2 / 0.05) / (2 * 0.02**2)), math.ceil(0.25 / (0.02**2 * 0.05)), math.ceil((1.96 * 0.5 / 0.02) ** 2)) # 4612 12500 2401
# the best of K = 1,000 identical models (true accuracy 0.8), each on 10,000 validation examples, 2,000 times
rng = np.random.default_rng(0)
best = (rng.binomial(10_000, 0.8, size=(2000, 1000)) / 10_000).max(axis=1)
eps = math.sqrt((math.log(1000) + math.log(40)) / (2 * 10_000))
print(round(float(best.mean()), 4), round(eps, 4), float((best - 0.8 > eps).mean())) # 0.8129 0.023 0.0
Next#
Chebyshev’s inequality already showed that averages approach the mean in probability. The next note states the law of large numbers in its weak and strong forms, explains the difference between the two kinds of convergence, and turns the law into a computational method: Monte Carlo estimation of any expectation by averaging samples.
References#
- W. Hoeffding. Probability Inequalities for Sums of Bounded Random Variables. Journal of the American Statistical Association, 58(301), 1963.
- S. Boucheron, G. Lugosi, P. Massart. Concentration Inequalities: A Nonasymptotic Theory of Independence. Oxford University Press, 2013. Chapter 2 (basic inequalities, the Chernoff method, Hoeffding’s lemma).
- S. Shalev-Shwartz, S. Ben-David. Understanding Machine Learning: From Theory to Algorithms. Cambridge University Press, 2014. Chapters 2–4 (finite hypothesis classes and uniform convergence).
- J. K. Blitzstein, J. Hwang. Introduction to Probability, 2nd ed. CRC Press, 2019. Chapter 10 (inequalities and limit theorems).