The Birthday Problem

Probability Puzzles in R

Peter Chi

Assistant Professor of Statistics Villanova University

Problem overview

Setup

  • Room with n people in it
  • What is the probability that anyone shares the same birthday?

Assumptions

  • Ignore February 29th
  • Birthdays are uniformly distributed across the remaining 365 days
  • Each individual in the room is independent
Probability Puzzles in R

Defining a counter

Simulating the probability of rolling a 12

counter <- 0

roll <- roll_dice(2) roll
12
if(roll == 12){
  counter <- counter + 1
}

counter
1

 

roll_dice <- function(k){
  all_rolls <- sample(c(1,2,3,4,5,6), 
                      k, 
                      replace = TRUE)
  final_answer <- sum(all_rolls)
  return(final_answer)
}
Probability Puzzles in R

Incrementing a counter in a loop

Simulating the probability of rolling a 12

counter <- 0

for(i in 1:10000){
  roll <- roll_dice(2)
  if(roll == 12){
    counter <- counter + 1
  }
}
p_twelve <- counter / 10000

print(p_twelve)
0.0278
Probability Puzzles in R

The pbirthday function

pbirthday(10)
0.1169482

 

room_sizes <- c(1:10)
match_probs <- sapply(room_sizes, pbirthday)
print(match_probs)
0.000000000 0.002739726 0.008204166 0.016355912 0.027135574 0.040462484 0.056235703 0.074335292 
0.094623834 0.116948178
Probability Puzzles in R

Plotting

plot(match_probs ~ room_size)

image-url

Probability Puzzles in R

Let's solve it!

Probability Puzzles in R

Preparing Video For Download...