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 with distribution :
provided the sum converges absolutely. For a fair die, , 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 approaches .
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 there is no need to find the distribution of first:
For a die, , which is not . In general ; the gap between and 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 :
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 tosses you win . Then
Yet nobody would pay much to play. Simulated averages keep creeping upward, dominated by rare huge prizes, instead of settling down:
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 . 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 with values in , . For the geometric distribution this gives at once.
- Total expectation: for a partition (see Conditional Expectation).
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
.
Solution
.
Exercise 2: Tail sum
Use the tail sum formula to find the expected maximum of two dice.
Answer
.
Solution
. Summing for : .
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
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 for a fair die?
- 3
- 3.5
- 4
- 6
Is in general?
- Yes
- No; the difference is the variance
- Only for continuous variables
- Only for negative values