TExcelTAD

Hoeffding's Inequality

The Chernoff bounds of the previous lesson are multiplicative: they bound deviations of δμ\delta\mu in terms of the mean μ\mu. Often the natural question is additive: is the average of nn bounded measurements within ε\varepsilon of its expectation? Hoeffding's inequality (Wassily Hoeffding, 1963) answers it for any independent variables with bounded ranges, whatever their distributions. This lesson proves it through Hoeffding's lemma, compares it with Chernoff, and uses it to settle the majority-vote bound promised in Amplification by Repetition.

The statement

Theorem (Hoeffding). Let X1,…,XnX_1, \ldots, X_n be independent with ai≤Xi≤bia_i \le X_i \le b_i, and let S=∑iXiS = \sum_i X_i. For every t>0t > 0,

Pr⁡[S−E[S]≥t]≤exp⁡(−2t2∑i=1n(bi−ai)2),\Pr\bigl[S - \mathbb{E}[S] \ge t\bigr] \le \exp\left(-\frac{2t^2}{\sum_{i=1}^n (b_i - a_i)^2}\right),

and the same bound holds for Pr⁡[S−E[S]≤−t]\Pr[S - \mathbb{E}[S] \le -t]. In particular, if every Xi∈[0,1]X_i \in [0, 1] and Xˉ=S/n\bar X = S/n, then

Pr⁡[∣Xˉ−E[Xˉ]∣≥ε]≤2e−2nε2.\Pr\bigl[\lvert \bar X - \mathbb{E}[\bar X] \rvert \ge \varepsilon\bigr] \le 2e^{-2n\varepsilon^2}.

The bound depends only on the ranges, not on the means or variances. That makes it the default tool for "average of bounded samples" questions, such as Monte Carlo estimation of an integral or a probability, and the sample-complexity bounds of learning theory.

Hoeffding's lemma

The proof follows the MGF method of Chernoff Bounds and Their Proofs. The one new ingredient bounds the MGF of a single bounded, centred variable.

Lemma (Hoeffding). If E[Y]=0\mathbb{E}[Y] = 0 and a≤Y≤ba \le Y \le b, then for every real λ\lambda

E[eλY]≤eλ2(b−a)2/8.\mathbb{E}\bigl[e^{\lambda Y}\bigr] \le e^{\lambda^2 (b - a)^2 / 8}.
Proof of the lemma

Since a≤0≤ba \le 0 \le b (the mean is 0) and y↦eλyy \mapsto e^{\lambda y} is convex, on [a,b][a, b] it lies below its chord:

eλy≤b−yb−aeλa+y−ab−aeλb.e^{\lambda y} \le \frac{b - y}{b - a} e^{\lambda a} + \frac{y - a}{b - a} e^{\lambda b}.

Take expectations and use E[Y]=0\mathbb{E}[Y] = 0: E[eλY]≤bb−aeλa−ab−aeλb\mathbb{E}[e^{\lambda Y}] \le \frac{b}{b - a} e^{\lambda a} - \frac{a}{b - a} e^{\lambda b}. Put θ=−ab−a∈[0,1]\theta = \frac{-a}{b - a} \in [0, 1] and u=λ(b−a)u = \lambda(b - a). The right-hand side equals eφ(u)e^{\varphi(u)} with

φ(u)=−θu+ln⁡(1−θ+θeu).\varphi(u) = -\theta u + \ln\bigl(1 - \theta + \theta e^{u}\bigr).

Now φ(0)=0\varphi(0) = 0, φ′(0)=0\varphi'(0) = 0, and φ′′(u)=q(1−q)\varphi''(u) = q(1 - q) with q=θeu1−θ+θeu∈[0,1]q = \frac{\theta e^u}{1 - \theta + \theta e^u} \in [0, 1], so φ′′(u)≤14\varphi''(u) \le \frac{1}{4}. By Taylor's theorem with remainder, φ(u)≤u28=λ2(b−a)28\varphi(u) \le \frac{u^2}{8} = \frac{\lambda^2 (b - a)^2}{8}.

Proof of the theorem. For λ>0\lambda > 0, Markov's inequality and independence give

Pr⁡[S−E[S]≥t]≤e−λt E[eλ(S−E[S])]=e−λt∏i=1nE[eλ(Xi−E[Xi])]≤exp⁡(−λt+λ28∑i(bi−ai)2)lemma, with Y=Xi−E[Xi]\begin{aligned} \Pr\bigl[S - \mathbb{E}[S] \ge t\bigr] &\le e^{-\lambda t}\, \mathbb{E}\bigl[e^{\lambda (S - \mathbb{E}[S])}\bigr] = e^{-\lambda t} \prod_{i=1}^n \mathbb{E}\bigl[e^{\lambda (X_i - \mathbb{E}[X_i])}\bigr] \\ &\le \exp\Bigl(-\lambda t + \frac{\lambda^2}{8} \sum_{i} (b_i - a_i)^2\Bigr) && \text{lemma, with } Y = X_i - \mathbb{E}[X_i] \end{aligned}

The exponent is smallest at λ=4t/∑i(bi−ai)2\lambda = 4t / \sum_i (b_i - a_i)^2, which gives the theorem. The lower tail follows by applying this to −Xi-X_i. □\square

The program checks the lemma numerically for a few centred distributions on [a,b][a, b] and a range of λ\lambda away from 0 (at λ=0\lambda = 0 both sides equal 1): the ratio of the true MGF to the bound stays below 1.

python · try it yourself
from math import exp

dists = {
    "fair +-1":            [(-1, 0.5), (1, 0.5)],
    "Bernoulli(0.1)-0.1":  [(-0.1, 0.9), (0.9, 0.1)],
    "three-point":         [(-1, 0.25), (0, 0.25), (0.5, 0.5)],
}
for name, d in dists.items():
    assert abs(sum(v * p for v, p in d)) < 1e-12           # centred
    a, b = min(v for v, _ in d), max(v for v, _ in d)
    worst = max(sum(p * exp(lam * v) for v, p in d) / exp(lam ** 2 * (b - a) ** 2 / 8)
                for lam in [i / 10 for i in range(-50, 51) if abs(i) >= 5])
    print(f"{name:20} range [{a:.2f}, {b:.2f}]: max of MGF / bound for 0.5 <= |lambda| <= 5: {worst:.4f}")
fair +-1             range [-1.00, 1.00]: max of MGF / bound for 0.5 <= |lambda| <= 5: 0.9951
Bernoulli(0.1)-0.1   range [-0.10, 0.90]: max of MGF / bound for 0.5 <= |lambda| <= 5: 0.9818
three-point          range [-1.00, 0.50]: max of MGF / bound for 0.5 <= |lambda| <= 5: 0.9802

The fair ±1\pm 1 variable comes closest: it is the extreme case of the lemma, with E[eλY]=cosh⁡λ≤eλ2/2\mathbb{E}[e^{\lambda Y}] = \cosh \lambda \le e^{\lambda^2/2}, and the ratio tends to 1 as λ→0\lambda \to 0.

Majority votes, settled

In Amplification by Repetition, kk independent runs each answer correctly with probability 12+γ\frac12 + \gamma, and the majority is taken. Let Xi∈{0,1}X_i \in \{0, 1\} indicate that run ii is correct, so E[S]=(12+γ)k\mathbb{E}[S] = (\frac12 + \gamma)k. The majority is wrong only if S≤k/2S \le k/2, a deviation of t=γkt = \gamma k below the mean, and the ranges are bi−ai=1b_i - a_i = 1:

Pr⁡[majority wrong]≤Pr⁡[S−E[S]≤−γk]≤e−2γ2k2/k=e−2γ2k.\Pr[\text{majority wrong}] \le \Pr\bigl[S - \mathbb{E}[S] \le -\gamma k\bigr] \le e^{-2\gamma^2 k^2 / k} = e^{-2\gamma^2 k}.

Hoeffding or Chernoff?

For 0/1 variables both bounds apply, and they are good at different things. Hoeffding's exponent is 2t2/n2t^2/n, which ignores the mean. Chernoff's exponent for a deviation t=δμt = \delta\mu is about t2/(3μ)t^2/(3\mu), which is much larger when μ\mu is small compared with nn, that is, for rare events. For fair coins the two are comparable, and Hoeffding's constant is better.

python · try it yourself
from math import exp, lgamma, log

def exact_upper(n, p, k):
    # binomial terms in log space, so that large n does not overflow
    return sum(exp(lgamma(n + 1) - lgamma(j + 1) - lgamma(n - j + 1) + j * log(p) + (n - j) * log(1 - p))
               for j in range(k, n + 1))

print("   n      p    mu    threshold     exact     Hoeffding   Chernoff (2+delta)")
for n, p, k in [(1000, 0.5, 550), (1000, 0.5, 600), (10000, 0.01, 130), (10000, 0.01, 200)]:
    mu = n * p
    t = k - mu
    d = t / mu
    hoeff = exp(-2 * t * t / n)
    chern = exp(-d * d * mu / (2 + d))
    print(f"{n:5}  {p:5}  {mu:4.0f}  {k:11}  {exact_upper(n, p, k):9.2e}  {hoeff:10.2e}  {chern:12.2e}")
   n      p    mu    threshold     exact     Hoeffding   Chernoff (2+delta)
 1000    0.5   500          550   8.65e-04    6.74e-03      9.25e-02
 1000    0.5   500          600   1.36e-10    2.06e-09      1.13e-04
10000   0.01   100          130   2.18e-03    8.35e-01      2.00e-02
10000   0.01   100          200   5.63e-19    1.35e-01      3.34e-15

With p=0.01p = 0.01 and n=10 000n = 10\,000, reaching 200 successes instead of the expected 100 is astronomically unlikely, which Chernoff sees and Hoeffding does not. For balls into bins, where each bin's load is a sum of many indicators with tiny means, Chernoff is therefore the right choice (The Union Bound at Work).

Estimating an average by sampling

To estimate the mean of a function ff with values in [0,1][0, 1] over a large domain, average ff at nn independent uniform samples. Hoeffding says n≥ln⁡(2/η)2ε2n \ge \frac{\ln(2/\eta)}{2\varepsilon^2} samples make the estimate ε\varepsilon-accurate with probability at least 1−η1 - \eta, independently of the domain size and of ff. The program estimates the fraction of pairs (x,y)(x, y) in {1,…,300}2\{1, \ldots, 300\}^2 that are coprime (close to 6/π26/\pi^2) and records how often the estimate misses by more than ε\varepsilon.

python · try it yourself
import random
from math import ceil, gcd, log

N = 300
true = sum(gcd(x, y) == 1 for x in range(1, N + 1) for y in range(1, N + 1)) / N ** 2
eps, eta = 0.05, 0.05
n = ceil(log(2 / eta) / (2 * eps ** 2))
rng = random.Random(6)
trials, misses = 150, 0
for _ in range(trials):
    hits = sum(gcd(rng.randint(1, N), rng.randint(1, N)) == 1 for _ in range(n))
    misses += abs(hits / n - true) > eps
print(f"true fraction {true:.5f}; Hoeffding sample size for eps = {eps}, eta = {eta}: n = {n}")
print(f"{trials} estimates: {misses} missed by more than eps (allowed about {eta * trials:.1f})")
true fraction 0.60883; Hoeffding sample size for eps = 0.05, eta = 0.05: n = 738
150 estimates: 1 missed by more than eps (allowed about 7.5)

Beyond independence

Hoeffding's proof extends almost word for word to martingales with bounded increments: if Z0,Z1,…,ZnZ_0, Z_1, \ldots, Z_n is a martingale with ∣Zi−Zi−1∣≤ci\lvert Z_i - Z_{i-1} \rvert \le c_i, then Pr⁡[Zn−Z0≥t]≤e−t2/(2∑ici2)\Pr[Z_n - Z_0 \ge t] \le e^{-t^2 / (2\sum_i c_i^2)} (the Azuma-Hoeffding inequality, Azuma 1967). A consequence, McDiarmid's bounded-differences inequality (1989), says that any function f(X1,…,Xn)f(X_1, \ldots, X_n) of independent variables that changes by at most cic_i when XiX_i alone changes satisfies Pr⁡[f−E[f]≥t]≤e−2t2/∑ici2\Pr[f - \mathbb{E}[f] \ge t] \le e^{-2t^2/\sum_i c_i^2}. It proves, for example, that the number of empty bins or the chromatic number of a random graph is concentrated, even though these are not sums of independent variables. Both are stated here without proof; see the martingale chapter of Mitzenmacher and Upfal.

Test yourself

Hoeffding's inequality for S=∑XiS = \sum X_i with Xi∈[ai,bi]X_i \in [a_i, b_i] depends on which quantities?

  1. Only the means of the XiX_i
  2. Only the deviation tt and the ranges bi−aib_i - a_i
  3. The variances of the XiX_i
  4. The number of distinct values

Hoeffding's lemma bounds E[eλY]\mathbb{E}[e^{\lambda Y}] for a centred Y∈[a,b]Y \in [a, b] by what?

  1. eλ2(b−a)2/8e^{\lambda^2 (b-a)^2/8}
  2. eλ(b−a)e^{\lambda (b - a)}
  3. 1+λ21 + \lambda^2
  4. eλ2Var⁡[Y]e^{\lambda^2 \operatorname{Var}[Y]}

How many samples of a [0,1][0,1]-valued function does Hoeffding require to estimate its mean within ε=0.01\varepsilon = 0.01 with failure probability 0.050.05?

  1. About 185
  2. About 18 445
  3. About 200 000
  4. It depends on the domain size

For a sum of 10 000 Bernoulli(0.01) variables, why is Chernoff much stronger than Hoeffding for Pr⁡[S≥200]\Pr[S \ge 200]?

  1. Hoeffding needs identical distributions
  2. Chernoff's exponent scales with t2/μt^2/\mu while Hoeffding's scales with t2/nt^2/n, and here μ=100\mu = 100 is tiny compared with nn
  3. Hoeffding applies only to the lower tail
  4. They give the same bound