Combinations
When order does not matter, count combinations: the number of -element subsets of an -element set is the binomial coefficient
The properties of binomial coefficients (Pascal's triangle, the binomial theorem) are in Discrete Mathematics. Here they compute probabilities of draws, hands and lotteries.
Drawing without replacement
An urn has red and blue balls; draw without replacement. Every -subset is equally likely, so the probability of exactly red balls is
choose which reds and which blues, divided by all ways. This is the hypergeometric distribution.
Poker hands
A poker hand is a 5-card subset of 52 cards: hands. Counting by choices:
- four of a kind: rank of the four (13), the fifth card (48): ;
- full house: rank of the triple (13) and its suits , rank of the pair (12) and its suits : ;
- flush (including straight flushes): suit (4), five ranks : .
from math import comb
from itertools import combinations
from collections import Counter
total = comb(52, 5)
counts = {"four of a kind": 13 * 48, "full house": 13 * 4 * 12 * 6, "flush": 4 * comb(13, 5)}
for name, c in counts.items():
print(f"{name:15s} {c:6d} P = {c / total:.6f}")
# brute-force check on all 2,598,960 hands would be slow in a browser;
# check the full house count on a 3-suit deck instead (formula 13*3*12*3)
deck = [(r, s) for r in range(13) for s in range(3)]
fh = 0
for hand in combinations(deck, 5):
shape = sorted(Counter(r for r, s in hand).values())
fh += shape == [2, 3]
print("3-suit deck full houses:", fh, "formula:", 13 * comb(3, 3) * 12 * comb(3, 2))four of a kind 624 P = 0.000240
full house 3744 P = 0.001441
flush 5148 P = 0.001981
3-suit deck full houses: 468 formula: 468
The brute-force check on a smaller deck confirms the counting pattern without enumerating all 2.6 million real hands.
Lotteries
In a 6-of-49 lottery, the chance of matching all six is . The chance of exactly matches is hypergeometric: choose of the 6 winning numbers and of the 43 others.
from math import comb
total = comb(49, 6)
for k in range(7):
ways = comb(6, k) * comb(43, 6 - k)
print(f"{k} matches: 1 in {total / ways:>12,.1f}")0 matches: 1 in 2.3
1 matches: 1 in 2.4
2 matches: 1 in 7.6
3 matches: 1 in 56.7
4 matches: 1 in 1,032.4
5 matches: 1 in 54,200.8
6 matches: 1 in 13,983,816.0
Choosing the right count
The most common error in probability is mixing ordered and unordered counting: counting ordered outcomes in the numerator and unordered ones in the denominator. A safe rule is to decide once whether your outcomes are sequences or sets and count both the event and the sample space the same way. The probability comes out the same either way, because ordering multiplies both counts by .
In software
Combinations show up in feature selection (how many subsets of features), test design (pairwise testing covers all pairs of options instead of all combinations), and in counting the possible keys or passwords of a scheme. itertools.combinations enumerates subsets lazily, which is fine for but not for ; counting with math.comb is instant either way.
Exercises
Exercise 1: A committee
A committee of 4 is chosen at random from 6 women and 5 men. What is the probability that it has exactly 2 women?
Answer
.
Solution
favourable; total.
Exercise 2: Two pairs
Count the poker hands with two pairs (two ranks appearing twice, plus a fifth card of a third rank).
Answer
.
Solution
Choose the two pair ranks , their suits , and the fifth card from the other 44 cards: . Choosing the ranks as an unordered pair avoids double counting.
Exercise 3: Check by sampling (code)
Estimate the probability of two pairs by dealing 200,000 random hands, and compare with .
Answer
About 0.0475.
Solution
import random
from collections import Counter
rng = random.Random(2)
deck = [(r, s) for r in range(13) for s in range(4)]
n, hits = 200_000, 0
for _ in range(n):
hand = rng.sample(deck, 5)
hits += sorted(Counter(r for r, s in hand).values()) == [1, 2, 2]
print(hits / n, round(123552 / 2598960, 4))0.04743 0.0475
Test yourself
How many 5-card hands can be dealt from a 52-card deck?
Drawing 3 balls without replacement from 4 red and 6 blue, is…