---
title: "Tail bounds: Markov, Chebyshev and Chernoff"
heading: Guarantees about rare events when you don't know the whole distribution
eyebrow: A11 · Lesson 30 · Tail bounds
xp: 40
---

::: hook
A buffer holds 10 packets and the average queue is 2: how often can it overflow? A test measured 20 000 bits: how sure can you be that the true error rate is within 1% of what you saw? Often you know a mean, maybe a variance or a generating function, but not the full distribution, and you need a guarantee, not a guess. **Tail bounds** give one. The more you know about a variable, the tighter the bound: the mean alone gives Markov's, the variance adds Chebyshev's, and the generating function (lesson 26) gives Chernoff's, which shrinks exponentially. Lesson 29's law of large numbers rests on them.
:::

::: play
- For a standard Gaussian, the curves are the true chance of landing at least $k$ standard deviations above the mean, $Q(k)$, and the bounds on it: Chebyshev's $1/k^2$ (for both tails together) and Chernoff's $e^{-k^2/2}$.
- On the log scale the true tail and Chernoff's bound both curve down like parabolas; Chebyshev's only falls as a straight line, which is why it's so loose far out.
:::

```sim plot
view: graph
x: k = 0.5..6
y: probability = 1e-10..1 log
plot: Q(k) | label: true tail, Q(k)
plot: 1/k^2 | dashed | label: Chebyshev, 1/k²
plot: exp(-k^2/2) | dashed | color: ch3 | label: Chernoff, e^(-k²/2)
readout: Chernoff over the truth at four standard deviations = exp(-8)/Q(4) | digits: 3
```

::: predict
## A queue holds 2 packets on average. Knowing only that, what can you say about the chance it holds 10 or more?

- [x] It's at most 20%
- [ ] It's exactly $e^{-5}$
- [ ] Nothing at all
- [ ] It's at most 2%

::: reveal
At most 20%. A count is never negative, so if it were 10 or more more than a fifth of the time, its mean would already exceed $0.2 \times 10 = 2$. That's Markov's inequality, $P(X \ge a) \le E[X]/a$. It's weak but it needs nothing beyond the mean, and knowing more tightens it.
:::
:::

::: nameit
## Markov's inequality

For a variable that's never negative,

$$P(X \ge a) \le \frac{E[X]}{a}$$

Proof: $E[X] \ge E[X\cdot 1_{X \ge a}] \ge a\,P(X \ge a)$.

## Chebyshev's inequality

Apply Markov to $(X - \mu)^2$, with $a = k^2\sigma^2$:

$$P(|X - \mu| \ge k\sigma) \le \frac{1}{k^2}$$

for any distribution with a variance. It's why "within a few standard deviations" means something even for unknown distributions, and with $\sigma^2/n$ for an average of $n$ samples it proves the law of large numbers.

## Chernoff's bound

Apply Markov to $e^{sX}$ for any $s > 0$, then pick the best $s$:

$$P(X \ge a) \le \min_{s > 0} e^{-sa}M_X(s)$$

For a Gaussian with variance $\sigma^2$ and mean 0, the minimum is at $s = a/\sigma^2$ and gives $e^{-a^2/2\sigma^2}$: exponentially small, like the true tail.

## Hoeffding's bound for averages

For the average $\bar X$ of $n$ independent variables each between 0 and 1, such as error indicators,

$$P(|\bar X - p| \ge \varepsilon) \le 2e^{-2n\varepsilon^2}$$

whatever their distribution. Setting the right side to $\delta$ gives a sample size that guarantees accuracy $\varepsilon$ with confidence $1 - \delta$:

$$n \ge \frac{\ln(2/\delta)}{2\varepsilon^2}$$
:::

::: example
## A buffer's queue averages 2 packets with a standard deviation of 1.5. Bound the chance it reaches 10.

::: solution
1. **Markov, from the mean alone.**
   $$\begin{aligned} P(Q \ge 10) &\le 2/10 \\ &= 0.2 \end{aligned}$$
2. **Chebyshev, using the variance.** 10 is $8/1.5 = 5.33$ standard deviations above the mean, so
   $$\begin{aligned} P(Q \ge 10) &\le P(|Q - 2| \ge 8) \\ &\le 1/5.33^2 \\ &= 0.035 \end{aligned}$$
3. **Compare.** The second bound is six times tighter because it uses more information. If the queue were known to be geometric, the exact answer would be smaller still, but neither bound needed that.

**Answer:** Markov bounds the chance by 0.2, and Chebyshev by 0.035.
:::
:::

::: example
## Compare the bounds with the truth for a Gaussian at 3 and 4 standard deviations from the mean.

::: solution
1. **At 3σ, Chebyshev.** It bounds both tails together by
   $$1/9 = 0.111$$
2. **At 3σ, the truth.**
   $$2Q(3) = 0.0027$$
   forty times smaller.
3. **At 4σ, Chernoff.** It bounds one tail by
   $$e^{-8} = 3.4 \times 10^{-4}$$
   against $Q(4) = 3.2 \times 10^{-5}$: ten times too big, but with the right exponential shape.
4. **Which to use.** For bit-error estimates that need the shape, Chernoff's bound is the useful one; Chebyshev's guarantee is for when you know nothing but the variance.

**Answer:** at 3σ Chebyshev gives 0.111 against a true 0.0027; at 4σ Chernoff gives $3.4 \times 10^{-4}$ against a true $3.2 \times 10^{-5}$.
:::
:::

::: example
## A link's bit error rate is estimated by counting errors in $n$ bits. How many bits guarantee the estimate is within 0.01 of the truth with 99% confidence, whatever the true rate?

::: solution
1. **Hoeffding's bound.** With $\varepsilon = 0.01$ and $\delta = 0.01$:
   $$\begin{aligned} n &\ge \frac{\ln(200)}{2 \times 10^{-4}} \\ &= 26\,492\ \text{bits} \end{aligned}$$
2. **What it guarantees.** That's a distribution-free guarantee; for a rate as small as $10^{-3}$, where the variance is tiny, a sharper bound needs fewer bits, but the absolute accuracy 0.01 would then be useless anyway.
3. **Small rates.** For small rates you specify relative accuracy, and need roughly $100/p$ bits or more.

**Answer:** 26 492 bits guarantee it, whatever the true rate.
:::
:::

::: check
given: m = 1 | 2 | 5; a = 10 | 20 | 50
answer: m/a
tolerance: 0.5%

## A nonnegative quantity has mean {m}. What's the most the chance it reaches {a} can be, knowing nothing else?

::: right
Right. Markov's inequality gives $P(X \ge a) \le E[X]/a$ = {answer}.
:::

::: misses
- [a/m] That's upside down, and it can exceed 1.
- [exp(-a/m)] That's the exact answer for an exponential, which you don't know this is.
- [any] Not yet. Use Markov's inequality: the mean divided by the threshold.
:::
:::

::: check
given: k = 1.5 | 2 | 3 | 5
answer: 1/k^2
tolerance: 0.5%

## Knowing only the mean and standard deviation, what is the largest possible chance of landing {k} or more standard deviations from the mean, in either direction?

::: right
Right. Chebyshev's inequality: at most $1/k^2$ = {answer}.
:::

::: misses
- [1/k] That's Markov applied to $|X - \mu|$ directly. Squaring first gives the tighter $1/k^2$.
- [erfc(k/sqrt(2))] That's the exact answer for a Gaussian. Chebyshev holds for every distribution, so it's larger.
- [any] Not yet. Use Chebyshev's $1/k^2$.
:::
:::

::: check
given: k = 2 | 3 | 4 | 5
answer: exp(-k^2/2)
tolerance: 0.5%

## What does Chernoff's bound give for the chance a Gaussian lands at least {k} standard deviations above its mean?

::: right
Right. With $M(s) = e^{s^2/2}$ in standard units, minimizing $e^{-sk + s^2/2}$ at $s = k$ gives $e^{-k^2/2}$ = {answer}.
:::

::: misses
- [1/k^2] That's Chebyshev's, which uses only the variance.
- [exp(-k^2)] Check the exponent: minimizing $-sk + s^2/2$ gives $-k^2/2$.
- [any] Not yet. Minimize $e^{-sk}M(s)$ over $s$, with $M(s) = e^{s^2/2}$.
:::
:::

::: check
kind: parts
given: e = 0.005 | 0.01 | 0.05; d = 0.01 | 0.05

## You estimate a probability by the fraction of successes in $n$ independent trials.

::: part
answer: ln(2/d)/(2*e^2)
tolerance: 0.5%

### By Hoeffding's bound, how many trials guarantee an error under {e} with probability at least $1 - \given{d}$?

::: misses
- [ln(1/d)/(2*e^2)] Both tails count, so it's $\ln(2/\delta)$.
- [ln(2/d)/(2*e)] The bound shrinks as $e^{-2n\varepsilon^2}$: $\varepsilon$ is squared.
- [any] Not yet. Solve $2e^{-2n\varepsilon^2} = \delta$ for $n$.
:::
:::

::: part
answer: 4*ln(2/d)/(2*e^2)
tolerance: 0.5%

### How many for half that error?

::: misses
- [2*ln(2/d)/(2*e^2)] Halving $\varepsilon$ quadruples $n$, since it enters squared.
- [any] Not yet. Halving $\varepsilon$ multiplies $n$ by 4.
:::
:::

::: right
Right. Accuracy costs the square: each extra digit needs a hundred times the trials.
:::
:::

::: check
kind: python
given: n = 100 | 1000; p = 0.1 | 0.3; e = 0.03 | 0.05

## For the average of `n` = {n} Bernoulli trials with $p$ = {p}, compute the exact chance $P(|\bar X - p| \ge \varepsilon)$ for `e` = {e} from the binomial distribution (sum the probabilities of the counts $k$ with $|k/n - p| \ge \varepsilon$, using `math.comb` or logs to avoid overflow). Set `exact`, `cheb` to Chebyshev's bound $p(1 - p)/(n\varepsilon^2)$ (capped at 1), and `hoeff` to Hoeffding's $2e^{-2n\varepsilon^2}$ (capped at 1).

```python starter
import math
exact = cheb = hoeff = 1
print(exact, cheb, hoeff)
```

```python solution
import math
def logpmf(k):
    return math.lgamma(n + 1) - math.lgamma(k + 1) - math.lgamma(n - k + 1) + k * math.log(p) + (n - k) * math.log(1 - p)
exact = sum(math.exp(logpmf(k)) for k in range(n + 1) if abs(k / n - p) >= e - 1e-12)
cheb = min(1, p * (1 - p) / (n * e**2))
hoeff = min(1, 2 * math.exp(-2 * n * e**2))
print(exact, cheb, hoeff)
```

```python tests
import math
lp = lambda k: math.lgamma(n + 1) - math.lgamma(k + 1) - math.lgamma(n - k + 1) + k * math.log(p) + (n - k) * math.log(1 - p)
ex = sum(math.exp(lp(k)) for k in range(n + 1) if abs(k / n - p) >= e - 1e-12)
assert abs(exact - ex) < 1e-9, f"The exact tail is {ex:.6g}: sum the binomial probabilities of counts at least ε away from np."
assert abs(cheb - min(1, p * (1 - p) / (n * e**2))) < 1e-12 and abs(hoeff - min(1, 2 * math.exp(-2 * n * e**2))) < 1e-12, "Check the two bounds' formulas."
assert exact <= cheb + 1e-12 and exact <= hoeff + 1e-12, "Both bounds must lie above the exact value."
```

::: right
Right. Both bounds hold, and the exact value sits below them. For large $n$ Hoeffding's exponential wins easily; for small $n$ or a $p$ near 0 or 1, Chebyshev's variance-aware bound can be the tighter one.
:::
:::

::: check
kind: derivation
vars: a = 0.5..4, s = 0.1..4, sg = 0.5..2

## Derive Chernoff's bound for a Gaussian with mean 0 and standard deviation $\sigma$ (written `sg`), whose $M(s) = e^{\sigma^2s^2/2}$.

::: step
answer: -s*a + sg^2*s^2/2

### What is $\ln\left(e^{-sa}M(s)\right)$?
:::

::: step
answer: a/sg^2

### Which $s$ minimizes it?
:::

::: step
answer: exp(-a^2/(2*sg^2))

### What is the bound there?
:::

::: right
Right. The bound $e^{-a^2/2\sigma^2}$ has the Gaussian tail's own exponent; the true tail is smaller only by a factor that grows like $a/\sigma$.
:::
:::

::: check
kind: choice

## You know a random variable's mean and variance but nothing about the shape of its distribution. Which tail bound can you use?

- [x] Chebyshev's
- [ ] Chernoff's
- [ ] The Gaussian $Q$ function
- [ ] None

::: right
Right. Chebyshev's needs only the variance. Chernoff's needs the generating function, which depends on the whole distribution, and $Q$ assumes the distribution is Gaussian.
:::

::: misses
- [2] Chernoff's bound needs $M(s)$, which you can't get from two moments.
- [3] Using $Q$ assumes a Gaussian shape, which you don't know.
- [4] Chebyshev's holds for every distribution with a variance.
:::
:::

::: explain
## Why is Chebyshev's bound so much looser than the truth for a Gaussian, and why can't it be made tighter without more information?

::: model
Chebyshev's bound must hold for every distribution with the given variance, including the worst one: a distribution that puts a little probability exactly at $\pm k\sigma$ and the rest at the mean, which reaches $1/k^2$ exactly. A Gaussian spreads its probability smoothly and has tails that fall like $e^{-k^2/2}$, far below $1/k^2$, but nothing in the mean and variance alone rules out the worst case. Only extra information, like a generating function or knowing the variable is a sum of many small independent pieces, lets a bound like Chernoff's or Hoeffding's capture the exponential fall.
:::
:::

::: practice

::: check
given: m = 3 | 5; s = 1 | 2; a = 9 | 15
answer: (s/(a - m))^2
tolerance: 0.5%

## A quantity has mean {m} and standard deviation {s}. By Chebyshev, at most what's the chance it reaches {a} or more (counting both tails as the bound does)?

::: misses
- [m/a] That's Markov's bound, from the mean alone.
- [any] Use $1/k^2$ with $k = (a - \mu)/\sigma$.
:::
:::

::: check
given: e = 0.01 | 0.02; d = 0.05 | 0.001
answer: ln(2/d)/(2*e^2)
tolerance: 0.5%

## How many trials does Hoeffding's bound need for an estimate within {e} with confidence $1 - \given{d}$?

::: misses
- [ln(2/d)/e^2] The denominator is $2\varepsilon^2$.
- [any] $n = \ln(2/\delta)/(2\varepsilon^2)$.
:::
:::
:::
