Contents/ Part VI Β· Probability/ Chapter 33

Counting & Combinatorics

To compute a probability you often must first answer a deceptively hard question: how many outcomes are there? Combinatorics counts them without listing, with three tools, the multiplication principle, permutations, and combinations.

⏱️ ~13 min read
🐍 Notebook included
πŸ“Š Chapter 33

Classical probability is favorable outcomes divided by total outcomes (see Probability Fundamentals). The catch is that both counts can be enormous, and listing them by hand is hopeless. Combinatorics is the art of counting outcomes without writing them all down.

n!
Three tools cover almost everything: the multiplication principle for a sequence of choices, permutations when order matters, and combinations when it does not. The factorial, n! = n × (n-1) × ... × 1, is their shared building block.
πŸ”’
One question decides almost everything

When you select items, ask: does the order matter? If reordering the same items counts as a new outcome (a podium, a password), it is a permutation. If it does not (a committee, a lottery ticket), it is a combination. That single question routes you to the right formula every time.

1

The Multiplication Principle

The most fundamental rule: if a task is a sequence of independent choices with n₁, n₂, n₃, ... options at each step, the total number of outcomes is their product.

A sequence of choices multiplies: 2 starters × 3 mains = 6 meals start Soup Salad Soup, PastaSoup, SteakSoup, Fish Salad, PastaSalad, SteakSalad, Fish 6 meals

Two starters, then three mains for each: 2 × 3 = 6 meals. The principle scales without effort, a 4-digit PIN is 10⁴ = 10,000 possibilities, and a plate of 3 letters then 3 digits is 26³ × 10³ = 17,576,000. Each independent slot simply multiplies in its own count.

2

Permutations: When Order Matters

A permutation is an ordered arrangement. Arranging all n distinct items gives n! orderings; selecting and ordering r of them gives:

πŸ”
The permutation formula

P(n, r) = n! / (n - r)!

Awarding gold, silver, and bronze among 8 runners is P(8, 3) = 8 × 7 × 6 = 336: the first medal has 8 candidates, then 7 remain, then 6. Arranging all of something is the special case P(n, n) = n!. By convention 0! = 1, which keeps every formula consistent.

3

Combinations: When Order Does Not

A combination is an unordered selection. Because order is ignored, each group is counted once. The clean way to see it: a combination is a permutation with its r! reorderings divided out.

Choosing 2 from {A, B, C}: order matters, or not PERMUTATIONS (order matters) AB BA AC CA BC CB P(3, 2) = 6 divide by 2! = 2 each pair has 2 orderings COMBINATIONS (order ignored) AB AC BC C(3, 2) = 3 C(n, r) = P(n, r) / r! = n! / (r! (n - r)!)

Choosing 2 of {A, B, C} gives 6 ordered pairs but only 3 actual pairs, because AB and BA are the same selection. That is the whole story of C(n, r) = n! / (r! (n - r)!), read "n choose r": take the permutations and divide away the r! ways to reorder each group.

4

Counting for Probability

Now the payoff. Classical probability is just favorable outcomes over total outcomes, and the tools above count both. First, the decision guide that picks your tool:

How are you counting? A sequence of choices multiply n₁ × n₂ × n₃ ... e.g. PINs, license plates Arrange (order matters) permutation P(n, r) = n! / (n - r)! e.g. podium, passwords Select a group (no order) combination C(n, r) = n! / (r! (n - r)!) e.g. committees, lottery

The lottery is a pure combination: choosing 6 numbers from 49 ignores order, so there are C(49, 6) = 13,983,816 possible tickets, and one ticket wins the jackpot with probability about 1 in 14 million. A poker flush is favorable-over-total counting: pick a suit and then 5 of its 13 cards, 4 × C(13, 5) = 5,148 hands, out of C(52, 5) = 2,598,960, so P(flush) ≈ 0.00198, about 1 in 505.

Where the combinations live: Pascal's triangle

Every combination C(n, r) is an entry of Pascal's triangle, and row n holds the coefficients of (a + b)ⁿ. That link is exactly why C(n, r), the number of ways to get r successes in n trials, drives the binomial distribution in the chapters ahead.

Pascal's triangle: every entry is C(n, r) 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 each interior entry is the sum of the two above it, and row n sums to 2ⁿ.
πŸ€–
A note on history, and why it matters

Pascal documented this triangle in 1653, but it was known centuries earlier to Pingala in India, and later to al-Karaji, Omar Khayyam, and Yang Hui, a reminder that mathematics is rarely the work of one name. For data science, counting underpins the binomial and hypergeometric distributions, the combinatorics of sampling, and the feature-interaction counts that explode model complexity. Counting cleanly is a quiet superpower.

5

Counting in Machine Learning & AI

Counting is not just for cards and lotteries. The same three tools quietly govern how machine-learning systems are built, why some are expensive, and where brute force has to give way to clever search.

The clearest example is the combinatorial explosion. Deciding which of n features to feed a model is a combinations problem: there are 2ⁿ possible subsets in total. With just 20 features that is already over a million, so you can never simply "try them all":

Feature subsets = 2ⁿ: each added feature DOUBLES the possibilities subsets (log scale) 325 features 1,02410 features 32,76815 features 1,048,57620 features just 20 features give over a million subsets, exhaustive feature selection is hopeless.

That single picture is the famous curse of dimensionality, and it is why feature selection, neural architecture search, and hyperparameter tuning all rely on smart shortcuts rather than enumeration. Here is where the chapter's three tools show up across machine learning:

Counting toolIn ML / AI it appears asA concrete number
Multiplication principleThe size of a hyperparameter grid; counting a network's parameters3 rates × 4 depths × 5 trees = 60 models to train
Combinations, 2ⁿFeature subsets, cross-validation folds, ensemble samples20 features → 2²⁰ ≈ 1,048,576 subsets
Binomial coefficient C(n, k)The binomial behind naive Bayes and logistic likelihoods, and significance testsways to get k of n predictions right by chance
Pairs, C(n, 2)Self-attention comparing every pair of tokens; similarity matrices512 tokens → C(512, 2) ≈ 130,816 pairs
Permutations, n!Permutation feature importance, permutation tests, sequence orderingsshuffling a column among its n! possible orders
πŸ€–
Why counting matters for AI research

Two ideas above are worth pausing on. Attention is pairwise: a Transformer compares all C(n, 2) ≈ n²/2 pairs of tokens, so doubling the context length roughly quadruples the work, which is exactly why long-context models are costly and "efficient attention" is a hot research area. And combinatorial explosion (2ⁿ feature subsets, branching game trees, Go's roughly 10¹⁷⁰ positions) is the reason modern AI learns to search instead of enumerating: when you cannot count your way through the space, you must approximate it. Counting tells you when brute force is doomed, which is half of knowing what to do instead.

🐍

Count it in Python

The companion notebook uses math.factorial, math.perm, and math.comb alongside itertools to verify every count by enumeration: the multiplication principle, permutations and factorials, combinations (and the C = P / r! relationship), the lottery and poker probabilities, and Pascal's triangle drawn out to row 8.

πŸ““ View Notebook (code & outputs) β–Ά Open in Colab ⬇ View / Download on GitHub

View opens the rendered notebook instantly (no setup). Open in Colab runs & edits it live in your browser. To run locally, install numpy and matplotlib and launch jupyter notebook.

πŸŽ“ Key Takeaways

  • βœ“Multiplication principle: a sequence of independent choices multiplies, n₁ × n₂ × n₃ ...
  • βœ“Permutation (order matters): P(n, r) = n! / (n - r)!; arrange all n in n! ways.
  • βœ“Combination (order ignored): C(n, r) = n! / (r! (n - r)!) = P(n, r) / r!.
  • βœ“Ask "does order matter?" to pick permutation vs combination, every time.
  • βœ“Counting feeds probability: favorable / total, both counted; Pascal's triangle holds every C(n, r) and powers the binomial.
  • βœ“Counting shapes ML/AI: hyperparameter grids multiply, feature subsets explode as 2ⁿ, attention compares C(n, 2) token pairs, the reason AI learns to search rather than enumerate.
6

Practice Challenges

Five short challenges, beginner to intermediate. Try them on paper or in Python before checking the solutions.

1

Count the menus

A cafe offers 4 mains, 3 sides, and 5 drinks. How many distinct meals (one of each)? And if you add a choice of 2 desserts?

Hint: multiply the options at each step.
2

Officers (order matters)

From a club of 10 members, how many ways to choose a President, Vice-President, and Treasurer (distinct roles)?

Hint: roles differ, so it is P(10, 3).
3

Committee & handshakes

From the same 10 members, how many 3-person committees (equal roles)? And how many handshakes if all 10 shake hands once?

Hint: groups and pairs are combinations, C(10, 3) and C(10, 2).
4

Which one?

Decide order-matters-or-not and compute: (a) a 4-letter password from 26 letters, no repeats; (b) choosing 4 pizza toppings from 12.

Hint: password is ordered (permutation); toppings are a set (combination).
5

Counting for probability

From a 52-card deck, what is the probability a 5-card hand is exactly four-of-a-kind (four of one rank plus any fifth card)?

Hint: favorable = 13 ranks × 48 remaining cards, over C(52, 5).
βœ…
Check your work

A fully-worked solutions notebook walks through all five challenges, with each count verified in code. Try them yourself first, then compare.

πŸ““ View Solutions β–Ά Open Solutions in Colab ⬇ View / Download on GitHub
7

Quiz: Test Yourself

Eight quick questions on counting and combinatorics. Answer them, hit Check Answers, and keep refining until you score 100%. Your progress is saved, so you can hop back to the chapter and return anytime.