The multiplication rule
When a choice is made in steps, multiply the number of ways at each step. A café offers 3 drinks and 4 cakes. Each drink goes with each cake, so there are orders.
Lesson 5.1.1's two dice work the same way: 6 faces for the red die, then 6 for the blue, outcomes. A 4-digit PIN has possibilities.
Rule: Multiplication rule
A first step can go ways. If a second step can then go ways, whatever the first was, the two together can go ways. More steps multiply on.
The options at the second step may depend on the first, but their number must not. Four runners race for gold and silver. Whoever wins gold, 3 runners are left for silver, so there are results.
Try it
product lists every drink with every cake, and len counts the list. The PIN count is .
permutations("ABCD", 2) lists the ordered pairs of different runners: gold first, then silver.
Order matters: arrangements
Put 4 books on a shelf. The first place has 4 choices, the second 3, the third 2 and the last 1: orders. This product is written , read "4 factorial". In general counts the orders of different things.
Factorials grow fast. , and a deck of 52 cards can be shuffled into orders. By agreement : there is one way to arrange nothing.
Often only some of the things are placed. A club of 10 chooses a chair, a secretary and a treasurer, three different people: ways. That is with the last cancelled, .
Rule: Ordered choices
The number of ordered lists of things from , each used at most once: a permutation of from , math.perm(n, k). If a thing may be used again, as a digit in a PIN, each place has all choices.
From the 5 letters A to E, 3-letter codes number when letters may repeat, and when they may not. Repeats allowed is also called drawing with replacement: the letter goes back before the next draw.
Try it
math.factorial and math.perm give the counts above. Listing all the codes with product and permutations confirms 125 and 60.
Order does not matter: combinations
Now the club picks a committee of 3, with no roles. The 720 ordered choices count each committee several times: once for each way to hand out the three roles, ways. So there are committees.
The picture shows the same idea with 4 runners and 2 medals. There are 12 orders, and each pair of medal winners appears twice.
Gold has 4 ways and silver 3, so orders. As pairs, comes from "A then B" and from "B then A", so there are pairs.
Rule: Combinations
The number of ways to choose things from when order does not matter: the subsets of size . Read " choose ", also written ; math.comb(n, k). It is the ordered count divided by the orders of each choice.
Four cases are worth knowing.
- Nothing or everything. : one way to choose no one, one way to choose everyone.
- Choosing who stays out. . Picking 3 of 10 to serve is picking 7 to stay home: both give 120.
- Too many. If there is no way, and .
- All sizes together. For , the counts for are 1, 5, 10, 10, 5, 1. They add up to , every subset, since each of the 5 things is either in or out.
Lesson 5.2.2 uses these numbers for the binomial distribution.
Try it
combinations("ABCDE", 2) lists the 10 pairs, each once. Then the committee count two ways, and the row for with its total.
math.comb(4, 5) is 0: five things cannot be chosen from four.
Probabilities by counting
With equally likely outcomes, (lesson 5.1.1), and both counts come from the rules above. Count the event and the sample space the same way: both ordered, or both unordered.
Six rolls of a die. The ordered sequences of faces are equally likely. Of them, contain no six. So , not the that adding gives.
Cards. A standard deck has 52 cards: 13 ranks (2 to 10, jack, queen, king, ace) in each of 4 suits (hearts, diamonds, clubs, spades). A 5-card hand from a well-shuffled deck is one of equally likely hands.
- All hearts: hands, so . Five cards of any one suit: hands, .
- Exactly two aces: choose the 2 aces from 4, ways, then the other 3 cards from the 48 that are not aces, . The multiplication rule gives hands, .
- At least one ace: the complement, no ace, has hands, so .
A committee. A club of 10 has 6 juniors and 4 seniors, and a committee of 3 is picked at random from the 120. All juniors: committees, so . Exactly two juniors: , so .
Counting in order gives the same answer if the top is ordered too. Three juniors one after another: .
Try it
The cell works out each probability above. Fraction gives the committee answers exactly.
A lottery
A lottery draws 6 different numbers from 1 to 49, and a ticket picks 6. There are equally likely draws, so the jackpot has probability about .
To match exactly 3 of your numbers, the draw takes 3 of your 6, in ways. Its other 3 come from the 43 numbers you did not pick, in ways. That is draws, a probability of about 0.01765. Every number of matches works the same way:
| matches | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|
| draws | 6,096,454 | 5,775,588 | 1,851,150 | 246,820 | 13,545 | 258 | 1 |
| probability | 0.436 | 0.413 | 0.1324 | 0.01765 | 0.0009686 |
The draws add up to all 13,983,816, so the probabilities add up to 1. At least 3 matches has probability about 0.01864.
The same pattern answers every question of this kind. Draw things from , of which are special. Exactly special ones turn up in of the equally likely draws.
For two aces in a hand, , , and .
Try it
The loop builds the table and checks the total. The rest simulates, with fixed seeds: 100,000 shuffled decks, keeping the first 5 cards, and 100,000 lottery draws.
rng.random((n, 52)).argsort(axis=1) puts the 52 cards in a random order in each row. Cards 0 to 3 stand for the aces, and numbers 0 to 5 for your ticket.
In this seeded run, 0.0389 of the hands had exactly two aces, against the exact 0.0399, and 0.3402 had at least one, against 0.3412. Case study 4.7.5's standard error, , is 0.0006 here: the size of a typical miss. The lottery run gives 0.0181 for exactly 3 matches, against 0.01765.
Which count to use
Two questions decide: does the order matter, and can a thing be chosen twice? Swap two of your choices: if the result changes, order matters.
| example | order matters? | repeats? | count |
|---|---|---|---|
| a PIN, dice rolled in turn | yes | yes | |
| medals, officers, a shelf | yes | no | |
| a committee, a card hand, a lottery draw | no | no |
An urn holds 3 red and 2 blue balls; draw 2. Without replacement, the 2 balls form one of pairs, and are both red: . With replacement, the 25 ordered draws are equally likely and 9 are red twice: .
Lesson 5.1.3 gets both answers from conditional probabilities.
Common mistakes
- Order or not. A committee of 3 from 10 has 120 choices; three officers have 720. Ask whether swapping two choices gives a different result.
- Count the top and the bottom the same way. An ordered count of the event over an unordered count of the sample space is wrong by a factor .
- "Exactly" means the rest are not of that kind. For exactly two aces the other 3 cards come from the 48 non-aces. Taking them from the 50 cards left gives 0.0452, counting hands with 3 or 4 aces.
- "At least one" by adding counts twice. Two-card hands with an ace number , not , which counts the 6 hands with two aces twice.
- Unordered choices with repeats are not equally likely. Like the sums of two dice, count the ordered draws instead.
- Big factorials in NumPy overflow. Python's integers are exact, so
math.factorial(21)is right. NumPy'snp.prod(np.arange(1, 22, dtype=np.int64))silently gives a negative number, because its 64-bit integers stop near .
Try it
The first line is mistake 3: the right count of two-ace hands, then the wrong one with the other cards from 50. The second is mistake 4.
The last line compares from math with NumPy's product, and prints the largest NumPy integer.
Counting
0 of 12 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Summary card
- Multiplication rule: steps with and ways give results.
- Ordered, no repeats: (
math.perm); with repeats: . , and . - Unordered: (
math.comb), the ordered count divided by . . - Probability by counting: count the event and the sample space the same way. "Exactly special" is over .
- Use the complement for "at least one", and check counts by listing small cases or by a seeded simulation.