Topic6 Counting SimulatingChance
Topic6 Counting SimulatingChance
Understanding probability
What is probability?
Chance variability
How can we model chance variability by a box model?
2/35
Simulating Chance
“Classical” probability
Factorial
Combinations
Summary
3/35
“Classical” probability
The sample space Ω consist of a finite, known number of equally likely outcomes (e.g.,
coins, dice, cards). The probability of an event 𝐴 ⊂ Ω occurring is
For example, suppose we want to know the probability of getting an even number when
we roll a fair die. There are 6 equally likely possible outcomes,
Ω = {⚀⚁⚂⚃⚄⚅}
of which 3 are even. Therefore, the probability of rolling an even number is
4/35
Enumeration
Counting and drawing trees
· For simple problems, a good start is to enumerate all the possible outcomes using:
· Method 1
- Write a list of all outcomes
- Count which outcomes belong to the event of interest.
· Method 2
- Draw a tree
6/35
Example
Two dice are thrown. What is the chance that their sum is 6?
Method 1: Write a full list of outcomes and count the outcomes of interest.
7/35
Method 2: Summarise in a tree diagram
Start
1 2 3 4 5 6
1 2 3 4 5 6
· One path has probability 1/36, because independence of 1st draw and 2nd draw
means probabilities of edges can be multiplied: 1/6*1/6=1/36
· Probability of paths can be added because events are mutually exclusive (if you throw
(1,5) you can’t have thrown (2,4) at the same time)
· Possible paths are (1,5), (2,4), (3,3), (4,2), (5,1), hence probability of 6 spots is
1/36+1/36+1/36+1/36+1/36=5/36.
8/35
Sample with/without replacement
Method 3: Simulate (in R)
· Use R and simulate throwing 2 dice 𝑥 times and record the findings.
10/35
Method 3: Simulate (in R)
barplot(table(totals), main = "1000 rolls: sum of 2 dice")
So the (simulated) chance of getting a total of 6 is 144/1000 = 0.144, which is very close
to the exact answer of 5/36 = 0.139.
11/35
· What will happen if sample without replacement (without rep=T )?
12/35
A simple box model
Many counting and probability problems can be reduced to a box model. In a box
model, there are 𝑁 tickets in a box, and we draw 𝑚 tickets from the box.
· For example, three rolls of a fair die can be modeled as 𝑚 = 3 draws from the box
1 2 3 4 5 6
we have to place the ticket back in the box after each draw, so the outcome of one die
roll does not affect the outcome of another. In other words, the 𝑚 = 3 draws are
made with replacement.
· In other situations, the draws are made without replacement. For example, consider
drawing four cards from a standard deck of 52 cards (without putting the drawn cards
back).
13/35
Example (a bit more complex)
Three dice are thrown. What is the chance of getting a total equal to 6?
14/35
Method 2: Summarise in a tree diagram
Start
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
15/35
Method 3: Simulate in R
set.seed(23)
totals = sample(1:6, 1000, rep = T) + sample(1:6, 1000, rep = T) + sample(1:6, 1000,
rep = T)
table(totals)/1000
## totals
## 3 4 5 6 7 8 9 10 11 12 13 14 15
## 0.007 0.011 0.025 0.048 0.066 0.105 0.112 0.135 0.134 0.113 0.094 0.065 0.041
## 16 17 18
## 0.029 0.013 0.002
barplot(table(totals), main = "1000 rolls: sum of 3 dice")
16/35
Example
Why did the Chevalier lose money? What is the chance of winning?
17/35
Method 3: Simulate in R (using a function)
gameA <- function() {
rolls <- sample(1:6, size = 4, replace = TRUE)
condition <- sum(rolls == 1) > 0
return(condition)
}
simsA <- replicate(1e+05, gameA())
sum(simsA)/length(simsA)
## [1] 0.51543
gameB <- function() {
first.die <- sample(1:6, size = 24, replace = TRUE)
second.die <- sample(1:6, size = 24, replace = TRUE)
condition <- sum((first.die == second.die) & (first.die == 1)) > 0
return(condition)
}
simsB <- replicate(1e+05, gameB())
sum(simsB)/length(simsB)
## [1] 0.48979
Indeed, Game A is better.
18/35
Sample without replacement (using R)
Example
· A company has 10,000 male employees and 11,000 female employess. A
representative committee is created by randomly picking 10 employees.
· What is chance that more than 75% in the committee are male?
set.seed(1)
committee <- function() {
committee <- sample(c(rep(1, 10000), rep(0, 11000)), size = 10, replace = FALSE)
condition <- mean(committee) > 0.75
return(condition)
}
sim <- replicate(10000, committee())
mean(sim)
## [1] 0.0418
19/35
Multiplication Principle of Counting
Multiplication Principle of Counting
Multiplication Principle of Counting
If a task can be performed in 𝑛1 ways, and for each of these ways, a second task
can be performed in 𝑛2 ways, then the two tasks can be performed together in a
total of 𝑛1 × 𝑛2 ways. If there are 𝑘 tasks in such a sequence, then 𝑘 tasks can be
performed together in a total of 𝑛1 × 𝑛2 × ⋯ 𝑛𝑘 ways.
For example, there are a total number 6 × 6 × 6 = 216 outcomes in rolling three dice.
21/35
Factorial
How many ways to arrange a deck of 52 cards?
We can use the multiplication principle to determine the number of ways to arrange the
deck.
23/35
Factorial
Factorial
The quantity 𝑛! (pronounced: “n factorial”) is defined as
𝑛! = 𝑛 × (𝑛 − 1) × ⋯ × 1.
It represents the number of ways to arrange n objects.
24/35
Example
A deck of 52 cards is shuffled thoroughly. What is the probability that the four aces
are all next to each other? (Hint: First, count the number of positions that the block
of four aces can go, then multiply this by the number of ways of ordering the four
aces.)
There are 52! ways to order a deck of 52 cards (total number of outcomes in Ω )
How many possible ways to have four aces next to each other?
Note: A deck of cards has 13 ranks (ace, king, queen, jack, 10, …, 2) and 4 suits
(spades, clubs, hearts, and diamonds).
25/35
· Step 1: Consider the block of four aces (next to each other) as a single block
- then we have 48 other cards plus this one block, making a total of 48 + 1 = 49
units to arrange.
· Step 2: there are 49! ways to arrange these 49 units.
· Step 3: Calculate the number of ways to arrange the aces (with different suits) within
the block.
26/35
Example
If a five-letter word (in English) is formed at random (meaning that all sequences of
five letters are equally likely), what is the probability that no letter occurs more than
once?
5
Total number of outcomes in Ω : 26
So the probability is
26 ⋅ 25 ⋅ 24 ⋅ 23 ⋅ 22
𝑃 = 5
≈ 0.6588
26
27/35
Combinations
Example
One of the most coveted hands in poker is a four-of-a-kind, which is when the hand
contains all four cards of a particular rank. For example, the hand below is an example
of a four-of-a-kind, since it contains all four 7s in the deck. (The last card, called the
“kicker”, can be any other card.)
Note: the order of the cards in the hand does not matter.
29/35
If drawing without replacement, the number of ways to draw 𝑘 tickets from the box
1 2 3 ⋯ N
is
𝑁!
𝑁 × (𝑁 − 1) × ⋯ × (𝑁 − 𝑘 + 1) =
(𝑁 − 𝑘)!
𝑘 terms
since the number of tickets remaining in the box decreases by 1 on each draw.
1 2 3 ⋯ 52
52!
Number of possible ordered poker hands: (52−5)!
= 52 ⋅ 51 ⋅ 50 ⋅ 49 ⋅ 48
> 300 × 106 .
30/35
Note that the order of cards in a hand matters here. We count not only how many hands
of cards, but also how many ordered hands.
Two hands formed by the same set of cards, but with different orders, for example
The factorial considers the different orders in which the cards might be drawn.
31/35
How many of these possible ordered outcomes result in a four-of-a-kind?
Let’s start by assuming that the first four cards in the hand are the four-of-a-kind and the
last card is the kicker.
We assume the kicker is the last card in the hand. But the kicker can be in any one of 5
positions. So we need to multiply everything by 5 in the end.
32/35
Combinations
The previous calculation was complicated because we had to consider the different
orders in which the cards might be drawn. It is often easier to ignore the order when
counting outcomes.
Combinations (order doesn’t matter)
The number of ways to draw 𝑘 tickets from the box
1 2 3 ⋯ N
( 𝑘 ) 𝑘!(𝑁 − 𝑘)!
𝑁 𝑁!
=
𝑘! is the number of ways of ordering the same set of 𝑘 objects.
33/35
Revisit the probability of a four-of-a-kind using combinations.
(5)
52
If we ignore the order of the cards in the hand, there are = 2, 598, 960 possible
poker hands.
choose(52, 5)
## [1] 2598960
34/35
Summary
“Classical” probability
Factorial
Combinations
Key R Functions
set.seed , sample , replicate , choose
35/35