Practice / Probability for ML

Counting: permutations, combinations and the binomial theorem

Ten counting problems: the size of a hyperparameter grid and the number of ordered augmentation pipelines by the multiplication principle, choosing three features of ten against ranking them, splitting twelve examples into three minibatches with a multinomial coefficient, benchmark suites with at least one text task by the complement, the coefficient of x² in (2x − 3)⁵ from the binomial theorem, counting feature subsets and even-sized subsets with (1 + 1)ⁿ and (1 − 1)ⁿ, sharing ten GPU nodes among four jobs by stars and bars, the chance of a hash collision among thirty keys and the birthday bound, derangements of five shuffled labels by inclusion–exclusion, and drawing a test batch with exactly two positives by Vandermonde's identity, with worked solutions and the mistakes of counting arrangements when order does not matter, dropping the power on the coefficient inside a binomial term, choosing one item and then the rest, putting one bar too many in stars and bars, and adding overlapping cases instead of using inclusion–exclusion.

Before you start

Most probability on a finite set of equally likely outcomes is counting: the probability of an event is the number of outcomes in it divided by the number of outcomes in all. The hard part is counting without listing, and almost every count comes from a handful of rules. Choices made one after another multiply. Arranging kk of nn distinct things in order gives a permutation; choosing them without caring about order divides that by the k!k! orders and gives a combination (nk)\binom nk. The same (nk)\binom nk are the coefficients of the binomial theorem, which is why the binomial distribution has them, and why sums of them can be found by substituting numbers into (x+y)n(x + y)^n. These ten problems count hyperparameter grids, feature subsets, minibatch splits, resource allocations and hash collisions, then use the counts as probabilities, including two cases where the obvious count is wrong and inclusion–exclusion fixes it.

  • Multiplication principle. If a first choice can be made in aa ways and, whatever it was, a second in bb ways, the pair can be made in abab ways.
  • Permutations. The number of ordered arrangements of kk of nn distinct items is P(n,k)=n(n−1)⋯(n−k+1)=n!(n−k)!P(n, k) = n(n - 1)\cdots(n - k + 1) = \frac{n!}{(n - k)!}, and all nn of them can be ordered in n!n! ways, with 0!=10! = 1.
  • Combinations. The number of kk-element subsets of nn items, order ignored, is (nk)=n!k! (n−k)!=P(n,k)k!\binom nk = \frac{n!}{k!\,(n - k)!} = \frac{P(n, k)}{k!}. Symmetry: (nk)=(nn−k)\binom nk = \binom n{n - k}. Pascal: (nk)=(n−1k−1)+(n−1k)\binom nk = \binom{n - 1}{k - 1} + \binom{n - 1}{k}.
  • Multinomial coefficients. Splitting nn distinct items into labeled groups of sizes k1,…,kmk_1, \dots, k_m with k1+⋯+km=nk_1 + \dots + k_m = n can be done in n!k1!⋯km!\frac{n!}{k_1!\cdots k_m!} ways. If groups of the same size are unlabeled, divide by the orders of those groups.
  • Binomial theorem. (x+y)n=∑k=0n(nk)xkyn−k(x + y)^n = \sum_{k=0}^n \binom nk x^ky^{n - k}. Setting x=y=1x = y = 1 gives ∑k(nk)=2n\sum_k \binom nk = 2^n, and x=−1x = -1, y=1y = 1 gives ∑k(−1)k(nk)=0\sum_k (-1)^k\binom nk = 0 for n≥1n \ge 1.
  • Stars and bars. The number of ways to write nn as an ordered sum of kk nonnegative integers, that is, to put nn identical items into kk distinct boxes, is (n+k−1k−1)\binom{n + k - 1}{k - 1}: nn stars and k−1k - 1 bars. With every box nonempty it is (n−1k−1)\binom{n - 1}{k - 1}.
  • Complement and inclusion–exclusion. "At least one" is often easiest as all minus none. For a union, ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣\lvert A \cup B\rvert = \lvert A\rvert + \lvert B\rvert - \lvert A \cap B\rvert, and in general add the single sets, subtract the pairwise intersections, add the triples, and so on.

Problems

  1. ·

    A grid search tries 44 learning rates, 33 batch sizes and 22 optimizers. How many configurations does it train? Separately, an augmentation pipeline applies 33 different transforms, chosen from 55, one after another, and the order matters. How many pipelines are there?

  2. ·

    A model may keep 33 of 1010 candidate features. How many different feature sets are possible? How many ways are there to rank the top 33 features, first, second and third?

  3. ··

    Twelve distinct training examples are split into three minibatches of four. How many splits are there if the minibatches are labeled first, second and third? How many if only the grouping matters?

  4. ··

    A benchmark suite of 44 tasks is drawn from 66 vision tasks and 55 text tasks. How many suites include at least one text task? If the suite is drawn at random, what is the probability that it does?

  5. ··

    Find the coefficient of x2x^2 in (2x−3)5(2x - 3)^5, and the sum of all the coefficients of its expansion.

  6. ··

    A model has 1010 optional input features. How many subsets of the features are there, how many are nonempty, and how many have an even number of features? Use the binomial theorem.

  7. ··

    A cluster has 1010 identical GPU nodes to share among 44 jobs. How many allocations are there if a job may get no nodes? How many if every job gets at least one?

  8. ···

    Thirty keys are hashed independently and uniformly into 10001000 buckets. Find the probability that at least two keys share a bucket, compare it with the approximation 1−e−n(n−1)/(2N)1 - e^{-n(n - 1)/(2N)} and with the sum of the pair probabilities, and find the smallest number of keys for which a collision is more likely than not.

  9. ···

    Five labeled test images are shuffled at random, so each permutation of the labels is equally likely. Find the number of permutations in which no image keeps its own label, and the probability of that.

  10. ··

    A test set has 88 positive and 1212 negative examples. A batch of 55 is drawn without replacement. Find the probability that exactly 22 positives are drawn, and use Vandermonde's identity to check that the probabilities for 00 to 55 positives add up to 11.

Worked solutions

Problem 1

A grid search tries 44 learning rates, 33 batch sizes and 22 optimizers. How many configurations does it train? Separately, an augmentation pipeline applies 33 different transforms, chosen from 55, one after another, and the order matters. How many pipelines are there?

  1. 4⋅3⋅2=244 \cdot 3 \cdot 2 = 24The multiplication principle: every learning rate pairs with every batch size and every optimizer.
  2. 55 choices for the first transformAny of the five.
  3. 44 for the second, 33 for the thirdA transform is not repeated, so each choice leaves one fewer.
  4. P(5,3)=5⋅4⋅3P(5, 3) = 5 \cdot 4 \cdot 3Equivalently 5!2!\frac{5!}{2!}.
  5. 2424 configurations and 6060 pipelinesIf the order did not matter, the 3!=63! = 6 orders of each set of transforms would be one choice, and there would be (53)=10\binom53 = 10.

Problem 2

A model may keep 33 of 1010 candidate features. How many different feature sets are possible? How many ways are there to rank the top 33 features, first, second and third?

  1. P(10,3)=10⋅9⋅8=720P(10, 3) = 10 \cdot 9 \cdot 8 = 720Ordered choices: the ranking.
  2. Each set of 33 features appears in 3!=63! = 6 rankingsThe same three features ranked in every possible order.
  3. (103)=720/6\binom{10}{3} = 720/6Divide out the orders.
  4. (103)=10⋅9⋅83⋅2⋅1=120\binom{10}{3} = \frac{10 \cdot 9 \cdot 8}{3 \cdot 2 \cdot 1} = 120Check with symmetry: (107)\binom{10}{7} is also 120120.
  5. 120120 feature sets and 720720 rankingsA feature set is a subset, so order does not matter; a ranking is an arrangement. Using 720720 for the sets overcounts six times (Mistake 1).

Problem 3

Twelve distinct training examples are split into three minibatches of four. How many splits are there if the minibatches are labeled first, second and third? How many if only the grouping matters?

  1. (124)=495\binom{12}{4} = 495 ways to fill the first batchChoose its four examples.
  2. (84)=70\binom84 = 70 for the second, (44)=1\binom44 = 1 for the thirdChoose from what is left.
  3. 495⋅70⋅1=34650495 \cdot 70 \cdot 1 = 34650The multiplication principle.
  4. 12!4! 4! 4!=34650\frac{12!}{4!\,4!\,4!} = 34650The same count as a multinomial coefficient.
  5. 3465034650 labeled splits and 34650/3!=577534650/3! = 5775 unlabeled onesWithout labels, each grouping was counted once for each of the 3!=63! = 6 ways to number its three batches.

Problem 4

A benchmark suite of 44 tasks is drawn from 66 vision tasks and 55 text tasks. How many suites include at least one text task? If the suite is drawn at random, what is the probability that it does?

  1. (114)=330\binom{11}{4} = 330 suites in allAny 44 of the 1111 tasks.
  2. (64)=15\binom64 = 15 suites with no text taskAll four tasks from the six vision tasks.
  3. 330−15=315330 - 15 = 315The complement: all suites minus those with none.
  4. Check: (51)(63)+(52)(62)+(53)(61)+(54)\binom51\binom63 + \binom52\binom62 + \binom53\binom61 + \binom54Split by the number of text tasks, 11 to 44.
  5. 315315 suites, with probability 315/330=21/22≈0.955315/330 = 21/22 \approx 0.955The check sums 100+150+60+5=315100 + 150 + 60 + 5 = 315. Picking one text task first and then any three of the rest gives 600600 (Mistake 3).

Problem 5

Find the coefficient of x2x^2 in (2x−3)5(2x - 3)^5, and the sum of all the coefficients of its expansion.

  1. (2x−3)5=∑k(5k)(2x)k(−3)5−k(2x - 3)^5 = \sum_k \binom5k(2x)^k(-3)^{5 - k}The binomial theorem with the two terms 2x2x and −3-3, signs included.
  2. k=2k = 2: (52)(2x)2(−3)3\binom52(2x)^2(-3)^3The term with x2x^2.
  3. (52)=10\binom52 = 10, (2x)2=4x2(2x)^2 = 4x^2, (−3)3=−27(-3)^3 = -27The 22 is squared along with the xx.
  4. 10⋅4⋅(−27)=−108010 \cdot 4 \cdot (-27) = -1080Multiply the three factors.
  5. The coefficient of x2x^2 is −1080-1080; the coefficients sum to (2−3)5=−1(2 - 3)^5 = -1Setting x=1x = 1 adds up every coefficient. Forgetting to square the 22 gives −540-540 (Mistake 2).

Problem 6

A model has 1010 optional input features. How many subsets of the features are there, how many are nonempty, and how many have an even number of features? Use the binomial theorem.

  1. ∑k=010(10k)=(1+1)10\sum_{k=0}^{10}\binom{10}{k} = (1 + 1)^{10}Count the subsets of each size kk and add; that is the binomial theorem at x=y=1x = y = 1.
  2. 210=10242^{10} = 1024Equivalently, each feature is in or out: 22 choices, ten times.
  3. 1024−1=10231024 - 1 = 1023 nonemptyRemove the empty set.
  4. ∑k(−1)k(10k)=(1−1)10=0\sum_k(-1)^k\binom{10}{k} = (1 - 1)^{10} = 0So the even-sized and odd-sized subsets are equal in number.
  5. 10241024 subsets, 10231023 nonempty, and 512512 of even sizeEven plus odd is 10241024 and even minus odd is 00, so each is 512512. Directly: 1+45+210+210+45+1=5121 + 45 + 210 + 210 + 45 + 1 = 512.

Problem 7

A cluster has 1010 identical GPU nodes to share among 44 jobs. How many allocations are there if a job may get no nodes? How many if every job gets at least one?

  1. An allocation is a row of 1010 stars split by 33 barsThe stars before the first bar go to job 11, between the first and second bars to job 22, and so on.
  2. 1313 positions, of which 33 are barsFour jobs need only three dividers.
  3. (133)=286\binom{13}{3} = 286Choose where the bars go.
  4. At least one each: give every job a node first, then share the other 66That leaves (6+33)=(93)\binom{6 + 3}{3} = \binom93.
  5. 286286 allocations, and 8484 with every job busy(93)=84\binom93 = 84, the same as (n−1k−1)\binom{n - 1}{k - 1} with n=10n = 10, k=4k = 4. Using 44 bars for 44 jobs gives (144)=1001\binom{14}{4} = 1001 (Mistake 4).

Problem 8

Thirty keys are hashed independently and uniformly into 10001000 buckets. Find the probability that at least two keys share a bucket, compare it with the approximation 1−e−n(n−1)/(2N)1 - e^{-n(n - 1)/(2N)} and with the sum of the pair probabilities, and find the smallest number of keys for which a collision is more likely than not.

  1. Pr⁡(no collision)=1000⋅999⋯971100030\Pr(\text{no collision}) = \frac{1000 \cdot 999 \cdots 971}{1000^{30}}The ii-th key must avoid the i−1i - 1 buckets already used: P(1000,30)P(1000, 30) ordered ways out of 1000301000^{30}.
  2. =∏i=029(1−i1000)≈0.6445= \prod_{i=0}^{29}\big(1 - \tfrac{i}{1000}\big) \approx 0.6445The same product, one factor per key.
  3. 1−0.6445≈0.35551 - 0.6445 \approx 0.3555At least one collision, by the complement.
  4. (302)=435\binom{30}{2} = 435 pairs, each colliding with probability 11000\frac{1}{1000}So the approximation is 1−e−0.435≈0.35271 - e^{-0.435} \approx 0.3527, and the sum of the pair probabilities is 0.4350.435.
  5. Pr⁡(collision)≈0.356\Pr(\text{collision}) \approx 0.356; the approximation gives 0.3530.353 and the pair sum 0.4350.435. A collision becomes more likely than not at 3838 keysAt 3737 keys the probability is 0.49050.4905, at 3838 it is 0.50930.5093; the rule of thumb 1.18N≈37.31.18\sqrt N \approx 37.3 lands between. The pair sum overcounts the outcomes where several pairs collide, so it is only an upper bound.

Problem 9

Five labeled test images are shuffled at random, so each permutation of the labels is equally likely. Find the number of permutations in which no image keeps its own label, and the probability of that.

  1. AiA_i: image ii keeps its label, ∣Ai∣=4!=24\lvert A_i\rvert = 4! = 24The other four labels can be in any order.
  2. ∣Ai∩Aj∣=3!=6\lvert A_i \cap A_j\rvert = 3! = 6, triples 2!=22! = 2, quadruples 1!=11! = 1, all five 0!=10! = 1Fixing more images leaves fewer labels to arrange.
  3. ∣⋃Ai∣=5⋅24−10⋅6+10⋅2−5⋅1+1\lvert\bigcup A_i\rvert = 5 \cdot 24 - 10 \cdot 6 + 10 \cdot 2 - 5 \cdot 1 + 1Inclusion–exclusion, with (5j)\binom5j ways to pick which jj images are fixed.
  4. =120−60+20−5+1=76= 120 - 60 + 20 - 5 + 1 = 76Permutations with at least one image keeping its label.
  5. 120−76=44120 - 76 = 44 derangements, with probability 44/120=11/30≈0.36744/120 = 11/30 \approx 0.367Close to 1/e≈0.3681/e \approx 0.368, the limit as the number of images grows. Stopping after the first term gives 120120 permutations with a fixed point and a probability of 00 (Mistake 5).

Problem 10

A test set has 88 positive and 1212 negative examples. A batch of 55 is drawn without replacement. Find the probability that exactly 22 positives are drawn, and use Vandermonde's identity to check that the probabilities for 00 to 55 positives add up to 11.

  1. (205)=15504\binom{20}{5} = 15504 batches, all equally likelyAny 55 of the 2020 examples.
  2. (82)=28\binom82 = 28 ways to pick the positives, (123)=220\binom{12}{3} = 220 the negativesExactly two of one kind and three of the other.
  3. 28⋅220=616028 \cdot 220 = 6160The multiplication principle.
  4. ∑k=05(8k)(125−k)=(205)\sum_{k=0}^{5}\binom8k\binom{12}{5 - k} = \binom{20}{5}Vandermonde: a batch of 55 from 2020 has some number kk of positives, so counting by kk counts every batch once.
  5. Pr⁡(2 positives)=6160/15504≈0.397\Pr(2 \text{ positives}) = 6160/15504 \approx 0.397, and the six probabilities add up to 15504/15504=115504/15504 = 1This is the hypergeometric distribution. Drawing with replacement would give the binomial (52)(0.4)2(0.6)3≈0.346\binom52(0.4)^2(0.6)^3 \approx 0.346 instead.

Where this goes wrong

1. Counting arrangements when order does not matter

The feature sets of Problem 2 are counted by choosing the features one at a time.

  1. 1010 choices for the first feature, 99 for the second, 88 for the thirdRight so far: Problem 2.
  2. “So there are 10⋅9⋅810 \cdot 9 \cdot 8 feature sets.”That counts ordered triples. The set {a,b,c}\{a, b, c\} is counted as abcabc, acbacb, bacbac, bcabca, cabcab and cbacba.
  3. 720720 feature setsThere are 720/3!=120720/3! = 120. Ask whether swapping two of the chosen items gives a different outcome: for a ranking it does, for a set it does not.

2. Dropping the power on the coefficient inside a binomial term

The term of (2x−3)5(2x - 3)^5 with x2x^2 is written down from the pattern (5k)xk\binom5k x^k.

  1. (52)(2x)2(−3)3\binom52(2x)^2(-3)^3Right so far: Problem 5.
  2. “(2x)2(2x)^2 contributes 2x22x^2.”The square applies to the whole term 2x2x, so it is 4x24x^2. The same goes for the sign: (−3)3(-3)^3 is −27-27, not 2727.
  3. The coefficient is 10⋅2⋅(−27)=−54010 \cdot 2 \cdot (-27) = -540It is 10⋅4⋅(−27)=−108010 \cdot 4 \cdot (-27) = -1080. A quick check is x=1x = 1: the coefficients must sum to (2−3)5=−1(2 - 3)^5 = -1, and that only works out with every power applied to the whole term.

3. Choosing one item first and then the rest

To make sure the benchmark suite of Problem 4 has a text task, one is picked first.

  1. 55 ways to pick a text taskRight so far.
  2. “Then any 33 of the other 1010 tasks: (103)=120\binom{10}{3} = 120.”A suite with two text tasks is now counted twice, once for each of them as "the" first pick, and a suite with three of them three times.
  3. 5⋅120=6005 \cdot 120 = 600 suites with at least one text taskThere are only 330330 suites in all. The right count is 330−(64)=315330 - \binom64 = 315, by the complement.

4. One bar too many in stars and bars

Ten identical GPU nodes go to four jobs, and each job seems to need a bar.

  1. 1010 stars, one for each nodeRight so far: Problem 7.
  2. “44 jobs, so 44 bars, and 1414 symbols in a row.”Bars separate the jobs. Four jobs need three bars; a fourth bar makes a fifth box.
  3. (144)=1001\binom{14}{4} = 1001 allocationsThat counts the ways to share ten nodes among five jobs. With 33 bars it is (133)=286\binom{13}{3} = 286. Check on a small case: 22 nodes for 22 jobs gives (2,0)(2,0), (1,1)(1,1), (0,2)(0,2), which is (31)=3\binom31 = 3, not (42)=6\binom42 = 6.

5. Adding overlapping cases instead of using inclusion–exclusion

The shuffled labels of Problem 9 are counted by adding up the ways each image can keep its label.

  1. ∣Ai∣=4!=24\lvert A_i\rvert = 4! = 24 for each of the 55 imagesRight so far: Problem 9.
  2. “At least one image keeps its label in 5⋅24=1205 \cdot 24 = 120 permutations.”A permutation that fixes two images is in two of the sets and was counted twice. The identity permutation, which fixes all five, was counted five times.
  3. No permutation leaves every image mislabeled, since 120−120=0120 - 120 = 0The overlaps must be subtracted and added back: 120−60+20−5+1=76120 - 60 + 20 - 5 + 1 = 76, which leaves 4444 derangements, and a probability of 11/3011/30, not 00.

Print this set: counting-and-the-binomial-theorem.pdf (problems, answers, and worked solutions on separate pages).

Read the guide: Counting and the binomial theorem: multiply the choices, divide out the orders that don't matter, and expand with C(n, k)