8/17
Part 3 of 7 · Expected valueLearn and do

An expected value is a weighted average, and averages add

An expected value is the average a random quantity takes over many repeats: each value times its probability, added up. Its most useful property is linearity: MIT's text says the expected value of a sum is the sum of the expected values. Grinstead and Snell add that this holds whether or not the parts are independent, which they call the First Fundamental Mystery of Probability.

The trick that makes linearity powerful is the indicator: a variable that's 1 when an event happens and 0 otherwise. Its expected value is just the event's probability. To count how many things happen on average, add the indicators' probabilities.

The shop's label printer jams, and the 20 shipping labels for a batch of 20 parcels go on in a random order.

import random

rng = random.Random(1)
parcels, trials = 20, 100_000
matches = 0
for _ in range(trials):
    labels = list(range(parcels))
    # the labels land on the parcels in a random order
    rng.shuffle(labels)
    matches += sum(parcel == label for parcel, label in enumerate(labels))
print(f"{matches / trials:.3f} parcels a batch get their own label, on average")
Predict

On average, how many of the 20 parcels still get their own label?

Pick one to see what it prints.
Predict

With 1,000 parcels in a batch instead of 20, what's the average?

Pick an answer above to see the rest