TExcelTAD
Home › Discrete Mathematics › Expectation

Expectation

The expected value of a random variable is its long-run average: what you get on average over many repetitions. Expectation is the main tool for analysing randomized algorithms ("quicksort makes O(nlog⁡n)O(n \log n) comparisons on average") and for comparing bets, strategies and caching policies. Its most useful property, linearity, works even when the variables are dependent.

Definition

Ex⁡[R]=∑xx⋅Pr⁡[R=x]=∑ω∈SR(ω)Pr⁡[ω]\operatorname{Ex}[R] = \sum_{x} x \cdot \Pr[R = x] = \sum_{\omega \in S} R(\omega) \Pr[\omega]

A fair die: Ex⁡[R]=1+2+⋯+66=3.5\operatorname{Ex}[R] = \frac{1 + 2 + \cdots + 6}{6} = 3.5. The expected value need not be a possible value.

For a variable with values in N\mathbb{N} there is a useful alternative: Ex⁡[R]=∑k≥0Pr⁡[R>k]\operatorname{Ex}[R] = \sum_{k \ge 0} \Pr[R > k].

Mean time to failure. If each step fails independently with probability pp, the number of steps until the first failure is geometric, and

Ex⁡[R]=∑k≥0(1−p)k=1p\operatorname{Ex}[R] = \sum_{k \ge 0} (1 - p)^k = \frac{1}{p}

A server that crashes with probability 11000\frac{1}{1000} per hour runs 1000 hours on average.

Linearity of expectation

For any random variables R1,…,RnR_1, \ldots, R_n and constants aia_i (no independence needed):

Ex⁡[∑iaiRi]=∑iaiEx⁡[Ri]\operatorname{Ex}\left[\sum_i a_i R_i\right] = \sum_i a_i \operatorname{Ex}[R_i]

The trick is to write a complicated count as a sum of indicator variables IAI_A (1 if event AA happens, else 0), whose expectation is Ex⁡[IA]=Pr⁡[A]\operatorname{Ex}[I_A] = \Pr[A].

Hat-check problem. nn people get their hats back in a random order. Expected number who get their own hat? Let IjI_j indicate that person jj does. Pr⁡[Ij=1]=1n\Pr[I_j = 1] = \frac{1}{n}, so the expectation is n⋅1n=1n \cdot \frac1n = 1, for every nn, although the IjI_j are dependent.

python · try it yourself
import random

rng = random.Random(3)
for n in [3, 10, 100]:
    trials, total = 20000, 0
    for _ in range(trials):
        p = list(range(n))
        rng.shuffle(p)
        total += sum(p[i] == i for i in range(n))
    print(f"n={n:3d}: average fixed points = {total / trials:.3f}")
n=  3: average fixed points = 0.998
n= 10: average fixed points = 1.011
n=100: average fixed points = 1.001

Coupon collector. To collect all nn coupon types, the wait for the (k+1)(k+1)-th new type is geometric with success probability n−kn\frac{n - k}{n}, expectation nn−k\frac{n}{n - k}. By linearity the total is n(1n+⋯+11)=nHnn(\frac1n + \cdots + \frac11) = n H_n (harmonic numbers).

Quicksort's expected comparisons

Randomized quicksort compares elements zi<zjz_i < z_j (in sorted order) at most once, and does so exactly when the first pivot chosen from {zi,…,zj}\{z_i, \ldots, z_j\} is ziz_i or zjz_j: probability 2j−i+1\frac{2}{j - i + 1}. By linearity,

Ex⁡[comparisons]=∑i<j2j−i+1≤2nHn≈1.39 nlog⁡2n\operatorname{Ex}[\text{comparisons}] = \sum_{i < j} \frac{2}{j - i + 1} \le 2n H_n \approx 1.39\, n \log_2 n
python · try it yourself
import random, math

def quicksort_comparisons(a, rng):
    if len(a) <= 1:
        return 0
    pivot = a[rng.randrange(len(a))]
    less = [x for x in a if x < pivot]
    more = [x for x in a if x > pivot]
    return len(a) - 1 + quicksort_comparisons(less, rng) + quicksort_comparisons(more, rng)

rng = random.Random(8)
n = 1000
avg = sum(quicksort_comparisons(list(range(n)), rng) for _ in range(30)) / 30
exact = sum(2 / (d + 1) * (n - d) for d in range(1, n))     # pairs at distance d
print(f"simulated: {avg:.0f}   exact expectation: {exact:.0f}   2n ln n: {2 * n * math.log(n):.0f}")
simulated: 11057   exact expectation: 10986   2n ln n: 13816

Expectation of products

Ex⁡[R1R2]=Ex⁡[R1]Ex⁡[R2]\operatorname{Ex}[R_1 R_2] = \operatorname{Ex}[R_1] \operatorname{Ex}[R_2] holds when R1,R2R_1, R_2 are independent, but not in general: for a fair coin indicator II, Ex⁡[I⋅I]=12≠14\operatorname{Ex}[I \cdot I] = \frac12 \ne \frac14. Also Ex⁡[1/R]≠1/Ex⁡[R]\operatorname{Ex}[1/R] \ne 1/\operatorname{Ex}[R] in general.

In software

Expected running time is the standard measure for randomized algorithms (quicksort, hash tables, skip lists, randomized load balancing). Linearity of expectation also gives quick estimates in practice: the expected number of duplicate IDs among nn random 32-bit IDs is (n2)2−32\binom{n}{2} 2^{-32}, which already exceeds 1 at n≈93000n \approx 93000.

Exercises

Exercise 1: A game

You roll a die and win the square of the number shown, in euros. What is the expected win?

Answer

916≈15.17\frac{91}{6} \approx 15.17 euros.

Solution

Ex⁡[R2]=1+4+9+16+25+366=916\operatorname{Ex}[R^2] = \frac{1 + 4 + 9 + 16 + 25 + 36}{6} = \frac{91}{6}. Note this is not Ex⁡[R]2=12.25\operatorname{Ex}[R]^2 = 12.25.

Exercise 2: Matching birthdays (linearity)

In a group of 30 people, what is the expected number of pairs sharing a birthday (365 equally likely days)?

Answer

(302)/365≈1.19\binom{30}{2} / 365 \approx 1.19.

Solution

One indicator per pair, each with probability 1365\frac{1}{365}; there are (302)=435\binom{30}{2} = 435 pairs, so the expectation is 435365≈1.19\frac{435}{365} \approx 1.19.

Exercise 3: Expected rolls until a six (code)

What is the expected number of die rolls until the first 6? Verify by simulation.

Answer
Solution

Geometric with p=16p = \frac16, so 1p=6\frac{1}{p} = 6.

python · try it yourself
import random

rng = random.Random(4)
def rolls():
    k = 1
    while rng.randint(1, 6) != 6:
        k += 1
    return k
print(round(sum(rolls() for _ in range(100000)) / 100000, 2))
6.0

Test yourself

Linearity of expectation requires the variables to be…

  1. independent
  2. identically distributed
  3. nothing: it always holds
  4. positive

Each attempt succeeds with probability 1/41/4. Expected attempts until the first success?

  1. 2
  2. 3
  3. 4
  4. 8

A random permutation of nn items has how many fixed points on average?

  1. 0
  2. 1
  3. n/2n/2
  4. ln⁡n\ln n