TExcelTAD
Home › Probability Theory › The Hypergeometric Distribution

The Hypergeometric Distribution

Draw without replacement from a finite population that has KK "successes" among NN items. The number of successes in a sample of size nn 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

Pr⁡[X=k]=(Kk)(N−Kn−k)(Nn)\Pr[X = k] = \frac{\binom{K}{k}\binom{N-K}{n-k}}{\binom{N}{n}}

Choose which kk of the KK successes and which n−kn - k of the N−KN - K failures are in the sample; every sample of size nn is equally likely (Combinations). The mean is nKNn\frac{K}{N}, the same as the binomial with p=K/Np = K/N, and the variance is

Var⁡(X)=nKN(1−KN)N−nN−1\operatorname{Var}(X) = n \frac{K}{N}\left(1 - \frac{K}{N}\right) \frac{N - n}{N - 1}

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 NN is much larger than nn the correction is close to 1 and the binomial is a fine approximation.

python · try it yourself
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 NN grows with K/N=1/4K/N = 1/4 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?

Pr⁡[X=0]=(50)(9510)(10010)≈0.584\Pr[X = 0] = \frac{\binom{5}{0}\binom{95}{10}}{\binom{100}{10}} \approx 0.584

So a lot with 5% defects passes more than half the time. Testing 30 parts brings this down to about 0.16.

python · try it yourself
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 NN by catching and tagging KK animals, releasing them, and later catching nn. If kk of the second catch are tagged, k/n≈K/Nk/n \approx K/N suggests N^=Kn/k\hat{N} = Kn/k (the Lincoln–Petersen estimator). The hypergeometric distribution tells how reliable this is.

python · try it yourself
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

(42)(483)(525)=6⋅172962598960≈0.0399\frac{\binom{4}{2}\binom{48}{3}}{\binom{52}{5}} = \frac{6 \cdot 17296}{2598960} \approx 0.0399.

Solution

Hypergeometric with N=52N = 52, K=4K = 4, n=5n = 5, k=2k = 2.

Exercise 2: Variance comparison

Draw 10 from 40 items of which 20 are successes. Compare the variance with and without replacement.

Answer

Without replacement 2.5⋅3039≈1.922.5 \cdot \frac{30}{39} \approx 1.92; with replacement 2.52.5.

Solution

np(1−p)=10⋅0.5⋅0.5=2.5np(1-p) = 10 \cdot 0.5 \cdot 0.5 = 2.5; the correction factor is N−nN−1=3039\frac{N - n}{N - 1} = \frac{30}{39}.

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 Pr⁡[X≥8]\Pr[X \ge 8] for the hypergeometric distribution.

Answer

About 0.0005: very unlikely by chance.

Solution

Expected count by chance: 40⋅50/1000=240 \cdot 50/1000 = 2.

python · try it yourself
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…

  1. sampling with replacement
  2. sampling without replacement from a finite population
  3. waiting times
  4. continuous measurements

Compared with the binomial with the same mean, the hypergeometric variance is…

  1. larger
  2. smaller (by the factor (N−n)/(N−1)(N - n)/(N - 1))
  3. the same
  4. undefined