Deviation from the Mean: Markov and Chebyshev
An expected value says nothing about how far a random variable typically strays from it. An algorithm that averages 1 second might take an hour once in a while. Tail bounds limit the probability of large deviations using only a little information: the mean (Markov), the variance (Chebyshev), or independence of many small parts (Chernoff). They are how "with high probability" guarantees are proved.
Markov's inequality
If and :
Proof. .
If an algorithm's expected running time is 2 seconds, the probability that it takes 20 seconds or more is at most . Restarting it after 4 seconds (failure probability at most each time) and repeating gives a running time that is very unlikely to be long: this turns "expected fast" into "fast with high probability".
Variance and Chebyshev's inequality
The variance measures spread:
and the standard deviation is . Applying Markov to gives Chebyshev's inequality: for ,
A value is at least 3 standard deviations from its mean with probability at most , for any distribution.
Variance of sums. For pairwise independent , . A binomial() variable is a sum of independent Bernoulli() indicators with variance each, so its variance is .
Polling: how many samples?
To estimate the fraction of users who like a feature, ask random users; the fraction of yes answers has mean and variance . Chebyshev:
For accuracy with failure probability at most 5%, suffices, whatever the population size. (The normal approximation of the Central Limit Theorem gives a sharper answer of about 600.)
import random
rng = random.Random(12)
p, n, eps, runs = 0.3, 3125, 0.04, 400
bad = 0
for _ in range(runs):
yes = sum(rng.random() < p for _ in range(n))
bad += abs(yes / n - p) >= eps
print(f"observed P(|p_hat - p| >= {eps}) = {bad / runs:.4f} Chebyshev bound: {1 / (4 * n * eps * eps):.4f}")observed P(|p_hat - p| >= 0.04) = 0.0000 Chebyshev bound: 0.0500
Chebyshev's bound holds but is very loose here: the real deviation probability is tiny. For sums of many independent variables, Chernoff bounds decrease exponentially: for .
Comparing the bounds
For the number of heads in 100 fair flips (, ), bound :
from math import comb, exp
n, mu, var = 100, 50, 25
exact = sum(comb(n, k) for k in range(70, n + 1)) / 2 ** n
markov = mu / 70
chebyshev = var / (70 - mu) ** 2 # bounds P(|R - 50| >= 20), so also P(R >= 70)
delta = 70 / mu - 1
chernoff = exp(-delta ** 2 * mu / 3)
print(f"exact {exact:.2e}")
print(f"Markov {markov:.2e}")
print(f"Chebyshev {chebyshev:.2e}")
print(f"Chernoff {chernoff:.2e}")exact 3.93e-05
Markov 7.14e-01
Chebyshev 6.25e-02
Chernoff 6.95e-02
More information gives sharper bounds: Markov uses only the mean, Chebyshev the variance, Chernoff full independence.
In software
Tail bounds justify engineering choices: how many hash functions a Bloom filter needs, how many replicas keep data safe, how many samples an A/B test needs, and why randomized load balancing ("power of two choices") keeps the maximum load small. Monitoring uses the same reasoning when it alerts on values several standard deviations from the mean.
Exercises
Exercise 1: Markov
The average response time is 50 ms. Bound the fraction of responses taking at least 500 ms.
Answer
At most 10%.
Solution
Response times are non-negative, so Markov gives .
Exercise 2: Variance of a die
Compute the variance of a fair die roll.
Answer
.
Solution
and , so .
Exercise 3: Chebyshev for dice (code)
For the sum of 100 dice, bound with Chebyshev and estimate it by simulation.
Answer
Bound ; the simulated value is far smaller (well under 1%).
Solution
The dice are independent, so and Chebyshev gives .
import random
rng = random.Random(6)
runs = 4000
far = sum(abs(sum(rng.randint(1, 6) for _ in range(100)) - 350) >= 50 for _ in range(runs))
print("bound:", round(100 * 35 / 12 / 2500, 4), " simulated:", far / runs)bound: 0.1167 simulated: 0.003
Test yourself
Markov's inequality requires the random variable to be…
- normal
- non-negative
- independent
- bounded above
By Chebyshev, the probability of being at least 2 standard deviations from the mean is at most…
- 5%
What is the variance of a binomial(, ) variable?