Course: Statistics and Probability for Data Science (DS14270). Source deck: Samer El Kababji, SD05 — Discrete Distributions.
The hypergeometric distribution counts successes in sampling without replacement. The mathematics below is paired with base R and the deck’s tagged-animal and light-bulb examples.
Sampling from a finite population
Let a population contain \(N\) objects, of which \(M\) are successes. Draw \(n\) objects without replacement and let \(X\) be the number of successes drawn. Then
N <-25M <-5n <-10x <-0:min(n, M)formula_pmf <-choose(M, x) *choose(N - M, n - x) /choose(N, n)R_pmf <-dhyper(x, m = M, n = N - M, k = n)data.frame(x, formula_pmf, R_pmf)
p_exactly_2 <-dhyper(2, m =5, n =20, k =10)p_at_most_2 <-phyper(2, m =5, n =20, k =10)components <-dhyper(0:2, m =5, n =20, k =10)c(P_X_equals_2 = p_exactly_2, P_X_at_most_2 = p_at_most_2)
P_X_equals_2 P_X_at_most_2
0.3853755 0.6988142
Code
data.frame(x =0:2, probability = components)
x probability
1 0 0.05652174
2 1 0.25691700
3 2 0.38537549
The terms for \(X=0,1,2\) are approximately \(0.057\), \(0.257\), and \(0.385\); their sum is approximately \(0.699\).
Mean, variance, and finite-population correction
For \(X\sim\operatorname{Hypergeometric}(N,M,n)\),
The factor \((N-n)/(N-1)\) is the finite-population correction. It reduces variance because draws without replacement are dependent.
Code
finite_population_correction <- (N - n) / (N -1)mu <- n * M / Nvariance <- finite_population_correction * n * (M / N) * (1- M / N)c(mean = mu, variance = variance, sd =sqrt(variance))
mean variance sd
2 1 1
Binomial approximation
When the population is large compared with the sample, sampling without replacement changes the success probability only slightly. A binomial model with \(p=M/N\) can then approximate the hypergeometric model.
The deck considers \(N=10000\) bulbs, \(M=200\) defective bulbs, and a sample of \(n=5\) bulbs. For exactly two defective bulbs,
Code
N <-10000M <-200n <-5p <- M / Nc(hypergeometric =dhyper(2, m = M, n = N - M, k = n),binomial =dbinom(2, size = n, prob = p))
hypergeometric binomial
0.003748545 0.003764768
The two values are about \(0.00377\) because the sampling fraction is only \(5/10000\).
Simulation check (fictional extension)
Code
set.seed(14270)simulated_X <-rhyper(100000, m =5, n =20, k =10)c(simulated_P_X_at_most_2 =mean(simulated_X <=2),exact_P_X_at_most_2 =phyper(2, 5, 20, 10),simulated_mean =mean(simulated_X),theoretical_mean =10*5/25)