Monte Hall

reference:
- Monty Hall problem

simulation: car or goat

pick <- function(n){
  sample(c("car", "goat", "goat"), n, replace = TRUE)
}
pick(10)
##  [1] "car"  "car"  "car"  "goat" "car"  "car"  "goat" "goat" "car"  "goat"
table(pick(1000))
## 
##  car goat 
##  318  682

simulation: 1 or 0

pick.1.0 <- function(n){
  sample(c(1, 0, 0), n, replace = TRUE)
}
pick.1.0(10)
##  [1] 0 0 0 0 1 0 0 1 0 0
table(pick.1.0(1000))
## 
##   0   1 
## 664 336

long-run probability

set.seed(2027)
n <- 1:1000
picks <- pick.1.0(1000)
cumsum.picks <- cumsum(picks)   # cummulative sum of picks
df <- data.frame(n, picks, cumsum.picks, p = cumsum.picks / n)
str(df)
## 'data.frame':    1000 obs. of  4 variables:
##  $ n           : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ picks       : num  1 0 1 0 1 1 0 0 0 0 ...
##  $ cumsum.picks: num  1 1 2 2 3 4 4 4 4 4 ...
##  $ p           : num  1 0.5 0.667 0.5 0.6 ...
ggplot(df, aes(n, p)) +
  geom_line(color = "skyblue") +
  ylim(0, 1) +
  geom_hline(yintercept = 1 / 3, color = "orange", lty = 2) +
  labs(title = "Long-run Probability")