TExcelTAD
Home › Probability Theory › Expected Value

Expected Value

The expected value of a random variable is its long-run average: the probability-weighted mean of its values. It is the single most useful summary of a random quantity, and it decides whether a bet, an algorithm or a business plan is worth it on average.

Definition

For a discrete random variable XX with distribution mm:

E[X]=∑xx Pr⁡[X=x]E[X] = \sum_{x} x \, \Pr[X = x]

provided the sum converges absolutely. For a fair die, E[X]=1+2+⋯+66=3.5E[X] = \frac{1 + 2 + \cdots + 6}{6} = 3.5, a value the die never shows: the expected value is an average, not a typical outcome.

The law of large numbers justifies the "long-run average" reading: the average of many independent copies of XX approaches E[X]E[X].

python · try it yourself
from fractions import Fraction
import random

dist = {x: Fraction(1, 6) for x in range(1, 7)}
print("E[die] =", sum(x * p for x, p in dist.items()))

rng = random.Random(1)
for n in [10, 1000, 100_000]:
    print(n, "rolls, average:", round(sum(rng.randint(1, 6) for _ in range(n)) / n, 4))
E[die] = 7/2
10 rolls, average: 3.4
1000 rolls, average: 3.528
100000 rolls, average: 3.4988

Functions of a random variable

To compute E[g(X)]E[g(X)] there is no need to find the distribution of g(X)g(X) first:

E[g(X)]=∑xg(x)Pr⁡[X=x]E[g(X)] = \sum_x g(x) \Pr[X = x]

For a die, E[X2]=1+4+9+16+25+366=916≈15.17E[X^2] = \frac{1 + 4 + 9 + 16 + 25 + 36}{6} = \frac{91}{6} \approx 15.17, which is not 3.52=12.253.5^2 = 12.25. In general E[g(X)]≠g(E[X])E[g(X)] \ne g(E[X]); the gap between E[X2]E[X^2] and E[X]2E[X]^2 is the variance.

Fair games and expected gain

A game is fair if the expected gain is 0. In roulette, a 1-unit bet on a single number pays 35 units with probability 1/381/38:

E[gain]=35⋅138−1⋅3738=−238≈−0.0526E[\text{gain}] = 35 \cdot \frac{1}{38} - 1 \cdot \frac{37}{38} = -\frac{2}{38} \approx -0.0526

Every roulette bet has the same expected loss of about 5.3% per unit. Changing the bet changes the variance, not the expectation.

When expectations are infinite

In the St. Petersburg game, a coin is tossed until heads; if that takes kk tosses you win 2k2^k. Then

E[prize]=∑k=1∞2k⋅12k=1+1+1+⋯=∞E[\text{prize}] = \sum_{k=1}^{\infty} 2^k \cdot \frac{1}{2^k} = 1 + 1 + 1 + \cdots = \infty

Yet nobody would pay much to play. Simulated averages keep creeping upward, dominated by rare huge prizes, instead of settling down:

python · try it yourself
import random

rng = random.Random(6)
def prize():
    k = 1
    while rng.random() < 0.5:
        k += 1
    return 2 ** k

total, n = 0, 0
for checkpoint in [100, 10_000, 1_000_000]:
    while n < checkpoint:
        total += prize()
        n += 1
    print(f"after {n:8d} games: average prize {total / n:.2f}")
after      100 games: average prize 7.76
after    10000 games: average prize 14.73
after  1000000 games: average prize 19.63

The average grows roughly like log⁡2n\log_2 n. Real casinos cap prizes, which makes the expectation finite and small.

Conditional expectation and the tail sum

Two useful tools come up again and again:

  • Tail sum formula: for XX with values in {0,1,2,…}\{0, 1, 2, \ldots\}, E[X]=∑k≥1Pr⁡[X≥k]E[X] = \sum_{k \ge 1} \Pr[X \ge k]. For the geometric distribution this gives ∑k≥1qk−1=1/p\sum_{k \ge 1} q^{k-1} = 1/p at once.
  • Total expectation: E[X]=∑iE[X∣Bi]Pr⁡[Bi]E[X] = \sum_i E[X \mid B_i] \Pr[B_i] for a partition B1,B2,…B_1, B_2, \ldots (see Conditional Expectation).
python · try it yourself
from fractions import Fraction

# expected number of rolls to see a six, by first-step analysis:
# E = 1 + (5/6) E  =>  E = 6
E = Fraction(1) / (1 - Fraction(5, 6))
print("rolls until a six:", E)

# expected number of tosses until HH (two heads in a row), by states:
# a = expected from scratch, b = expected after one H
#   a = 1 + a/2 + b/2,   b = 1 + 0/2 + a/2
from sympy import symbols, solve
a, b = symbols("a b")
print(solve([a - (1 + a / 2 + b / 2), b - (1 + a / 2)], [a, b]))
rolls until a six: 6
{a: 6, b: 4}

Waiting for HH takes 6 tosses on average, but waiting for HT takes only 4: patterns that overlap with themselves take longer to appear.

In software

Expected running time is how randomized algorithms are analysed (Randomized Algorithms); expected cost decides cache sizes and retry policies; and expected reward is the objective of reinforcement learning (Markov Decision Processes). Training a neural network minimises an expected loss, estimated by averaging over a mini-batch.

Exercises

Exercise 1: A dice game

You win the face value if a die shows 4, 5 or 6 and pay 3 otherwise. What is your expected gain?

Answer

11.

Solution

4+5+66−3⋅36=2.5−1.5=1\frac{4 + 5 + 6}{6} - 3 \cdot \frac{3}{6} = 2.5 - 1.5 = 1.

Exercise 2: Tail sum

Use the tail sum formula to find the expected maximum of two dice.

Answer

161/36≈4.47161/36 \approx 4.47.

Solution

Pr⁡[max⁡≥k]=1−(k−1)236\Pr[\max \ge k] = 1 - \frac{(k-1)^2}{36}. Summing for k=1,…,6k = 1, \ldots, 6: 6−0+1+4+9+16+2536=6−5536=161366 - \frac{0 + 1 + 4 + 9 + 16 + 25}{36} = 6 - \frac{55}{36} = \frac{161}{36}.

Exercise 3: HT versus HH (code)

Simulate the average number of tosses until HH and until HT appear.

Answer

About 6 for HH and 4 for HT.

Solution
python · try it yourself
import random

rng = random.Random(3)
def wait(pattern):
    last, n = "", 0
    while not last.endswith(pattern):
        last = (last + rng.choice("HT"))[-2:]
        n += 1
    return n

for pattern in ["HH", "HT"]:
    print(pattern, round(sum(wait(pattern) for _ in range(100_000)) / 100_000, 3))
HH 6.013
HT 4.002

Test yourself

What is E[X]E[X] for a fair die?

  1. 3
  2. 3.5
  3. 4
  4. 6

Is E[X2]=(E[X])2E[X^2] = (E[X])^2 in general?

  1. Yes
  2. No; the difference is the variance
  3. Only for continuous variables
  4. Only for negative values