Created
January 27, 2020 23:00
-
-
Save benjamin-chan/b5071510711c9037263b855e85133011 to your computer and use it in GitHub Desktop.
Simulate the Birthday Problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ref: https://en.wikipedia.org/wiki/Birthday_problem | |
# Ref: https://www.scientificamerican.com/article/bring-science-home-probability-birthday-paradox/ | |
library(magrittr) | |
library(dplyr) | |
people <- 23 | |
simulations <- 1e5 | |
expand.grid(id = 1:people, | |
sim = 1:simulations) %>% | |
mutate(bday = sample(365, simulations * people, replace = TRUE)) %>% | |
group_by(sim) %>% | |
summarize(anyDupes = anyDuplicated(bday) > 1) %>% | |
group_by(anyDupes) %>% | |
summarize(freq = n()) %>% | |
mutate(pct = freq / sum(freq)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment