TExcelTAD
Home › Discrete Mathematics › Deviation from the Mean: Markov and Chebyshev

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 R≥0R \ge 0 and c>0c > 0:

Pr⁡[R≥c]≤Ex⁡[R]c\Pr[R \ge c] \le \frac{\operatorname{Ex}[R]}{c}

Proof. Ex⁡[R]≥Ex⁡[R⋅IR≥c]≥c⋅Pr⁡[R≥c]\operatorname{Ex}[R] \ge \operatorname{Ex}[R \cdot I_{R \ge c}] \ge c \cdot \Pr[R \ge c]. ■\blacksquare

If an algorithm's expected running time is 2 seconds, the probability that it takes 20 seconds or more is at most 220=10%\frac{2}{20} = 10\%. Restarting it after 4 seconds (failure probability at most 12\frac12 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:

Var⁡[R]=Ex⁡[(R−Ex⁡[R])2]=Ex⁡[R2]−Ex⁡[R]2\operatorname{Var}[R] = \operatorname{Ex}\left[(R - \operatorname{Ex}[R])^2\right] = \operatorname{Ex}[R^2] - \operatorname{Ex}[R]^2

and the standard deviation is σR=Var⁡[R]\sigma_R = \sqrt{\operatorname{Var}[R]}. Applying Markov to (R−μ)2(R - \mu)^2 gives Chebyshev's inequality: for x>0x > 0,

Pr⁡[∣R−μ∣≥x]≤Var⁡[R]x2,Pr⁡[∣R−μ∣≥cσ]≤1c2\Pr\left[\lvert R - \mu \rvert \ge x\right] \le \frac{\operatorname{Var}[R]}{x^2}, \qquad \Pr\left[\lvert R - \mu \rvert \ge c\sigma\right] \le \frac{1}{c^2}

A value is at least 3 standard deviations from its mean with probability at most 19\frac19, for any distribution.

Variance of sums. For pairwise independent RiR_i, Var⁡[∑Ri]=∑Var⁡[Ri]\operatorname{Var}[\sum R_i] = \sum \operatorname{Var}[R_i]. A binomial(n,pn, p) variable is a sum of nn independent Bernoulli(pp) indicators with variance p(1−p)p(1 - p) each, so its variance is np(1−p)np(1-p).

Polling: how many samples?

To estimate the fraction pp of users who like a feature, ask nn random users; the fraction p^\hat p of yes answers has mean pp and variance p(1−p)n≤14n\frac{p(1-p)}{n} \le \frac{1}{4n}. Chebyshev:

Pr⁡[∣p^−p∣≥ε]≤14nε2\Pr\left[\lvert \hat p - p \rvert \ge \varepsilon\right] \le \frac{1}{4n\varepsilon^2}

For accuracy ε=0.04\varepsilon = 0.04 with failure probability at most 5%, n≥14⋅0.0016⋅0.05=3125n \ge \frac{1}{4 \cdot 0.0016 \cdot 0.05} = 3125 suffices, whatever the population size. (The normal approximation of the Central Limit Theorem gives a sharper answer of about 600.)

python · try it yourself
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: Pr⁡[R≥(1+δ)μ]≤e−δ2μ/3\Pr[R \ge (1 + \delta)\mu] \le e^{-\delta^2 \mu / 3} for 0<δ≤10 < \delta \le 1.

Comparing the bounds

For the number of heads RR in 100 fair flips (μ=50\mu = 50, σ=5\sigma = 5), bound Pr⁡[R≥70]\Pr[R \ge 70]:

python · try it yourself
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 Pr⁡[R≥500]≤50500=0.1\Pr[R \ge 500] \le \frac{50}{500} = 0.1.

Exercise 2: Variance of a die

Compute the variance of a fair die roll.

Answer

3512≈2.92\frac{35}{12} \approx 2.92.

Solution

Ex⁡[R2]=916\operatorname{Ex}[R^2] = \frac{91}{6} and Ex⁡[R]=72\operatorname{Ex}[R] = \frac72, so Var⁡[R]=916−494=182−14712=3512\operatorname{Var}[R] = \frac{91}{6} - \frac{49}{4} = \frac{182 - 147}{12} = \frac{35}{12}.

Exercise 3: Chebyshev for dice (code)

For the sum SS of 100 dice, bound Pr⁡[∣S−350∣≥50]\Pr[\lvert S - 350 \rvert \ge 50] with Chebyshev and estimate it by simulation.

Answer

Bound 100⋅35/122500≈0.117\frac{100 \cdot 35/12}{2500} \approx 0.117; the simulated value is far smaller (well under 1%).

Solution

The dice are independent, so Var⁡[S]=100⋅3512≈291.7\operatorname{Var}[S] = 100 \cdot \frac{35}{12} \approx 291.7 and Chebyshev gives 291.7502≈0.117\frac{291.7}{50^2} \approx 0.117.

python · try it yourself
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…

  1. normal
  2. non-negative
  3. independent
  4. bounded above

By Chebyshev, the probability of being at least 2 standard deviations from the mean is at most…

  1. 12\frac12
  2. 14\frac14
  3. 19\frac{1}{9}
  4. 5%

What is the variance of a binomial(nn, pp) variable?

  1. npnp
  2. np(1−p)np(1-p)
  3. n2pn^2 p
  4. p(1−p)p(1-p)