Geometric and Negative Binomial
The binomial distribution counts successes in a fixed number of trials. Flip the question around: how many trials until the first success? Until the -th? These waiting times have the geometric and negative binomial distributions.
The geometric distribution
Repeat independent trials with success probability until the first success. The number of trials has
( failures, then a success). The tail is simple: (the first trials all fail). The mean is and the variance . To roll a six takes 6 rolls on average; to win a 1-in-1000 lottery, 1000 tickets on average.
Like the exponential, the geometric distribution is memoryless: . A die does not remember that it has not shown a six for 20 rolls.
import random
from collections import Counter
rng = random.Random(1)
p, n = 1 / 6, 100_000
def first_six():
t = 1
while rng.randint(1, 6) != 6:
t += 1
return t
T = [first_six() for _ in range(n)]
c = Counter(T)
for k in range(1, 6):
print(f"P(T={k}) ~ {c[k] / n:.4f} exact {(5 / 6) ** (k - 1) / 6:.4f}")
print("mean:", round(sum(T) / n, 3), " 1/p =", 6)
long_wait = [t - 10 for t in T if t > 10]
print("mean extra wait after 10 failures:", round(sum(long_wait) / len(long_wait), 3))P(T=1) ~ 0.1656 exact 0.1667
P(T=2) ~ 0.1369 exact 0.1389
P(T=3) ~ 0.1162 exact 0.1157
P(T=4) ~ 0.0971 exact 0.0965
P(T=5) ~ 0.0798 exact 0.0804
mean: 6.023 1/p = 6
mean extra wait after 10 failures: 5.992
After 10 failures, the expected extra wait is still 6.
The negative binomial distribution
The number of trials until the -th success has
The last trial must be a success, and the other successes fall anywhere among the first trials. is a sum of independent geometric waiting times, so and .
Banach's matchbox problem is a classic: a smoker carries two matchboxes with matches each and picks one at random each time. When one box is found empty, how many matches remain in the other? It is a negative binomial question.
from math import comb
import random
def T_k(n, k, p):
return comb(n - 1, k - 1) * p ** k * (1 - p) ** (n - k)
k, p = 3, 0.25
dist = {n: T_k(n, k, p) for n in range(k, 200)}
mean = sum(n * pr for n, pr in dist.items())
print("P(3rd success at trial 3, 6, 12):", [round(dist[n], 4) for n in (3, 6, 12)])
print("mean:", round(mean, 4), " k/p =", k / p)
rng = random.Random(2)
def trials_until(k, p):
t = s = 0
while s < k:
t += 1
s += rng.random() < p
return t
sim = [trials_until(k, p) for _ in range(50_000)]
print("simulated mean:", round(sum(sim) / len(sim), 3))P(3rd success at trial 3, 6, 12): [0.0156, 0.0659, 0.0645]
mean: 12.0 k/p = 12.0
simulated mean: 12.036
Coupon collecting
How many cereal boxes until you have all different toys? After collecting distinct toys, the wait for a new one is geometric with , mean . Adding these up:
For 50 toys, about 225 boxes; the last toy alone takes 50 on average.
import random
rng = random.Random(3)
n, runs = 50, 4000
total = 0
for _ in range(runs):
seen, boxes = set(), 0
while len(seen) < n:
seen.add(rng.randrange(n))
boxes += 1
total += boxes
print("simulated:", round(total / runs, 1), " n H_n =", round(n * sum(1 / i for i in range(1, n + 1)), 1))simulated: 225.9 n H_n = 225.0
In software
Retry loops are geometric: if each attempt succeeds with probability , the expected number of attempts is , and the chance of needing more than is , which sizes retry limits. Rejection sampling also takes a geometric number of tries. Coupon collecting estimates how many random probes are needed to hit every shard, test every branch, or see every item in a random sample.
Exercises
Exercise 1: Retries
A request succeeds with probability 0.8. What is the expected number of attempts, and the probability of needing more than 3?
Answer
attempts; .
Solution
; .
Exercise 2: Two sixes
What is the expected number of die rolls until the second six, and the probability that it occurs exactly on roll 4?
Answer
12 rolls; .
Solution
. .
Exercise 3: Coupon collector variance (code)
Estimate by simulation the standard deviation of the number of boxes needed for , and compare with where .
Answer
About 62 boxes.
Solution
The stage waiting times are independent geometric variables, so their variances add.
import random
import math
rng = random.Random(9)
n, runs = 50, 4000
samples = []
for _ in range(runs):
seen, boxes = set(), 0
while len(seen) < n:
seen.add(rng.randrange(n))
boxes += 1
samples.append(boxes)
m = sum(samples) / runs
sd = math.sqrt(sum((x - m) ** 2 for x in samples) / runs)
theory = math.sqrt(sum((1 - (n - i) / n) / ((n - i) / n) ** 2 for i in range(n)))
print(round(sd, 1), round(theory, 1))62.7 62.0
Test yourself
If each attempt succeeds with probability , the expected number of attempts until the first success is…
A fair coin showed tails 10 times in a row. The probability that the next toss is heads is…
- Larger than 1/2
- 1/2
- Smaller than 1/2
- 1/1024