Counting Combinations
Intuition
"How many different 3-person teams can be chosen from 5 students?" Order does not matter, so {Ann, Bob, Cy} is the same team as {Cy, Ann, Bob}. The answer is the binomial coefficient .
Precise explanation
Computing three factorials is wasteful. The multiplicative formula builds the result step by step and stays an integer at every step:
r = 1
for i in range(1, k + 1):
r = r * (n - k + i) // i # exact: r*(n-k+i) is always divisible by i
Because , use k = min(k, n - k) to do fewer steps.
Pascal's rule gives Pascal's triangle.
Worked example
: r = 1 → 1·4//1 = 4 → 4·5//2 = 10.
Common mistakes
- Using
/(float division) → large values lose precision (e.g.comb(60, 30)). - Writing
r = r // i * (n-k+i)— dividing first may not be exact. - Forgetting and for .
Why this matters
Binomial coefficients give the binomial distribution (ST-08), count subsets for exact Shapley values (XA-11), and count n-gram / feature combinations.
Library implementation
math.comb(n, k) is the standard-library binomial coefficient (exact integers, returns 0 when
k > n). ST-08 uses it directly once you have seen how it works.
References
- Mathematics for Machine Learning — Marc Peter Deisenroth, A. Aldo Faisal, Cheng Soon Ong (Cambridge University Press) (
S009)