Practice / Probability for ML
Counting: permutations, combinations and the binomial theorem
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 of distinct things in order gives a permutation; choosing them without caring about order divides that by the orders and gives a combination . The same 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 . 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 ways and, whatever it was, a second in ways, the pair can be made in ways.
- Permutations. The number of ordered arrangements of of distinct items is , and all of them can be ordered in ways, with .
- Combinations. The number of -element subsets of items, order ignored, is . Symmetry: . Pascal: .
- Multinomial coefficients. Splitting distinct items into labeled groups of sizes with can be done in ways. If groups of the same size are unlabeled, divide by the orders of those groups.
- Binomial theorem. . Setting gives , and , gives for .
- Stars and bars. The number of ways to write as an ordered sum of nonnegative integers, that is, to put identical items into distinct boxes, is : stars and bars. With every box nonempty it is .
- Complement and inclusion–exclusion. "At least one" is often easiest as all minus none. For a union, , and in general add the single sets, subtract the pairwise intersections, add the triples, and so on.
Problems
- ·
A grid search tries learning rates, batch sizes and optimizers. How many configurations does it train? Separately, an augmentation pipeline applies different transforms, chosen from , one after another, and the order matters. How many pipelines are there?
- ·
A model may keep of candidate features. How many different feature sets are possible? How many ways are there to rank the top features, first, second and third?
- ··
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?
- ··
A benchmark suite of tasks is drawn from vision tasks and 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?
- ··
Find the coefficient of in , and the sum of all the coefficients of its expansion.
- ··
A model has 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.
- ··
A cluster has identical GPU nodes to share among jobs. How many allocations are there if a job may get no nodes? How many if every job gets at least one?
- ···
Thirty keys are hashed independently and uniformly into buckets. Find the probability that at least two keys share a bucket, compare it with the approximation and with the sum of the pair probabilities, and find the smallest number of keys for which a collision is more likely than not.
- ···
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.
- ··
A test set has positive and negative examples. A batch of is drawn without replacement. Find the probability that exactly positives are drawn, and use Vandermonde's identity to check that the probabilities for to positives add up to .
Answers
- configurations and pipelines
- feature sets and rankings
- labeled splits and unlabeled ones
- suites, with probability
- The coefficient of is ; the coefficients sum to
- subsets, nonempty, and of even size
- allocations, and with every job busy
- ; the approximation gives and the pair sum . A collision becomes more likely than not at keys
- derangements, with probability
- , and the six probabilities add up to
Worked solutions
Problem 1
A grid search tries learning rates, batch sizes and optimizers. How many configurations does it train? Separately, an augmentation pipeline applies different transforms, chosen from , one after another, and the order matters. How many pipelines are there?
- The multiplication principle: every learning rate pairs with every batch size and every optimizer.
- choices for the first transformAny of the five.
- for the second, for the thirdA transform is not repeated, so each choice leaves one fewer.
- Equivalently .
- configurations and pipelinesIf the order did not matter, the orders of each set of transforms would be one choice, and there would be .
Problem 2
A model may keep of candidate features. How many different feature sets are possible? How many ways are there to rank the top features, first, second and third?
- Ordered choices: the ranking.
- Each set of features appears in rankingsThe same three features ranked in every possible order.
- Divide out the orders.
- Check with symmetry: is also .
- feature sets and rankingsA feature set is a subset, so order does not matter; a ranking is an arrangement. Using 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?
- ways to fill the first batchChoose its four examples.
- for the second, for the thirdChoose from what is left.
- The multiplication principle.
- The same count as a multinomial coefficient.
- labeled splits and unlabeled onesWithout labels, each grouping was counted once for each of the ways to number its three batches.
Problem 4
A benchmark suite of tasks is drawn from vision tasks and 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?
- suites in allAny of the tasks.
- suites with no text taskAll four tasks from the six vision tasks.
- The complement: all suites minus those with none.
- Check: Split by the number of text tasks, to .
- suites, with probability The check sums . Picking one text task first and then any three of the rest gives (Mistake 3).
Problem 5
Find the coefficient of in , and the sum of all the coefficients of its expansion.
- The binomial theorem with the two terms and , signs included.
- : The term with .
- , , The is squared along with the .
- Multiply the three factors.
- The coefficient of is ; the coefficients sum to Setting adds up every coefficient. Forgetting to square the gives (Mistake 2).
Problem 6
A model has 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.
- Count the subsets of each size and add; that is the binomial theorem at .
- Equivalently, each feature is in or out: choices, ten times.
- nonemptyRemove the empty set.
- So the even-sized and odd-sized subsets are equal in number.
- subsets, nonempty, and of even sizeEven plus odd is and even minus odd is , so each is . Directly: .
Problem 7
A cluster has identical GPU nodes to share among jobs. How many allocations are there if a job may get no nodes? How many if every job gets at least one?
- An allocation is a row of stars split by barsThe stars before the first bar go to job , between the first and second bars to job , and so on.
- positions, of which are barsFour jobs need only three dividers.
- Choose where the bars go.
- At least one each: give every job a node first, then share the other That leaves .
- allocations, and with every job busy, the same as with , . Using bars for jobs gives (Mistake 4).
Problem 8
Thirty keys are hashed independently and uniformly into buckets. Find the probability that at least two keys share a bucket, compare it with the approximation and with the sum of the pair probabilities, and find the smallest number of keys for which a collision is more likely than not.
- The -th key must avoid the buckets already used: ordered ways out of .
- The same product, one factor per key.
- At least one collision, by the complement.
- pairs, each colliding with probability So the approximation is , and the sum of the pair probabilities is .
- ; the approximation gives and the pair sum . A collision becomes more likely than not at keysAt keys the probability is , at it is ; the rule of thumb 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.
- : image keeps its label, The other four labels can be in any order.
- , triples , quadruples , all five Fixing more images leaves fewer labels to arrange.
- Inclusion–exclusion, with ways to pick which images are fixed.
- Permutations with at least one image keeping its label.
- derangements, with probability Close to , the limit as the number of images grows. Stopping after the first term gives permutations with a fixed point and a probability of (Mistake 5).
Problem 10
A test set has positive and negative examples. A batch of is drawn without replacement. Find the probability that exactly positives are drawn, and use Vandermonde's identity to check that the probabilities for to positives add up to .
- batches, all equally likelyAny of the examples.
- ways to pick the positives, the negativesExactly two of one kind and three of the other.
- The multiplication principle.
- Vandermonde: a batch of from has some number of positives, so counting by counts every batch once.
- , and the six probabilities add up to This is the hypergeometric distribution. Drawing with replacement would give the binomial 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.
- choices for the first feature, for the second, for the thirdRight so far: Problem 2.
- “So there are feature sets.”That counts ordered triples. The set is counted as , , , , and .
- feature setsThere are . 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 with is written down from the pattern .
- Right so far: Problem 5.
- “ contributes .”The square applies to the whole term , so it is . The same goes for the sign: is , not .
- The coefficient is It is . A quick check is : the coefficients must sum to , 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.
- ways to pick a text taskRight so far.
- “Then any of the other tasks: .”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.
- suites with at least one text taskThere are only suites in all. The right count is , 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.
- stars, one for each nodeRight so far: Problem 7.
- “ jobs, so bars, and symbols in a row.”Bars separate the jobs. Four jobs need three bars; a fourth bar makes a fifth box.
- allocationsThat counts the ways to share ten nodes among five jobs. With bars it is . Check on a small case: nodes for jobs gives , , , which is , not .
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.
- for each of the imagesRight so far: Problem 9.
- “At least one image keeps its label in 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.
- No permutation leaves every image mislabeled, since The overlaps must be subtracted and added back: , which leaves derangements, and a probability of , not .
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)
More at eulerpad.app. This is one of 82 free practice sets with worked solutions at eulerpad.app/practice, alongside guides to the topics behind them.
EulerPad, the app, reads your handwritten math on iPad, iPhone and Android and checks it line by line. It's free to start.
This set is also Chapter 1 of Probability & Statistics for Machine Learning, a 250-problem workbook with worked solutions: eulerpad.app/workbooks