ปัญหาวันเกิด

Probability Puzzles in R

Peter Chi

Assistant Professor of Statistics Villanova University

ภาพรวมของปัญหา

การตั้งค่า

  • ห้องที่มีคน n คน
  • ความน่าจะเป็นที่มีคนเกิดวันเดียวกันคือเท่าใด?

สมมติฐาน

  • ไม่นับวันที่ 29 กุมภาพันธ์
  • วันเกิดกระจายสม่ำเสมอใน 365 วันที่เหลือ
  • แต่ละคนในห้องเป็นอิสระจากกัน
Probability Puzzles in R

การกำหนด counter

การจำลองความน่าจะเป็นของการทอยได้ 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

การเพิ่มค่า counter ในลูป

การจำลองความน่าจะเป็นของการทอยได้ 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

ฟังก์ชัน pbirthday

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

การพล็อตกราฟ

plot(match_probs ~ room_size)

image-url

Probability Puzzles in R

มาฝึกกันเถอะ!

Probability Puzzles in R

Preparing Video For Download...