TExcelTAD
Home › Probability Theory › Geometric and Negative Binomial

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 kk-th? These waiting times have the geometric and negative binomial distributions.

The geometric distribution

Repeat independent trials with success probability pp until the first success. The number of trials TT has

Pr⁡[T=n]=qn−1p,n=1,2,3,…(q=1−p)\Pr[T = n] = q^{n-1} p, \qquad n = 1, 2, 3, \ldots \qquad (q = 1 - p)

(n−1n - 1 failures, then a success). The tail is simple: Pr⁡[T>n]=qn\Pr[T > n] = q^n (the first nn trials all fail). The mean is E[T]=1/pE[T] = 1/p and the variance q/p2q/p^2. 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: Pr⁡[T>m+n∣T>m]=qn=Pr⁡[T>n]\Pr[T > m + n \mid T > m] = q^n = \Pr[T > n]. A die does not remember that it has not shown a six for 20 rolls.

python · try it yourself
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 TkT_k until the kk-th success has

Pr⁡[Tk=n]=(n−1k−1)pkqn−k,n=k,k+1,…\Pr[T_k = n] = \binom{n-1}{k-1} p^k q^{n-k}, \qquad n = k, k+1, \ldots

The last trial must be a success, and the other k−1k - 1 successes fall anywhere among the first n−1n - 1 trials. TkT_k is a sum of kk independent geometric waiting times, so E[Tk]=k/pE[T_k] = k/p and Var⁡(Tk)=kq/p2\operatorname{Var}(T_k) = kq/p^2.

Banach's matchbox problem is a classic: a smoker carries two matchboxes with NN 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.

python · try it yourself
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 nn different toys? After collecting ii distinct toys, the wait for a new one is geometric with p=(n−i)/np = (n - i)/n, mean n/(n−i)n/(n - i). Adding these up:

E[boxes]=nn+nn−1+⋯+n1=nHn≈nln⁡nE[\text{boxes}] = \frac{n}{n} + \frac{n}{n-1} + \cdots + \frac{n}{1} = n H_n \approx n \ln n

For 50 toys, about 225 boxes; the last toy alone takes 50 on average.

python · try it yourself
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 pp, the expected number of attempts is 1/p1/p, and the chance of needing more than mm is qmq^m, 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

1.251.25 attempts; 0.0080.008.

Solution

1/p=1.251/p = 1.25; Pr⁡[T>3]=0.23=0.008\Pr[T > 3] = 0.2^3 = 0.008.

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; 3⋅(1/6)2(5/6)2=75/1296≈0.0583 \cdot (1/6)^2 (5/6)^2 = 75/1296 \approx 0.058.

Solution

k/p=2⋅6=12k/p = 2 \cdot 6 = 12. Pr⁡[T2=4]=(31)p2q2\Pr[T_2 = 4] = \binom{3}{1} p^2 q^2.

Exercise 3: Coupon collector variance (code)

Estimate by simulation the standard deviation of the number of boxes needed for n=50n = 50, and compare with ∑iqi/pi2\sqrt{\sum_{i} q_i/p_i^2} where pi=(n−i)/np_i = (n - i)/n.

Answer

About 62 boxes.

Solution

The stage waiting times are independent geometric variables, so their variances add.

python · try it yourself
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 pp, the expected number of attempts until the first success is…

  1. pp
  2. 1/p1/p
  3. q/pq/p
  4. 1/q1/q

A fair coin showed tails 10 times in a row. The probability that the next toss is heads is…

  1. Larger than 1/2
  2. 1/2
  3. Smaller than 1/2
  4. 1/1024