Skip to content
BIASED

Inequalities and concentration

Markov's, Chebyshev's and Hoeffding's inequalities — how far an average can stray from its mean, how many examples you need, and why the best of K models looks better than it is.

How far can an average stray

The topic Conditional expectation found the best predictor m(X)=E[Y∣X]m(X) = \mathbb E[Y \mid X] and split the variance into an explained part and noise. All of it was defined through a joint distribution that we treated as known. In practice we see a distribution only through a sample, and almost every number we report is an average: the error rate on a test set, the mean loss, a conversion rate.

Here is the setup for the whole topic. Let X1,…,XnX_1, \dots, X_n be independent, each with mean μ\mu and variance σ2\sigma^2, and let their average be

Xˉn=X1+⋯+Xnn.\bar X_n = \frac{X_1 + \dots + X_n}{n}.

Linearity of expectation and the fact that variances of independent variables add (the topics Expectation and variance and Joint distributions and independence) give

E[Xˉn]=μ,Var⁡(Xˉn)=σ2n.\mathbb E[\bar X_n] = \mu, \qquad \operatorname{Var}(\bar X_n) = \frac{\sigma^2}{n}.

The running example is a classifier with an unknown true error rate μ\mu. On test example ii, let Xi=1X_i = 1 if the classifier makes a mistake and Xi=0X_i = 0 otherwise. Then Xi∼Bern⁡(μ)X_i \sim \operatorname{Bern}(\mu) with variance μ(1−μ)\mu(1 - \mu), and Xˉn\bar X_n is the measured error rate.

The variance gives the typical size of the error, but not the probability of a large one. This topic asks for a bound on

P(∣Xˉn−μ∣≥ε)P(|\bar X_n - \mu| \ge \varepsilon)

when the distribution of XiX_i is unknown and we know only its mean, its variance or its range. Each answer will be an inequality that holds for every distribution with that information. Each one asks for a little more information and gives a much better bound.

Markov's inequality

Start with the least information: a variable that is never negative, and its mean.

Markov's inequality. If X≥0X \ge 0 and a>0a > 0, then

P(X≥a)≤E[X]a.P(X \ge a) \le \frac{\mathbb E[X]}{a}.

The proof takes one line. Write 1{X≥a}\mathbf 1\{X \ge a\} for the indicator that equals 11 when X≥aX \ge a and 00 otherwise. Since X≥0X \ge 0, it is always true that

a 1{X≥a}≤X.a\,\mathbf 1\{X \ge a\} \le X.

When the indicator is 11, the left side is a≤Xa \le X. When it is 00, the left side is 0≤X0 \le X. Taking expectations of both sides gives a P(X≥a)≤E[X]a\,P(X \ge a) \le \mathbb E[X].

An example. A service's response time averages 0.20.2 s. Then at most 0.2/2=0.10.2/2 = 0.1 of requests take 22 s or more, whatever the distribution. If the times are in fact exponential with that mean, the true share is e−10≈4.5⋅10−5e^{-10} \approx 4.5 \cdot 10^{-5}. The bound is off by more than three orders of magnitude.

Still, it cannot be improved using the mean alone. Let X=aX = a with probability E[X]/a\mathbb E[X]/a and X=0X = 0 otherwise: for this distribution the inequality is an equality. And nonnegativity matters. Without it, large values could be balanced by negative ones, and the mean would say nothing about the right tail.

Chebyshev's inequality

Markov's inequality ignores how spread out XX is. The fix is to apply it to another nonnegative variable, the squared deviation (X−μ)2(X - \mu)^2, whose mean is the variance σ2\sigma^2.

The events ∣X−μ∣≥kσ|X - \mu| \ge k\sigma and (X−μ)2≥k2σ2(X - \mu)^2 \ge k^2\sigma^2 are the same event. Markov's inequality with a=k2σ2a = k^2\sigma^2 gives Chebyshev's inequality:

P(∣X−μ∣≥kσ)≤σ2k2σ2=1k2.P(|X - \mu| \ge k\sigma) \le \frac{\sigma^2}{k^2\sigma^2} = \frac{1}{k^2}.

In another form, P(∣X−μ∣≥ε)≤σ2/ε2P(|X - \mu| \ge \varepsilon) \le \sigma^2/\varepsilon^2 for every ε>0\varepsilon > 0.

So any distribution with finite variance puts at most 1/41/4 of its probability two or more standard deviations from the mean, and at most 1/9≈0.1111/9 \approx 0.111 three or more. For a normal distribution these shares are 0.04550.0455 and 0.00270.0027. Again the worst case is far from the typical one.

Now apply the inequality to the average. Its variance is σ2/n\sigma^2/n, so

P(∣Xˉn−μ∣≥ε)≤σ2nε2.P(|\bar X_n - \mu| \ge \varepsilon) \le \frac{\sigma^2}{n\varepsilon^2}.

Keep this line in mind; it will matter at the end of the topic. Note also how slowly it shrinks: only like 1/n1/n. For bounded variables we can do much better.

Hoeffding's inequality

Most averages in ML are averages of bounded variables. A mistake indicator is 00 or 11, a click happens or not, a loss can be clipped to [0,1][0, 1]. Boundedness is much stronger information than a finite variance, and it buys a bound that falls exponentially in nn.

Hoeffding's inequality. If X1,…,XnX_1, \dots, X_n are independent and every XiX_i lies in [a,b][a, b], then

P(∣Xˉn−μ∣≥ε)≤2exp⁡ ⁣(−2nε2(b−a)2).P(|\bar X_n - \mu| \ge \varepsilon) \le 2\exp\!\left(-\frac{2n\varepsilon^2}{(b - a)^2}\right).

Here aa and bb are the ends of the range, not Markov's threshold. The variables need not have the same distribution; they must be independent and bounded. The width b−ab - a plays the role of the spread.

Compare the bounds on a fair coin. Toss it nn times: how likely is the share of heads to be off 0.50.5 by at least 0.10.1? Here σ2=1/4\sigma^2 = 1/4, so Chebyshev gives 25/n25/n, and Hoeffding with b−a=1b - a = 1 gives 2e−0.02n2e^{-0.02n}. The exact probability comes from the binomial distribution.

nn Chebyshev Hoeffding Exact
100100 0.250.25 0.270.27 0.0570.057
10001000 0.0250.025 4.1⋅10−94.1 \cdot 10^{-9} 2.7⋅10−102.7 \cdot 10^{-10}

At n=100n = 100 Hoeffding even loses to Chebyshev. At n=1000n = 1000 Chebyshev is off by a factor of about 10810^8, while Hoeffding is within a factor of 1515. As soon as nn is large, the exponential wins.

How many examples you need

Now turn the question around. How many examples do we need for the measured error rate to be within ε\varepsilon of the truth with probability at least 1−δ1 - \delta? Set a bound equal to δ\delta and solve for nn.

Chebyshev gives n≥σ2/(δε2)n \ge \sigma^2/(\delta\varepsilon^2), and for a 0/10/1 variable σ2≤1/4\sigma^2 \le 1/4. Hoeffding with b−a=1b - a = 1 requires 2e−2nε2≤δ2e^{-2n\varepsilon^2} \le \delta, which gives

n≥ln⁡(2/δ)2ε2.n \ge \frac{\ln(2/\delta)}{2\varepsilon^2}.

For a margin of ε=0.02\varepsilon = 0.02 with probability 0.950.95 (δ=0.05\delta = 0.05):

Method Examples Assumes
Chebyshev, σ2≤1/4\sigma^2 \le 1/4 12,50012{,}500 finite variance
Hoeffding 4,6124{,}612 independent, bounded
Normal approximation 2,4012{,}401 large nn

The last row comes from the central limit theorem, two topics ahead. It is an approximation, not a guarantee.

The two guarantees differ most in how they depend on δ\delta. Chebyshev's nn grows like 1/δ1/\delta, Hoeffding's like ln⁡(1/δ)\ln(1/\delta). At δ=0.001\delta = 0.001 Chebyshev asks for 625,000625{,}000 examples and Hoeffding for 9,5029{,}502.

Where the exponent comes from

Where does Hoeffding's exponential come from? It is Markov's inequality again, applied to an exponential. This argument is called the Chernoff method.

Let S=∑i=1n(Xi−μ)S = \sum_{i=1}^n (X_i - \mu), so that the upper deviation is the event S≥nεS \ge n\varepsilon. For any t>0t > 0 the function etxe^{tx} is increasing, so this is the same event as etS≥etnεe^{tS} \ge e^{tn\varepsilon}. The variable etSe^{tS} is nonnegative, and Markov's inequality gives

P(S≥nε)≤e−tnε E[etS].P(S \ge n\varepsilon) \le e^{-tn\varepsilon}\,\mathbb E[e^{tS}].

As a function of tt, an expectation like E[etX]\mathbb E[e^{tX}] is called the moment generating function. For independent variables the expectation of a product is the product of expectations, so

E[etS]=∏i=1nE[et(Xi−μ)].\mathbb E[e^{tS}] = \prod_{i=1}^n \mathbb E\big[e^{t(X_i - \mu)}\big].

Each factor is bounded by Hoeffding's lemma, which we take without proof: if X∈[a,b]X \in [a, b], then

E[et(X−EX)]≤et2(b−a)2/8.\mathbb E\big[e^{t(X - \mathbb E X)}\big] \le e^{t^2 (b - a)^2/8}.

Putting the pieces together, for every t>0t > 0,

P(S≥nε)≤exp⁡ ⁣(n(−tε+t2(b−a)28)).P(S \ge n\varepsilon) \le \exp\!\left(n\left(-t\varepsilon + \frac{t^2 (b - a)^2}{8}\right)\right).

The left side does not depend on tt, so we are free to pick the tt that makes the exponent smallest. That choice gives exactly −2nε2/(b−a)2-2n\varepsilon^2/(b - a)^2. The lower tail S≤−nεS \le -n\varepsilon is handled the same way, and the union bound from Kolmogorov's axioms adds the two tails, which is where the factor 22 comes from.

So all three inequalities are one idea: Markov's inequality applied to XX, to (X−μ)2(X - \mu)^2 and to etSe^{tS}. The more of XX the function sees, the tighter the bound. Boundedness is what makes the exponent quadratic in ε\varepsilon.

The best of K models

Back to the classifier. Write R(h)R(h) for the true error rate of a classifier hh, and R^(h)\hat R(h) for its error rate on a validation set of nn examples. If hh is fixed before we look at the validation data, R^(h)\hat R(h) is an average of 0/10/1 indicators, and Hoeffding gives

P(∣R^(h)−R(h)∣≥ε)≤2e−2nε2.P\big(|\hat R(h) - R(h)| \ge \varepsilon\big) \le 2e^{-2n\varepsilon^2}.

In practice we try KK models h1,…,hKh_1, \dots, h_K (architectures, hyperparameters, checkpoints) and keep the one with the lowest validation error. The winner was chosen by the validation data, so the bound for a fixed model does not apply to it. The way out is a bound that holds for all KK models at once. The union bound from Kolmogorov's axioms gives

P(∃ k: ∣R^(hk)−R(hk)∣≥ε)≤2Ke−2nε2.P\big(\exists\, k:\ |\hat R(h_k) - R(h_k)| \ge \varepsilon\big) \le 2K e^{-2n\varepsilon^2}.

Set the right side equal to δ\delta and solve for ε\varepsilon. With probability at least 1−δ1 - \delta, simultaneously for all KK models,

∣R^(h)−R(h)∣≤ln⁡K+ln⁡(2/δ)2n.|\hat R(h) - R(h)| \le \sqrt{\frac{\ln K + \ln(2/\delta)}{2n}}.

This is a first generalization bound. With K=1000K = 1000, n=10,000n = 10{,}000 and δ=0.05\delta = 0.05 the margin is about 0.0230.023. The number of models enters only through ln⁡K\ln K: a thousand candidates widen the margin by a factor of about 1.71.7 compared with a single model.

The other side of the coin is the selection effect. Suppose all 10001000 models have true accuracy exactly 0.80.8, and each is scored on 10,00010{,}000 examples with independent noise. The best measured accuracy then averages about 0.8130.813: more than a point of pure luck. The best of many looks better than it is. That is why the final model is reported on a held-out test set that played no part in the choice.

What comes next

Each inequality in this topic trades information for strength:

What we know Inequality How the bound for Xˉn\bar X_n falls
X≥0X \ge 0 and the mean Markov gains nothing from nn
the variance Chebyshev like 1/n1/n
independent, bounded Hoeffding like e−cne^{-cn}

They have limits. All of them are worst-case bounds. Heavy tails break Hoeffding; there one uses robust estimates such as the median of means. Dependent data, like frames of a video or a time series, call for block methods. When the variance is small relative to the range, Bernstein's inequality is sharper. The union bound ignores how similar the KK candidates are, so the real selection effect is often smaller. And infinite model classes need measures of capacity, which belong to learning theory.

Now look once more at the Chebyshev bound for the average, σ2/(nε2)\sigma^2/(n\varepsilon^2), and ask what it does as nn grows with ε\varepsilon fixed. The answer is the statement that averages settle down around the mean. The next topic, The law of large numbers, proves its weak form with exactly this line, states the strong form, and reads the empirical risk of a model as a sample mean.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.