The Hypergeometric Distribution

SD05 — Discrete Distributions

Author

Samer El Kababji

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

\[ P(X=x)=\frac{\binom Mx\binom{N-M}{n-x}}{\binom Nn}, \]

where

\[ \max(0,n-N+M)\leq x\leq\min(n,M). \]

Code
N <- 25
M <- 5
n <- 10
x <- 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)
  x formula_pmf       R_pmf
1 0 0.056521739 0.056521739
2 1 0.256916996 0.256916996
3 2 0.385375494 0.385375494
4 3 0.237154150 0.237154150
5 4 0.059288538 0.059288538
6 5 0.004743083 0.004743083
Code
sum(R_pmf)
[1] 1

In R, m is the number of successes, n is the number of failures, and k is the sample size. Those argument names differ from the mathematical symbols.

Tagged-animal example

Among \(N=25\) animals, \(M=5\) are tagged. If \(n=10\) are recaptured, then

\[ P(X=2)=\frac{\binom52\binom{20}8}{\binom{25}{10}}\approx0.385. \]

Code
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)\),

\[ E(X)=n\frac MN, \]

and

\[ \operatorname{Var}(X)= \frac{N-n}{N-1}\,n\frac MN\left(1-\frac MN\right). \]

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 / N
variance <- 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 <- 10000
M <- 200
n <- 5
p <- M / N

c(
  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
)
simulated_P_X_at_most_2     exact_P_X_at_most_2          simulated_mean 
              0.7012400               0.6988142               1.9940500 
       theoretical_mean 
              2.0000000 

Practice questions

  1. A lot has 40 devices, 8 defective. If 6 are sampled without replacement, find \(P(X=2)\).
  2. Find \(P(X\leq1)\).
  3. Compute \(E(X)\) and \(\operatorname{Var}(X)\).
  4. Explain why a binomial model is not exact here.
  1. dhyper(2, 8, 32, 6) gives approximately \(0.2623\).
  2. phyper(1, 8, 32, 6) gives approximately \(0.6558\).
  3. \(E(X)=1.2\) and \(\operatorname{Var}(X)=(34/39)(6)(0.2)(0.8)\approx0.837\).
  4. Without replacement, one draw changes the success probability for the next draw, so the trials are dependent.