Sampling from a distribution
Draw an index from a probability vector, e.g. with cumulative sums or np.random.choice.
Builds on: Probability and distributions
What the animation says
One number, a fair pick
A text generator, choosing its next word, holds a list of probabilities. Ours has three options: point five, point three, point two. We want one pick, so that over many picks option zero wins half the time, option one thirty percent, option two twenty. But the computer only gives us one uniform random number, anywhere from zero up to just below one. How do we turn that one number into a fair pick?
Throw a dart at a ruler
Lay the probabilities end to end on a ruler from zero to one. Option zero gets zero to point five. Option one gets point five to point eight. Option two gets point eight to one. The random number is a dart, equally likely to land anywhere. A stretch twice as long gets hit twice as often. Whichever stretch the dart lands in is the pick. Our dart is at point six two. Which option is it?
Running totals make the stretches
Option one. The dart landed past point five, but before point eight. Those boundaries are running totals. Point five. Point five plus point three, point eight. Plus point two, one. These are cumulative sums, and the last one is always one. The rule: check the running totals from left to right, and pick the first one bigger than the dart. Compare the dart with the running totals, never with the probabilities themselves. New dart: point nine three. Tap the option it picks.
Why the pick is fair
Option two: point nine three gets past point five and point eight, and stops at one. A dart at point one stops at the very first total, so option zero. Why is this fair? Each option's stretch runs from the previous total to its own total. That length is exactly its probability, so a uniform dart hits it exactly that often. Throw a hundred thousand darts, and the counts settle near half, thirty percent and twenty percent.
In code: by hand, then NumPy
In code, cumsum builds the running totals: point five, point eight, one. This line draws the dart. The loop walks the totals and stops at the first one bigger than the dart. NumPy's random choice does the same job in one call: give it the number of options and the probabilities. The last two lines draw a hundred thousand times and count how often each option comes up. So, to sample: stack the probabilities into running totals, then pick the first total above a uniform dart.
Throw a dart at a ruler
Cumulative sums make the stretches
Three darts, by hand
By hand, then with np.random.choice
import numpy as np
p = [0.5, 0.3, 0.2]
c = np.cumsum(p) # [0.5, 0.8, 1.0]
u = np.random.rand() # uniform in [0, 1)
for i, ci in enumerate(c): # first i with u < c[i]
if u < ci:
break
print(u, i)
j = np.random.choice(len(p), p=p) # built-in version
print(j)
draws = np.random.choice(3, size=100000, p=p)
print(np.bincount(draws) / 100000) # close to [0.5, 0.3, 0.2]
Remember this
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses