TExcelTAD
Home › Probability Theory › Combinations

Combinations

When order does not matter, count combinations: the number of kk-element subsets of an nn-element set is the binomial coefficient

(nk)=n!k! (n−k)!\binom{n}{k} = \frac{n!}{k!\,(n-k)!}

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 rr red and bb blue balls; draw nn without replacement. Every nn-subset is equally likely, so the probability of exactly kk red balls is

Pr⁡[k red]=(rk)(bn−k)(r+bn)\Pr[k \text{ red}] = \frac{\binom{r}{k}\binom{b}{n-k}}{\binom{r+b}{n}}

choose which kk reds and which n−kn - k blues, divided by all ways. This is the hypergeometric distribution.

Poker hands

A poker hand is a 5-card subset of 52 cards: (525)=2,598,960\binom{52}{5} = 2{,}598{,}960 hands. Counting by choices:

  • four of a kind: rank of the four (13), the fifth card (48): 13⋅48=62413 \cdot 48 = 624;
  • full house: rank of the triple (13) and its suits (43)\binom{4}{3}, rank of the pair (12) and its suits (42)\binom{4}{2}: 13⋅4⋅12⋅6=374413 \cdot 4 \cdot 12 \cdot 6 = 3744;
  • flush (including straight flushes): suit (4), five ranks (135)\binom{13}{5}: 51485148.
python · try it yourself
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 1/(496)=1/13,983,8161/\binom{49}{6} = 1/13{,}983{,}816. The chance of exactly kk matches is hypergeometric: choose kk of the 6 winning numbers and 6−k6 - k of the 43 others.

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

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 (2010)=184,756\binom{20}{10} = 184{,}756 but not for (6030)≈1017\binom{60}{30} \approx 10^{17}; 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

150/330=5/11150/330 = 5/11.

Solution

(62)(52)=15⋅10=150\binom{6}{2}\binom{5}{2} = 15 \cdot 10 = 150 favourable; (114)=330\binom{11}{4} = 330 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

123,552123{,}552.

Solution

Choose the two pair ranks (132)=78\binom{13}{2} = 78, their suits (42)2=36\binom{4}{2}^2 = 36, and the fifth card from the other 44 cards: 78⋅36⋅44=123,55278 \cdot 36 \cdot 44 = 123{,}552. 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 123552/2598960123552/2598960.

Answer

About 0.0475.

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

  1. 52552^5
  2. 52!/47!52!/47!
  3. (525)=2,598,960\binom{52}{5} = 2{,}598{,}960
  4. 5!5!

Drawing 3 balls without replacement from 4 red and 6 blue, P(all red)P(\text{all red}) is…

  1. (43)/(103)=1/30\binom{4}{3}/\binom{10}{3} = 1/30
  2. (4/10)3(4/10)^3
  3. 4/104/10
  4. 3/103/10