Markov's and Chebyshev's Inequalities
Often you know only a mean, or a mean and a variance, and need to bound the chance of an extreme outcome. Two simple inequalities do this for every distribution. They are rarely tight, but they always hold, and they are the key step in proving the law of large numbers.
Markov's inequality
For a non-negative random variable and any :
Proof: . If the average response time is 100 ms, at most 10% of requests take 1 second or more, whatever the distribution.
Chebyshev's inequality
Apply Markov to with :
At most of any distribution lies 2 or more standard deviations from the mean, at most beyond 3. Compare the normal distribution's 4.6% and 0.27%: Chebyshev is the worst case, the price for assuming nothing about the shape.
import numpy as np
rng = np.random.default_rng(1)
samples = {
"normal": rng.normal(0, 1, 500_000),
"exponential": rng.exponential(1, 500_000),
"uniform": rng.uniform(0, 1, 500_000),
"two-point +-1": rng.choice([-1.0, 1.0], 500_000),
}
for k in [1.5, 2, 3]:
row = []
for name, x in samples.items():
mu, sd = x.mean(), x.std()
row.append(f"{name} {np.mean(np.abs(x - mu) >= k * sd):.4f}")
print(f"k={k}: Chebyshev bound {1 / k ** 2:.4f} | " + " | ".join(row))k=1.5: Chebyshev bound 0.4444 | normal 0.1332 | exponential 0.0820 | uniform 0.1339 | two-point +-1 0.0000
k=2: Chebyshev bound 0.2500 | normal 0.0459 | exponential 0.0501 | uniform 0.0000 | two-point +-1 0.0000
k=3: Chebyshev bound 0.1111 | normal 0.0029 | exponential 0.0185 | uniform 0.0000 | two-point +-1 0.0000
All observed frequencies stay below the bound. The bound can be attained: the distribution with and has standard deviation 1 and exactly of its mass at distance .
When the bounds are useful
These inequalities shine when the distribution is unknown or complicated: the number of comparisons of a randomized algorithm, the load on a server, the error of a simulation. For example, if a randomized algorithm's running time has mean , Markov's inequality says it exceeds with probability at most ; by stopping and restarting after steps, restarts all fail with probability at most .
For sums of many independent pieces, much sharper Chernoff bounds exist, decreasing exponentially in the number of standard deviations (Deviation from the Mean).
import math
# Sum of n fair coin flips: P(S >= 0.6 n)
for n in [50, 100, 500]:
mu, var = n / 2, n / 4
a = 0.6 * n
markov = mu / a
cheb = var / (a - mu) ** 2 # one-sided use of Chebyshev (an over-estimate)
exact = sum(math.comb(n, k) for k in range(math.ceil(a), n + 1)) / 2 ** n
print(f"n={n:3d} Markov {markov:.3f} Chebyshev {cheb:.4f} exact {exact:.2e}")n= 50 Markov 0.833 Chebyshev 0.5000 exact 1.01e-01
n=100 Markov 0.833 Chebyshev 0.2500 exact 2.84e-02
n=500 Markov 0.833 Chebyshev 0.0500 exact 4.47e-06
Markov does not improve with ; Chebyshev improves like ; the truth improves exponentially.
In software
Markov-style reasoning bounds tail latency from averages, memory use from expected sizes, and the probability that a hash table bucket overflows. Chebyshev gives sample sizes for simulations and A/B tests that are valid without assuming normality. Many proofs in randomized algorithms are "compute the expectation, then apply Markov".
Exercises
Exercise 1: Markov for queue length
A queue has average length 3. Bound the probability that it holds at least 15 items.
Answer
At most .
Solution
.
Exercise 2: Chebyshev for exam scores
Scores have mean 70 and standard deviation 8. What fraction of students at least must score between 54 and 86?
Answer
At least 75%.
Solution
54 and 86 are standard deviations from the mean; at most lie outside.
Exercise 3: Sample size by Chebyshev (code)
How many fair coin tosses guarantee, by Chebyshev, that the proportion of heads is within 0.05 of with probability at least 0.95? Check how often it fails in simulation.
Answer
; the actual failure rate is far below 5%.
Solution
, so needs .
import numpy as np
rng = np.random.default_rng(7)
n = 2000
props = rng.binomial(n, 0.5, 100_000) / n
print("failure rate:", (np.abs(props - 0.5) >= 0.05).mean())failure rate: 1e-05
The normal approximation shows that about 400 tosses would already suffice (Using the CLT).
Test yourself
Markov's inequality applies to…
- any random variable
- non-negative random variables
- only normal variables
- only discrete variables
By Chebyshev, at most what fraction of a distribution lies 3 or more standard deviations from its mean?
- 0.27%
- 1/3
- 1/9
- 1/27