Random variables and expected value

A random variable turns each outcome into a number: the face of a die, the money a game pays, the number of heads.

Expected value

The expected value E[X] is the average you would get over very many repeats:

E[X] = sum of (value × its probability)

For a fair die: (1 + 2 + 3 + 4 + 5 + 6) × 1/6 = 3.5. You can never roll 3.5; it is a long-run average.

Variance and standard deviation

Variance is the expected squared distance from the mean: Var(X) = E[(X − E[X])²]. Its square root, the standard deviation, is in the same units as X and says how far values typically land from the mean.

faces = [1, 2, 3, 4, 5, 6]
mean = sum(faces) / 6
variance = sum((x - mean) ** 2 for x in faces) / 6
print("E[die] =", mean)
print("Var =", round(variance, 3), " SD =", round(variance ** 0.5, 3))

payout = 10 * 0.1 + 0 * 0.9
print("ticket costs 2, expected payout", payout, "so expected gain", payout - 2)
Open in compiler

Is the game worth it?

A ticket costs 2 and pays 10 with probability 0.1, else nothing. E[payout] = 10 × 0.1 = 1, so on average you lose 1 per ticket. Every casino game has a negative expected value for the player.

Quick check

1. A game pays 6 with probability 1/3, else 0. E[payout]?
2. Variance is measured in…
3. Expected value of the sum of two fair dice?

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.