Concentration inequalities: Chernoff, Hoeffding and sample sizes
Before this lesson: lesson 06 (Markov, Chebyshev, the law of large numbers and the CLT) and lesson 03 (moment generating functions). Convexity and Taylor's theorem from calculus (calculus 03) are used in one proof. Time: about 60 minutes, plus the notebook.
Why this matters #
- How big must a test set be? Hoeffding answers with a number that holds for every model and every data distribution: 1,060 examples for an error below 0.05 with probability 0.99.
- Generalisation bounds. "Training error is close to test error" is a concentration statement. For a finite set of models it follows from Hoeffding and the union bound; VC dimension and Rademacher complexity extend it to infinite model classes (deep learning 17).
- Test-set reuse and leaderboards. Each time you pick the best of models on the same test set, its score is biased upwards by about . This is why benchmarks keep a hidden test set.
- Bandits and reinforcement learning. The UCB algorithm adds the Hoeffding width to each arm's mean reward (reinforcement learning 03).
- Randomised algorithms in high dimension. Random projections (the Johnson–Lindenstrauss lemma), randomised SVD and sketching rely on sub-Gaussian concentration (linear algebra 18).
1. Predict first #
2. The picture #
Flip a fair coin times. What is the probability of 60 or more heads, that is ? Here is the exact value next to what each tool says. Section 3 derives every bound.
| method | value | kind |
|---|---|---|
| Markov, | 0.833 | bound |
| Chebyshev, | 0.25 | bound |
| Cantelli (one-sided Chebyshev) | 0.20 | bound |
| Hoeffding, | 0.135 | bound |
| Chernoff, | 0.134 | bound |
| exact binomial, | 0.0284 | exact |
| CLT, | 0.0228 | approximation |
At one the differences look small. The real difference is how the bounds change with . Chebyshev falls like : a polynomial tail. Hoeffding and Chernoff fall like : an exponential tail, the same rate as the exact probability. On a log scale an exponential tail is a straight line.
Move and and compare the curves on the log scale. Blue is the exact binomial tail, dashed orange the CLT approximation. The bounds: Markov (grey), Chebyshev (violet), Hoeffding (aqua) and Chernoff (magenta). Notice: at Chebyshev still gives 0.05 while the truth is about ; the exponential bounds stay parallel to the truth. Then set : Chernoff becomes much better than Hoeffding, because it uses the small variance of a rare event.
Where does the exponential come from? Markov's inequality uses only the mean. It can be applied to any nonnegative increasing function of the variable. If we apply it to , the bound uses the whole moment generating function, and independence makes that function a product. The free parameter is then chosen to make the bound as small as possible.
Move to the lowest point of the orange curve. The curve is the bound for each ; every point on it is a valid bound. The yellow dot is your , the aqua dot the best one, , and the blue line the exact tail. For the Gaussian, and the bound is . Try "Exp(1)" with : the best Chernoff bound (0.74) is worse than Markov (0.5). Chernoff is strong for large deviations and for sums, not everywhere.
For the mean of fair coin flips, Chebyshev gives and Hoeffding gives . At , about how do they compare?
and . A polynomial tail cannot follow an exponential tail. The exponent is , not : makes the rate slower.
3. Definitions and main results #
Markov and Chebyshev, and why they are loose #
Recall from lesson 06: Markov's inequality for , and Chebyshev's inequality . Both are tight for some two- or three-point distribution, so they cannot be improved without more information. But for an average of many independent variables we have much more information, and both bounds waste it.
Cantelli's inequality (one-sided Chebyshev). For , , always below .
Proof of Cantelli's inequality
- For every : implies , so it implies .
- Markov on : .
- Choose , which minimises the right side. Then .
Chebyshev is not always sharper than Markov. Take (mean 1, variance 1). The true tail is .
| exact | Markov | Chebyshev | Chernoff | |
|---|---|---|---|---|
| 2 | 0.135 | 0.5 | 1 | 0.736 |
| 5 | 0.0067 | 0.2 | 0.0625 | 0.092 |
| 10 | 0.000045 | 0.1 | 0.0123 | 0.0012 |
(Chebyshev here uses .) At Markov wins; for large Chebyshev wins; and only the exponential Chernoff bound follows the exponential decay of the truth.
The Chernoff method #
Theorem (Chernoff bound). For every random variable and every ,
Proof.
- Fix . The map is increasing, so the events and are the same.
- , so Markov gives .
- This holds for every , so it also holds for the infimum over .
Sums of independent variables. Let with i.i.d. By independence, the expectation of a product is the product of the expectations, so . Write for the cumulant generating function (CGF) of the centred variable. With and threshold :
The exponent grows linearly in : the bound decays exponentially. The function is the Legendre transform of ; it is the rate in Cramér's large-deviation theorem.
Example 1: Gaussian. For , . The exponent is a downward parabola; its maximum is at , with value . So
For one standard Gaussian at : the bound is , the truth is 0.0228. The exponent is right; the bound misses only a factor of order .
Example 2: Bernoulli. For Bernoulli() and , the optimal is , and the bound becomes
where is the Kullback–Leibler divergence between two Bernoulli distributions (information theory 03). This is the 0.134 in the table.
The Chernoff bound for the Gaussian is at . If you use instead, what do you get?
Every gives a valid bound, . At the exponent is , which is larger than , so the bound is weaker. The optimum only makes the valid bound as small as possible.
Hoeffding's inequality #
To use the Chernoff method we need an upper bound on the MGF. For bounded variables, Hoeffding's lemma gives one.
Hoeffding's lemma. If , then for every ,
A bounded variable has an MGF no larger than that of a Gaussian with variance .
Proof of Hoeffding's lemma
Shift so that ; then .
- The function is convex, so on it lies below its chord: .
- Take expectations and use : .
- Write and . The right side equals with .
- and . The second derivative is with , so .
- Taylor's theorem with the remainder: .
Theorem (Hoeffding's inequality). Let be independent with , and . Then for every ,
Proof.
- Chernoff: for every .
- Independence: .
- Hoeffding's lemma on each factor: , with .
- So for every .
- The exponent is a parabola in , smallest at , where it equals .
For the mean of i.i.d. variables in , put and , and add the same bound for the lower tail (apply the theorem to ):
Hoeffding uses only the range , not the variance. That makes it simple and universal, but loose for rare events. For , and , the one-sided Hoeffding bound is 0.82, the Chernoff (KL) bound 0.020 and the exact tail 0.0033.
Bernstein's inequality: using the variance
If and , then
For small the exponent is about : the Gaussian rate with the true variance. In the rare-event example (, ) Bernstein gives 0.023, close to Chernoff and far below Hoeffding's 0.82. Empirical Bernstein bounds use the sample variance instead of .
Sub-Gaussian variables #
Boundedness was used at only one place: step 3, to bound each factor by a Gaussian MGF. So the proof works for every variable with such a bound.
Definition. A random variable with mean is sub-Gaussian with variance proxy if
- is sub-Gaussian with proxy (with equality).
- A variable in is sub-Gaussian with proxy (Hoeffding's lemma).
- A Rademacher variable (a random sign ) has proxy 1, because (compare the Taylor series term by term: ).
- Exponential, Poisson and gamma variables are not sub-Gaussian. For Exp(1), is infinite for . These are sub-exponential. Heavy-tailed variables (Student's , Pareto) are neither.
The Chernoff method turns the definition into the tail bound . Variance proxies of independent variables add (multiply the MGF bounds), so the mean of i.i.d. copies has proxy , and
With this is exactly Hoeffding again.
Equivalent definitions
Vershynin defines sub-Gaussian variables by the norm . The tail definition (), the moment definition (), and the MGF definition above are equivalent up to absolute constants (his Proposition 2.6.1). In the same way, sub-exponential variables have tails , and the square of a sub-Gaussian variable is sub-exponential.
Which of these variables is not sub-Gaussian?
A die takes values in , so Hoeffding's lemma gives proxy . A random sign has proxy 1, and a Gaussian is sub-Gaussian with its own variance. The exponential MGF is infinite for , so it cannot be bounded by : its tail is heavier than any Gaussian tail.
From a bound to a sample size #
Set the bound equal to and solve for . For values in :
- Hoeffding: .
- Chebyshev with the worst-case variance : .
- CLT (an approximation, not a guarantee): .
| Hoeffding | Chebyshev | CLT | ||
|---|---|---|---|---|
| 0.05 | 0.05 | 738 | 2,000 | 385 |
| 0.05 | 0.01 | 1,060 | 10,000 | 664 |
| 0.05 | 0.001 | 1,521 | 100,000 | 1,083 |
| 0.01 | 0.05 | 18,445 | 50,000 | 9,604 |
All methods grow like : half the error costs four times the data. But in , Hoeffding grows like and Chebyshev like . With an exponential tail, high confidence is cheap.
Choose and , then press Simulate. The widget runs 1,000 experiments of size and plots their errors (blue); errors beyond the orange lines are magenta. With the Hoeffding , the failure rate is always below , usually far below (it is a worst case over all distributions on ). Switch "Use n from" to CLT: for the fair coin the failure rate moves up to about .
The union bound and a generalisation bound #
Union bound. For any events (independent or not),
(Proof by induction on . For two events, , and the last term is .)
To make statements hold at the same time with probability , give each one the failure probability . With Hoeffding, this replaces by . The cost of statements is only an additive in the numerator, because the tail is exponential.
Setting. A model (hypothesis) has a loss on an example (for example, the 0/1 error). Its true risk is and its empirical risk on i.i.d. examples is .
Theorem (finite-class generalisation bound). Let be a finite set of models, fixed before the data are seen. With probability at least , for all at once,
Proof.
- For one fixed , is the mean of i.i.d. values in with mean . Hoeffding: .
- Union bound over the models: .
- Set this equal to and solve: .
Consequence for model selection. If minimises over (empirical risk minimisation, or "pick the best on the test set"), then with probability at least , . Step 1 needed a model that is fixed before the data: the selected depends on the data, so it is covered only through the union bound over all models.
Press "Run 50 experiments" and change . classifiers guess at random, so each has true accuracy 0.5. The blue curve is the best test accuracy among the first ; it grows with . The dashed orange curve is the bound with ; the blue curves almost never cross it. The aqua dot is the selected classifier on a fresh test set: back near 0.5. The high test score was selection, not skill.
You compare models on one test set and want all 1000 accuracy estimates to be within of the truth with probability 0.95. Which failure probability must each single estimate have, and how much larger is than for one model?
The union bound gives each estimate . Hoeffding needs , so scales with : against . For this is 13,246 examples instead of 4,612.
Concentration in high dimension
Let . Then is a sum of independent terms with mean 1, so and . Concentration says more: is sub-Gaussian with a variance proxy that does not depend on (Vershynin, Theorem 3.1.1). A simulation gives the mean norms 3.08, 9.96 and 31.6 for , with SD about 0.71 in each case. In high dimension a Gaussian is concentrated on a thin shell of radius , not near its mode at 0. Random projections and the Johnson–Lindenstrauss lemma use exactly this (linear algebra 18).
Answers to section 1. (1) Much less. Hoeffding needs , so grows by the factor : 43% more data (1,060 → 1,521 for ). With Chebyshev, would grow ten times. (2) No. For Exp(1) at , Markov gives 0.5 and Chebyshev gives 1. Chebyshev wins only for large deviations. (3) About 0.55. A simulation gives a mean best accuracy of 0.551. The union bound says that, with probability 0.95, no classifier exceeds .
4. Worked example #
(a) A guaranteed sample size. A web page has an unknown click rate . How many visitors guarantee , for every ?
- Clicks are 0/1 values, so , and .
- Hoeffding: is the same as , that is .
- , so : 738 visitors.
- Compare: the CLT suggests 385 (an approximation, from lesson 06) and Chebyshev guarantees it with 2,000. Ratio CLT : Hoeffding : Chebyshev ≈ 1 : 2 : 5.
(b) The range matters. Rewards in a game lie in . With episodes, bound .
- and .
- Hoeffding: .
- Forgetting the range (using ) would claim an absurdly small probability.
(c) A test set and a leaderboard. A test set has examples, and .
- One model, fixed in advance: with probability 0.95, its test accuracy is within of its true accuracy.
- The best of models on the same test set: replace by . The width becomes .
- 100 times more models increase the width only by the factor 1.5, but the width never goes away: a reported "best of 100" score can be optimistic by up to 0.02.
5. In code #
SciPy checks the numbers of sections 2–4. The notebook builds the bounds yourself.
import numpy as np
from scipy import stats
# coin: P(at least 60 heads in 100 flips) and five bounds
p, n, eps = 0.5, 100, 0.1
q = p + eps
kl = q * np.log(q / p) + (1 - q) * np.log((1 - q) / (1 - p))
v = p * (1 - p) / n
print(round(stats.binom.sf(59, n, p), 4), round(stats.norm.sf(2), 4)) # 0.0284 0.0228
print(round(p / q, 3), round(v / eps**2, 3), round(v / (v + eps**2), 3),
round(np.exp(-2 * n * eps**2), 3), round(np.exp(-n * kl), 3)) # 0.833 0.25 0.2 0.135 0.134
# Hoeffding sample sizes and the union bound
hoeff_n = lambda eps, delta: int(np.ceil(np.log(2 / delta) / (2 * eps**2)))
print(hoeff_n(0.05, 0.05), hoeff_n(0.05, 0.01), hoeff_n(0.05, 0.001)) # 738 1060 1521
gap = lambda k, n, delta: np.sqrt(np.log(2 * k / delta) / (2 * n))
print(round(gap(1, 10_000, 0.05), 4), round(gap(100, 10_000, 0.05), 4)) # 0.0136 0.0204
# best of 1000 random classifiers on 1000 test examples
rng = np.random.default_rng(0)
best = rng.binomial(1000, 0.5, size=(2000, 1000)).max(axis=1) / 1000
print(round(best.mean(), 3), np.mean(best - 0.5 > gap(1000, 1000, 0.05))) # about 0.551 and 0.0025
The last line is a simulation: the mean best accuracy has a Monte Carlo error of about 0.0003, and the fraction of runs above the bound is far below .
6. Where it shows up #
- Test-set size. To estimate an accuracy within with probability 0.95 for every model, Hoeffding asks for 18,445 test examples (the CLT suggests 9,604).
- PAC learning. The finite-class bound is the simplest probably approximately correct (PAC) statement. VC dimension and Rademacher complexity replace for infinite classes (deep learning 17).
- Adaptive data analysis. Every decision based on a test set (early stopping, hyperparameter search, choosing the architecture) adds to . Kaggle-style leaderboards and "test-set overfitting" are union-bound effects.
- Multi-armed bandits. UCB and successive elimination use Hoeffding intervals for each arm (reinforcement learning 03).
- Monte Carlo with a guarantee. For an integrand with values in , Hoeffding gives a sample size that holds without any CLT assumption; for rare events, Chernoff or Bernstein give much smaller ones.
- High-dimensional data. Norms and distances of random vectors concentrate; random projections preserve distances (Johnson–Lindenstrauss); random matrix bounds follow the same Chernoff pattern (linear algebra 18).
7. Common mistakes #
- Treating the CLT as a bound. The CLT gave 0.0228 for the coin, below the exact 0.0284. When you need a guarantee, use a concentration inequality.
- Hoeffding with the wrong range. The exponent has . For values in it is , not . Hoeffding does not apply to unbounded variables at all.
- Forgetting the factor 2 of the two-sided bound, or the union bound when you report many metrics or select among many models.
- Applying the bound to a model chosen on the same data. The single-model Hoeffding bound needs a model fixed before the data; after selection, use the union bound (or a fresh test set).
- "Chebyshev is always sharper than Markov." False: Exp(1) at gives 0.5 for Markov and 1 for Chebyshev.
- Calling the exponential distribution sub-Gaussian. Its MGF is infinite for ; it is sub-exponential.
- Using Hoeffding for rare events. It ignores the variance. For use the Chernoff (KL) or Bernstein bound.
8. Check yourself #
- Values in , , . What does Hoeffding require?
Answer
, so . - Where does the proof of Hoeffding's inequality use independence, and where boundedness?
Answer
Independence: to write as a product (step 2). Boundedness: in Hoeffding's lemma, to bound each factor by (step 3). Any sub-Gaussian bound on the factors would do. - Derive the Chernoff bound for at threshold .
Answer
, so . The exponent is smallest at , giving . - 100 rolls of a fair die. Use Hoeffding to bound , and compare with Chebyshev.
Answer
Range , so : . Chebyshev with : . Here Chebyshev wins: the variance is much smaller than the worst case that Hoeffding assumes, and is not large. - Show that a random sign (probability each) is sub-Gaussian with proxy 1.
Answer
, because . - A finite class has models. With examples and , what is the uniform gap?
Answer
. A million models cost only about twice the width of one model (0.0136).
State the Chernoff bound.
: Markov's inequality applied to , then optimised over .
State Hoeffding's inequality for the mean of i.i.d. variables in .
.
State Hoeffding's lemma.
If , then for all .
Define a sub-Gaussian variable with variance proxy .
for all .
Is the exponential distribution sub-Gaussian? Why?
No. Its MGF is infinite for , so its tail is heavier than any Gaussian tail; it is sub-exponential.
What sample size does Hoeffding give for error with probability (values in )?
; for , : 1,060.
Why is high confidence cheap with Hoeffding but expensive with Chebyshev?
Hoeffding's grows like (exponential tail); Chebyshev's grows like (polynomial tail).
State the union bound.
, for any events.
State the finite-class generalisation bound.
For models and a loss in , with probability : for all at once.
9. Interview angle #
- "How many samples do you need to estimate a probability within ±1% at 95% confidence?" — CLT, worst case : . A guarantee from Hoeffding: .
- "Derive Hoeffding's inequality." — Chernoff (Markov on ), factorise by independence, bound each factor by Hoeffding's lemma, optimise the parabola in .
- "Give a generalisation bound for a finite hypothesis class." — Hoeffding plus the union bound: for all , with probability .
- "Why is the best leaderboard score optimistic?" — It is the maximum of noisy estimates on one test set. The maximum is biased upwards by up to about .
- "When is Hoeffding a poor choice?" — Rare events or small variance (use Chernoff/KL or Bernstein), and unbounded or heavy-tailed data (Hoeffding does not apply).
10. Practice #
The notebook for this lesson has four exercises, each with a check cell and a folded solution:
bernoulli_tail_bounds: Markov, Chebyshev, Cantelli, Hoeffding and Chernoff for a coin mean, checked against the exact binomial tail on a grid (every bound must stay above it).- A numerical Chernoff bound for any CGF, checked against the Gaussian and Bernoulli closed forms.
hoeffding_sample_size, with the tests and a range of width 2.- The best of random classifiers on one test set, compared with the union bound.
11. Watch and read #
- Roman Vershynin, High-Dimensional Probability, 2nd edition (free PDF on the author's page): §2.1–2.3 (why concentration, Hoeffding, Chernoff), §2.6 (sub-Gaussian distributions), §3.1 (the norm of a Gaussian vector).
- Mehryar Mohri, Afshin Rostamizadeh and Ameet Talwalkar, Foundations of Machine Learning, 2nd edition (free PDF from the authors): ch. 2 (PAC learning, the bound for a finite hypothesis set) and appendix D (concentration inequalities).
- Joseph Blitzstein and Jessica Hwang, Introduction to Probability, 2nd edition (free PDF): §10.1 (inequalities).
Runs in your browser with nothing to install. The notebook repeats the key demos and has exercises with checks; solutions are inside, folded.
No Google account needed: Python runs inside your browser. The first start downloads about 40 MB; your changes are saved in this browser only.