2026-10-12
Define permutations and combinations
Calculate the probability of sampling any combination of the following: with or without replacement and order does or does not matter
Use a simulation to check a probability found by counting
Example 1
Suppose we have 10 (distinguishable) subjects for study.
How many possible ways are there to order them?
How many ways to order them if we can reuse the same subject and
need 10 total?
need 6 total?
How many ways to order them without replacement and only need 6?
How many ways to choose 6 subjects without replacement if the order doesn’t matter?
Suppose we have 10 (distinguishable) subjects for study.
Example 1.1
How many possible ways are there to order them?
Example 1.2
How many ways to order them if we can reuse the same subject and
need 10 total?
need 6 total?
Suppose we have 10 (distinguishable) subjects for study.
Example 1.3
How many ways to order them without replacement and only need 6?
Example 1.4
How many ways to choose 6 subjects without replacement if the order doesn’t matter?
Calculate the probability of sampling any combination of the following: with or without replacement and order does or does not matter
Use a simulation to check a probability found by counting
Definition: Permutations
Permutations are the number of ways to arrange in order \(r\) distinct objects when there are \(n\) total.
\[nPr = \frac{n!}{(n-r)!}\]
Definition: Combinations
Combinations are the number of ways to choose (order doesn’t matter) \(r\) objects from \(n\) without replacement.
\[nCr = \textrm{"n choose r"} = \binom{n}{r} = \frac{n!}{r!(n-r)!}\]
| Property | Proof | |
|---|---|---|
| \[\binom{n}{r} = \binom{n}{n-r}\] | \(\binom{n}{r} = \dfrac{n!}{r!(n-r)!}\) and \(\binom{n}{n-r} = \dfrac{n!}{(n-r)!(n-(n-r))!} = \dfrac{n!}{(n-r)!r!}\) | |
| \[\binom{n}{1} = n\] | \[\binom{n}{1} = \dfrac{n!}{1!(n-1)!}=\dfrac{n\cdot(n-1)\cdot(n-2)\cdots1}{1! \cdot (n-1)\cdot(n-2)\cdots1} = \dfrac{n\cdot(n-1)!}{1\cdot (n-1)!} = \dfrac{n}{1}=n\] | |
| \[\binom{n}{0} = 1\] | \[\binom{n}{0} = \dfrac{n!}{0!(n-0)!}=\dfrac{n!}{1\cdot n!} = 1\] |
\(n\) = total number of objects
\(r\) = number objects needed
| with replacement | without replacement | |
|---|---|---|
| order matters | \[n^r\] | \[n\text{P}r = \frac{n!}{(n-r)!}\] (permutations) |
| order doesn’t matter | \[ \binom{n+r-1}{r}\] | \[n\text{C}r = \binom{n}{r} = \frac{n!}{r!(n-r)!}\] (combinations) |
For example in the following example drawing 2 spades when order does not matter, we actually need to enumerate the other cards that are NOT spades. So the event is choosing 2 spades out of 13 AND choosing 0 other cards of 39 cards (13 hearts + 13 clubs + 13 diamonds).
Thus the probability is actually:
\[ P(\text{two spades}) = \dfrac{{13 \choose 2}{39 \choose 0}}{{52 \choose 2}} \]
Example 2
Suppose we draw 2 cards from a standard deck without replacement. What is the probability that both are spades when
Example 2
Suppose we draw 2 cards from a standard deck without replacement. What is the probability that both are spades when
We can do a simulation! First, a tiny version with 5 simulations:
rep() repeats each suit 13 times (each = 13), so deck holds all 52 cards.
reps, so it is easy to change later.
|> passes it to the next line.
mutate() adds a column called card. For each simulation, sample() picks 2 cards from the deck. replace = FALSE means a card cannot be picked twice.
ungroup() turns the grouping off, so later steps use the whole table.
# A tibble: 10 × 3
sim draw card
<int> <int> <chr>
1 1 1 C
2 1 2 H
3 2 1 H
4 2 2 C
5 3 1 D
6 3 2 D
7 4 1 S
8 4 2 S
9 5 1 C
10 5 2 H
Example 2
Suppose we draw 2 cards from a standard deck without replacement. What is the probability that both are spades when
Same code, now with 100,000 simulations. Then check each simulated hand:
set.seed(1234)
reps <- 100000
hands <- expand_grid(sim = 1:reps, draw = 1:2) |>
group_by(sim) |>
mutate(card = sample(deck, n(), replace = FALSE)) |>
ungroup()
spade_results <- hands |>
group_by(sim) |>
summarise(ordered = card[1] == "S" & card[2] == "S",
unordered = sum(card == "S") == 2)
spade_results |>
summarise(prob_ordered = mean(ordered),
prob_unordered = mean(unordered))TRUE if the first card is a spade and the second card is a spade. [1] and [2] pick the first and second card.
TRUE if exactly 2 of the 2 cards are spades. sum() counts the TRUEs.
mean() of a TRUE/FALSE column is the proportion of TRUEs, because TRUE counts as 1 and FALSE as 0.
# A tibble: 1 × 2
prob_ordered prob_unordered
<dbl> <dbl>
1 0.0588 0.0588
Exact answer: choose(13, 2) / choose(52, 2) = 0.0588
Define permutations and combinations
Calculate the probability of sampling any combination of the following: with or without replacement and order does or does not matter
Example 3
Suppose 8 people are in a room, and every day of the year is equally likely to be a birthday (ignore leap days). What is the probability that at least two people share a birthday?
replace = TRUE because two people can share a birthday.
# A tibble: 10 × 3
sim person birthday
<int> <int> <int>
1 1 1 145
2 1 2 246
3 1 3 284
4 1 4 90
5 1 5 75
6 1 6 293
7 1 7 164
8 1 8 266
9 2 1 124
10 2 2 60
n_distinct() counts how many different birthdays are in the room.
shared is TRUE or FALSE.
mean() gives the proportion of rooms with a shared birthday.
# A tibble: 1 × 1
prob_shared
<dbl>
1 0.0730
Exact answer: \[1 - \dfrac{365 \cdot 364 \cdots 343}{365^{23}} = 0.5073\]
Sensitivity analysis: what if there are 30 people in the room?
reps <- 100000
n_people <- 30
birthdays <- expand_grid(sim = 1:reps, person = 1:n_people) |>
mutate(birthday = sample(1:365, n(), replace = TRUE))
birthdays |>
group_by(sim) |>
summarise(n_different = n_distinct(birthday)) |>
mutate(shared = n_different < n_people) |>
summarise(prob_shared = mean(shared))# A tibble: 1 × 1
prob_shared
<dbl>
1 0.707
Define permutations and combinations
Calculate the probability of sampling any combination of the following: with or without replacement and order does or does not matter
Use a simulation to check a probability found by counting
Lesson 5 Slides