Foundations

Concentration Inequalities and Why Test Set Estimates Can Be Trusted

Use Hoeffding, Chebyshev and Bernstein bounds to say how far a test score can stray from the truth, and learn when they fail.

Key takeaways

  • Each test mistake is a 0 or 1 variable, so the error rate is an average that concentrates around the true rate.
  • Hoeffding's bound shrinks like 1/√n and needs only independence and bounded values.
  • Using variance information gives tighter intervals when the error rate is far from one half.
  • Comparing many models on one test set widens the guarantee, through the logarithm of their number.
  • The bounds say nothing about data unlike the test set or about models tuned against it.

A test set score can be trusted because averages of many independent, well-behaved quantities rarely stray far from their expected value. Concentration inequalities make "rarely" and "far" precise. With 1,000 independent test examples and a model chosen without looking at them, Hoeffding's inequality says the measured error rate is within about 4.3 percentage points of the true rate with at least 95% probability.

The same tools show exactly what breaks that trust: dependence, reuse of the test set, and quantities with heavy tails.

The setup

Fix a trained model. For test example i, let Zᵢ be 1 if the model errs and 0 otherwise. If examples are independent draws from the distribution you care about, then each Zᵢ is 1 with the same probability p, the true error rate.

The measured error is p̂ = (Z₁ + ... + Zₙ)/n. Its expected value is p and its variance is p(1 − p)/n. We want a limit on the probability that |p̂ − p| ≥ ε.

The condition "fix a trained model" matters. If the model was chosen using these examples, the Zᵢ are no longer independent trials of a fixed rule.

Markov and Chebyshev

Markov's inequality is the root: for a non-negative variable V and a > 0, P(V ≥ a) ≤ E[V]/a. A non-negative quantity cannot often be much larger than its average.

Apply it to the squared deviation and you get Chebyshev's inequality:

P( |p̂ − p| ≥ ε ) ≤ p(1 − p) / (n ε²)

With p = 0.9, n = 1,000 and a failure probability δ = 0.05, solve 0.09/(1000·ε²) = 0.05. That gives ε² = 0.0018 and ε ≈ 0.042.

Chebyshev needs only a finite variance, which makes it general. Its drawback is that the required ε grows like 1/√δ, so high confidence is expensive.

Hoeffding's inequality

If the Zᵢ are independent and lie between 0 and 1:

P( |p̂ − p| ≥ ε ) ≤ 2·exp(−2 n ε²)
solve for ε:   ε = √( ln(2/δ) / (2n) )
solve for n:   n ≥ ln(2/δ) / (2ε²)

For δ = 0.05 and n = 1,000, ln 40 ≈ 3.689, and ε = √(3.689/2000) ≈ 0.043. For n = 10,000, ε ≈ 0.014. Ten times the data narrows the interval by a factor of about 3.2, which is √10.

The idea behind the proof is to apply Markov's inequality to exp(t·(p̂ − p)), use independence to factor the expectation into one term per example, bound each term, and pick the best t. Exponentials punish large deviations so strongly that the tail falls exponentially.

Example: 95% half-widths at n = 1,000 when the true error rate is 0.9
Chebyshev 0.04error rate Hoeffding 0.04error rate Bernstein 0.03error rate Normal approximation 0.02error rate Chebyshev 0.04error rate Hoeffding 0.04error rate Bernstein 0.03error rate Normal approximation 0.02error rate

Hypothetical example: the numbers are made up to show the method, not a quote or an average.

Using the variance

Hoeffding ignores p. Yet an error rate near 0 or 1 varies much less than one near 0.5. Bernstein's inequality uses the variance σ² = p(1 − p). For variables that stay within 1 of their mean:

P( |p̂ − p| ≥ ε ) ≤ 2·exp( −n ε² / (2σ² + 2ε/3) )

With p = 0.9 (so σ² = 0.09), n = 1,000 and δ = 0.05, solving the quadratic gives ε ≈ 0.027, much tighter than Hoeffding.

The familiar normal approximation, ε ≈ 1.96·√(p(1 − p)/n), gives about 0.019 here. It is an approximation based on the central limit theorem, not a guarantee, and it degrades when n·p or n·(1 − p) is small. In practice you plug in p̂ for p; empirical Bernstein bounds account for that properly. For proportions, a Wilson or exact binomial interval is usually a good practical choice. Confidence intervals for test metrics compares them.

Check coverage by simulation

import numpy as np

rng = np.random.default_rng(0)
p, n, delta, trials = 0.1, 1000, 0.05, 100000   # p = error rate
p_hat = rng.binomial(n, p, size=trials) / n     # shape (trials,)

hoeff = np.sqrt(np.log(2 / delta) / (2 * n))
normal = 1.96 * np.sqrt(p * (1 - p) / n)
for name, eps in [("hoeffding", hoeff), ("normal", normal)]:
    print(name, round(eps, 4), np.mean(np.abs(p_hat - p) > eps))

The normal interval misses roughly 5% of the time. The Hoeffding interval misses far less often than 5%, which shows the price of a guarantee that covers every p.

Many models, one test set

If you evaluate k models fixed in advance, the chance that at least one estimate is off by ε or more is at most the sum of the individual chances. This union bound gives:

ε = √( ln(2k/δ) / (2n) )

With n = 1,000, k = 100 and δ = 0.05, ε = √(8.294/2000) ≈ 0.064, compared with 0.043 for a single model. The cost is logarithmic, but real: if the 100 models differ by less than this, picking the winner is partly picking luck.

It gets worse when later models are designed after seeing earlier test scores. The set of candidates then depends on the data, and counting the models you happened to try understates the freedom you had. Methods for adaptive reuse exist, such as releasing only coarse or noisy scores, but the simplest remedy is a fresh test set for the final claim.

Comparing two models on the same examples

Often you care about a difference. For each test example, let Dᵢ be A's error minus B's error: +1 if only A is wrong, −1 if only B is wrong, and 0 if they agree on correctness.

Say that on 1,000 examples, only A is wrong 40 times and only B is wrong 20 times.

mean of D           = (40 − 20)/1000           = 0.020
mean of D²          = (40 + 20)/1000           = 0.060
variance of D       ≈ 0.060 − 0.020²           = 0.0596
standard error      = √(0.0596/1000)           ≈ 0.0077
95% normal interval = 0.020 ± 1.96 × 0.0077    ≈ 0.005 to 0.035

The interval excludes zero, which suggests B makes fewer errors on this distribution. Because D ranges over a width of 2, plain Hoeffding would give a half-width of about 0.086 and could not separate them. It cannot use the fact that D is zero on 94% of examples. A variance-aware bound or a paired method uses that information.

Two separate intervals, one around each error rate, would also be wasteful. Much of their noise is shared, since hard examples tend to be hard for both models, and it cancels in the difference.

The same effect during training

Concentration also explains mini-batch gradients. A gradient computed on B randomly chosen training examples is an average of B per-example gradients, so it concentrates around the full training set gradient. Its typical noise falls like 1/√B.

Going from a batch of 32 to 128 multiplies the computation per step by four and halves the noise. That diminishing return is one reason very large batches are not automatically more efficient per example. The same reasoning applies to any Monte Carlo estimate: random projections, sampled softmax or averaging several dropout passes.

Beyond error rates

Many metrics are not simple averages. For F1 or AUC, McDiarmid's inequality applies when changing a single example can move the metric only by a small bounded amount. For ratio metrics with small denominators, that amount may be large, and bootstrapping is the usual practical route. See bootstrap resampling.

Losses such as squared error can be unbounded. A few enormous errors may dominate the average, and then Hoeffding does not apply. Heavy-tailed losses call for clipping with a stated threshold, robust estimators such as the median of means, or reporting quantiles.

When trust is misplaced

Problem Why the bound fails
Test set used to tune Model depends on the data being scored
Repeated rows per person Terms are not independent
New region or season The target p has changed
Wrong labels You are estimating agreement with noisy labels
Reporting only the best run Selection among many outcomes

Dependence deserves special care, because it is easy to miss. If 1,000 rows come from 20 patients, the effective number of independent terms may be far nearer 20 than 1,000. Apply the inequality to per-patient averages when patients are independent, and accept the wider result. The i.i.d. assumption shows how to estimate the damage.

The bound is a statement about sampling noise alone. It cannot tell you whether your metric reflects what matters, or whether the test data resemble deployment.

Sizing a test set

Decide the smallest difference you care about, then solve for n. To pin an error rate within ±0.02 at 95% confidence by Hoeffding, n ≥ ln 40/(2 × 0.0004) ≈ 4,611.1, so 4,612 examples. If you know the error rate is small, a variance-based calculation will ask for fewer. Apply the requirement to each subgroup you intend to report.

Common questions

What does "95% probability" refer to?

It refers to the random draw of the test set. Before sampling, the chance that the procedure produces an estimate within ε of p is at least 95%.

Which bound should I report?

For an ordinary evaluation report, a Wilson, exact binomial or bootstrap interval is usually appropriate and tighter than Hoeffding. Say which method you used and how many independent units it was based on. Keep Hoeffding-style bounds for sizing studies in advance, for proofs, and for cases where you want protection without assuming anything about the rate.

Why does the bound ignore how large the model is?

Because the model is fixed before the test examples are drawn, each test point is just one trial of that rule. Model size matters for the gap between training error and true error, where the model was chosen using the very data being averaged.

Written for 4MachineLearning with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 42 articles in Foundations →

Latest news on this topic