Binomial Coefficients and the Bookkeeper Rule
The number of -element subsets of an -element set is the binomial coefficient , read " choose ". It counts committees, lottery tickets, poker hands, bit strings with ones and lattice paths on a grid. Its generalization, the multinomial coefficient, counts arrangements of letters with repeats (the bookkeeper rule).
The formula and its symmetry
From the division rule:
Symmetry: , because choosing which to take is the same as choosing which to leave out (a bijection).
| Question | Answer |
|---|---|
| 5-card poker hands from 52 cards | |
| lottery: 6 numbers from 49 | |
| bit strings of length 10 with exactly 3 ones | |
| shortest grid paths from to (right/up steps) |
The grid paths are 7 steps, of which you choose which 3 are "up": a bijection with 3-subsets of .
from math import comb
from itertools import combinations
print("C(52,5) =", comb(52, 5), " C(49,6) =", comb(49, 6))
bits = [s for s in range(2 ** 10) if bin(s).count("1") == 3]
print("10-bit strings with three 1s:", len(bits), "=", comb(10, 3))
def paths(x, y):
if x == 0 or y == 0:
return 1
return paths(x - 1, y) + paths(x, y - 1)
print("grid paths to (4,3):", paths(4, 3), "=", comb(7, 3))C(52,5) = 2598960 C(49,6) = 13983816
10-bit strings with three 1s: 120 = 120
grid paths to (4,3): 35 = 35
Pascal's triangle
Pascal's rule: for .
Combinatorial proof: fix one element . A -subset either contains (choose the other from ) or does not (choose all from the other ). The cases are disjoint, so add (sum rule).
The rule builds Pascal's triangle row by row, using only additions, which is how programs compute binomial coefficients without huge factorials:
row = [1]
for n in range(9):
print(f"n={n}: " + " ".join(f"{x:3d}" for x in row))
row = [1] + [row[i] + row[i + 1] for i in range(len(row) - 1)] + [1]n=0: 1
n=1: 1 1
n=2: 1 2 1
n=3: 1 3 3 1
n=4: 1 4 6 4 1
n=5: 1 5 10 10 5 1
n=6: 1 6 15 20 15 6 1
n=7: 1 7 21 35 35 21 7 1
n=8: 1 8 28 56 70 56 28 8 1
The bookkeeper rule (multinomials)
If a sequence has letters made of copies of letter 1, of letter 2, ..., of letter , the number of distinct arrangements is the multinomial coefficient
The word COMMITTEE has C, O, I once, M, T, E twice: arrangements. The same number counts ways to split a set into labelled groups of given sizes: splitting 10 developers into a team of 5, a team of 3 and a team of 2 can be done in ways. With only two kinds of letters, the multinomial is the binomial: .
from math import factorial
from collections import Counter
def multinomial(word):
out = factorial(len(word))
for c in Counter(word).values():
out //= factorial(c)
return out
for w in ["COMMITTEE", "BOOKKEEPER", "ABRACADABRA"]:
print(w, multinomial(w))
print("teams of 5, 3, 2 from 10:", factorial(10) // (factorial(5) * factorial(3) * factorial(2)))COMMITTEE 45360
BOOKKEEPER 151200
ABRACADABRA 83160
teams of 5, 3, 2 from 10: 2520
Poker probabilities
With equally likely hands, probability = favourable hands / all hands. A full house (three of one rank, two of another): choose the rank of the triple (13), its 3 suits (), the rank of the pair (12 remaining), its 2 suits ():
Order matters for the two ranks here (a triple of kings with a pair of 7s differs from a triple of 7s with a pair of kings), so we use 13 and 12, not .
In software
math.comb and itertools.combinations are the tools. Binomial coefficients size test matrices (choosing of feature flags), estimate collisions, appear in the binomial distribution of probability, and count the paths a dynamic-programming grid algorithm must consider.
Exercises
Exercise 1: Committees with roles
From 12 people, choose a committee of 4 with one chair among them. How many ways?
Answer
Solution
Choose the 4 members, then the chair among them: . Alternatively choose the chair first (12 ways), then 3 more members: .
Exercise 2: Two pairs
How many 5-card hands are "two pair" (two cards of one rank, two of another, and a fifth card of a third rank)?
Answer
Solution
Here the two pair ranks are interchangeable, so choose them as a set: . Suits for each pair: . The fifth card: any of the cards of other ranks.
Exercise 3: Verify by brute force (code)
Count full houses by enumerating all hands? That is 2.6 million hands: feasible but slow in a browser. Instead verify the method on a small deck with 5 ranks and 4 suits.
Answer
, and brute force agrees.
Solution
from itertools import combinations
from collections import Counter
from math import comb
deck = [(r, s) for r in range(5) for s in range(4)]
brute = sum(1 for hand in combinations(deck, 5)
if sorted(Counter(r for r, _ in hand).values()) == [2, 3])
print(brute, 5 * comb(4, 3) * 4 * comb(4, 2))480 480
Test yourself
What is ?
- 16
- 28
- 56
- 64
Which identity is Pascal's rule?
How many distinct arrangements does AABBB have?
- 10
- 20
- 60
- 120