By Jamal Hussein · Last updated:
Hypergeometric Calculator P(X = k) = C(K,k) C(N−K,n−k) / C(N,n)
Enter the population size, how many successes it contains, how many items you draw without replacement, and the number of successes you want in the sample. You get the exact probability, all four cumulative probabilities, the mean and variance, and a chart of the whole distribution.
The hypergeometric distribution is the probability distribution of the number of successes in a sample drawn without replacement from a finite population that contains a known number of successes. This hypergeometric calculator returns P(X = k), the four cumulative probabilities, the mean and the variance for any N, K, n and k, and plots every possible outcome.
What is the hypergeometric distribution?
Picture a population of N items in which K are “successes” (hearts in a deck, defective parts in a lot, genes in a pathway) and the remaining N − K are not. You take n items and do not put any back. The count of successes in your sample, X, follows a hypergeometric distribution.
The phrase without replacement is the whole point. Each item you remove changes the make-up of what is left, so the draws are dependent: after one heart leaves a deck, the chance that the next card is a heart drops from 13/52 to 12/51. A model that ignores this is a binomial model, and for small populations it gives the wrong answer.
What is the hypergeometric formula?
Every sample of size n is equally likely, so the probability is a count of favourable samples divided by a count of all samples:
- N is the population size.
- K is the number of successes in the population.
- n is the sample size, drawn without replacement.
- k is the number of successes observed in the sample.
- \(\binom{a}{b}\) is the binomial coefficient “a choose b”, the number of ways to pick b items from a when order does not matter. You can evaluate any single one with the combination calculator.
X can only take values from \(\max(0,\,n+K-N)\) to \(\min(n,\,K)\). Outside that range the probability is zero, and the tool says so instead of raising an error. The mean and variance are:
The mean is the long-run average count, the same idea the expected value calculator works out for any discrete distribution. The last factor in the variance, (N − n)/(N − 1), is the finite population correction; it is what separates this variance from the binomial one.
Worked example: two hearts in a five-card hand
A standard deck has N = 52 cards and K = 13 hearts. You are dealt n = 5 cards. What is the probability that exactly k = 2 are hearts? These are the calculator’s default inputs.
- Ways to choose 2 of the 13 hearts: \(\binom{13}{2} = 78\).
- Ways to choose the other 3 cards from the 39 non-hearts: \(\binom{39}{3} = 9{,}139\).
- Ways to choose any 5 cards from 52: \(\binom{52}{5} = 2{,}598{,}960\).
- Divide: \(78 \times 9{,}139 = 712{,}842\), and \(712{,}842 / 2{,}598{,}960 = 0.274280\).
So about 27.4% of five-card hands hold exactly two hearts. The cumulative results are P(X ≤ 2) = 0.907233 and P(X ≥ 2) = 0.367047. The mean is 5 × 13 / 52 = 1.25 hearts per hand, and the variance is 5 × 0.25 × 0.75 × 47/51 = 0.863971.
Hypergeometric vs binomial: how much does replacement matter?
If each card were returned and the deck reshuffled before the next draw, every draw would have the same 0.25 chance of a heart and X would be binomial with n = 5 and p = 0.25. The table puts the two models side by side for the worked example.
| Hearts k | Hypergeometric (no replacement) | Binomial (with replacement) | Binomial minus hypergeometric |
|---|---|---|---|
| 0 | 0.221534 | 0.237305 | +0.015771 |
| 1 | 0.411420 | 0.395508 | −0.015912 |
| 2 | 0.274280 | 0.263672 | −0.010608 |
| 3 | 0.081543 | 0.087891 | +0.006348 |
| 4 | 0.010729 | 0.014648 | +0.003919 |
| 5 | 0.000495 | 0.000977 | +0.000481 |
The binomial puts too much weight on the extremes and too little near the mean. For five hearts it is off by almost a factor of two (0.000977 against 0.000495). Its variance is 0.9375, while the true variance is 0.863971, smaller by exactly the correction factor 47/51 = 0.9216.
When is the binomial approximation acceptable?
The gap closes as the sample becomes a smaller share of the population. Keep the proportion of hearts at 25% and the sample at 5, and P(X = 2) is 0.274280 for N = 52, 0.264690 for N = 520 and 0.263773 for N = 5,200, against a binomial value of 0.263672.
The usual rule of thumb is that the binomial is acceptable when the sample is under roughly 5% to 10% of the population, that is n/N < 0.05 for careful work and n/N < 0.10 for rough estimates. The five-card hand is 9.6% of the deck, right at the loose end of that rule, and the table shows errors in the second decimal place. When you have the exact population counts there is little reason to approximate at all.
Where is the hypergeometric distribution used?
- Card and lottery odds. The chance of exactly one ace in five cards is 0.299474 (N = 52, K = 4, n = 5, k = 1). In a 6-from-49 lottery, matching exactly three numbers has probability 0.017650, and the jackpot is 1 in 13,983,816.
- Acceptance sampling in quality control. A lot of 50 parts contains 5 defectives and an inspector tests 10. The probability that the sample shows no defects, so the lot passes a zero-defect plan, is 0.310563. A binomial shortcut would say 0.348678, because the sample is 20% of the lot.
- Fisher’s exact test. For a 2×2 table with fixed margins, the count in one cell is hypergeometric under the null hypothesis of no association. The one-sided p-value is the tail probability P(X ≥ k) that this page reports. The four cells are the same layout you enter in the confusion matrix calculator.
- Gene-set and feature enrichment. Out of N = 10,000 genes, K = 200 belong to a pathway. An experiment flags n = 150 genes and k = 8 of them are in the pathway, where only 3 would be expected by chance. P(X ≥ 8) = 0.010437 is the enrichment p-value. The same test checks whether a cluster, a set of top-ranked features or a list of search results is over-represented in a category.
Common mistakes
- Using the binomial for a small population. If the sample is more than about a tenth of the population, the draws are too dependent for a binomial model.
- Mixing up K and n. K counts successes in the population and n is how many items you draw. The formula happens to be symmetric in the two, but the labels matter when you read the result.
- Reporting P(X = k) as a p-value. A test needs a tail, P(X ≥ k) or P(X ≤ k), not the probability of the single observed count.
- Confusing “at least” with “more than”. P(X ≥ k) includes k and P(X > k) does not. With small samples the difference is large: 0.367047 against 0.092767 in the hearts example.
- Passing SciPy the arguments in textbook order. See the next section.
How do you compute it in Python with SciPy?
scipy.stats.hypergeom uses its own letters: M is the population size, n is the number of successes in the population and N is the sample size. In this page’s notation the call is hypergeom(N, K, n).
from scipy.stats import hypergeom
# SciPy order: M = population, n = successes in population, N = draws
M, n, N = 52, 13, 5
rv = hypergeom(M, n, N)
print(rv.pmf(2)) # 0.274280 P(X = 2)
print(rv.cdf(2)) # 0.907233 P(X <= 2)
print(rv.sf(1)) # 0.367047 P(X >= 2), sf(k-1) = P(X > k-1)
print(rv.mean()) # 1.25
print(rv.var()) # 0.863971
These match the worked example and the calculator’s default output. The argument order is documented on the scipy.stats.hypergeom reference page, and the test built on this distribution is scipy.stats.fisher_exact.
Frequently asked questions
What is the difference between the hypergeometric and binomial distributions?
The hypergeometric distribution models draws without replacement from a finite population, so the chance of success changes after every draw. The binomial distribution models independent draws with a constant chance of success, which is what you get when sampling with replacement or from a very large population.
When can I use the binomial instead of the hypergeometric distribution?
When the sample is a small share of the population, commonly under 5 to 10 percent. Below that level the finite population correction is close to 1 and the two distributions agree to about two decimal places.
What do N, K, n and k mean?
N is the population size, K is the number of successes in the population, n is the number of items drawn without replacement and k is the number of successes in the sample. K and n cannot exceed N.
What happens if k is larger than n or K?
The probability is 0. The number of successes in the sample can only run from the larger of 0 and n + K – N up to the smaller of n and K, and any value outside that range cannot occur.
Related calculators
Browse every tool in the statistics calculators hub, or continue with one of these: