The Hypergeometric Distribution
Draw without replacement from a finite population that has "successes" among items. The number of successes in a sample of size has the hypergeometric distribution. It is the without-replacement twin of the binomial and appears in quality control, card games, lotteries, ecology and gene-set enrichment tests.
Definition
Choose which of the successes and which of the failures are in the sample; every sample of size is equally likely (Combinations). The mean is , the same as the binomial with , and the variance is
The last factor, the finite population correction, is less than 1: sampling without replacement varies less, because each draw makes the remaining population more balanced. When is much larger than the correction is close to 1 and the binomial is a fine approximation.
from math import comb
def hyper(N, K, n, k):
return comb(K, k) * comb(N - K, n - k) / comb(N, n)
def binom(n, p, k):
return comb(n, k) * p ** k * (1 - p) ** (n - k)
for N in [20, 200, 20000]:
K, n = N // 4, 8
row = [round(hyper(N, K, n, k), 4) for k in range(5)]
print(f"N={N:6d}:", row)
print("binomial:", [round(binom(8, 0.25, k), 4) for k in range(5)])N= 20: [0.0511, 0.2554, 0.3973, 0.2384, 0.0542]
N= 200: [0.0954, 0.2669, 0.3179, 0.2104, 0.0847]
N= 20000: [0.1001, 0.267, 0.3115, 0.2077, 0.0865]
binomial: [0.1001, 0.267, 0.3115, 0.2076, 0.0865]
As grows with fixed, the hypergeometric probabilities approach the binomial ones.
Acceptance sampling
A lot of 100 parts contains 5 defective ones. An inspector tests 10 at random and accepts the lot if none is defective. What is the probability the lot is accepted?
So a lot with 5% defects passes more than half the time. Testing 30 parts brings this down to about 0.16.
from math import comb
def p_accept(N, K, n):
return comb(N - K, n) / comb(N, n)
for n in [10, 20, 30, 45]:
print(f"test {n:2d} parts: P(accept a lot with 5 defects) = {p_accept(100, 5, n):.4f}")test 10 parts: P(accept a lot with 5 defects) = 0.5838
test 20 parts: P(accept a lot with 5 defects) = 0.3193
test 30 parts: P(accept a lot with 5 defects) = 0.1608
test 45 parts: P(accept a lot with 5 defects) = 0.0462
Capture–recapture
Ecologists estimate a population size by catching and tagging animals, releasing them, and later catching . If of the second catch are tagged, suggests (the Lincoln–Petersen estimator). The hypergeometric distribution tells how reliable this is.
import random
rng = random.Random(5)
N_true, K, n = 1200, 100, 150
pond = [1] * K + [0] * (N_true - K) # 1 = tagged
estimates = []
for _ in range(2000):
k = sum(rng.sample(pond, n))
if k > 0:
estimates.append(K * n / k)
estimates.sort()
print("median estimate:", round(estimates[len(estimates) // 2]))
print("middle 90% of estimates:", round(estimates[100]), "to", round(estimates[-101]))median estimate: 1250
middle 90% of estimates: 833 to 2143
The estimates centre near the true 1200 but spread widely; more recaptures narrow the range.
In software
Gene-set enrichment and "is this keyword over-represented in these documents?" use the hypergeometric tail as a test (Fisher's exact test). Audits that sample records without replacement, and card-game AIs computing the odds of drawing a card, use the exact formula. Database query planners estimate how many matching rows a sample will contain the same way.
Exercises
Exercise 1: Aces
What is the probability of exactly 2 aces in a 5-card poker hand?
Answer
.
Solution
Hypergeometric with , , , .
Exercise 2: Variance comparison
Draw 10 from 40 items of which 20 are successes. Compare the variance with and without replacement.
Answer
Without replacement ; with replacement .
Solution
; the correction factor is .
Exercise 3: Fisher's exact test (code)
Out of 1000 documents, 50 mention "outage". Among the 40 documents from one bad day, 8 mention it. Compute for the hypergeometric distribution.
Answer
About 0.0005: very unlikely by chance.
Solution
Expected count by chance: .
from math import comb
N, K, n = 1000, 50, 40
tail = sum(comb(K, k) * comb(N - K, n - k) for k in range(8, n + 1)) / comb(N, n)
print(f"{tail:.6f}")0.000498
Test yourself
The hypergeometric distribution models…
- sampling with replacement
- sampling without replacement from a finite population
- waiting times
- continuous measurements
Compared with the binomial with the same mean, the hypergeometric variance is…
- larger
- smaller (by the factor )
- the same
- undefined