Skip to content

Instantly share code, notes, and snippets.

@alejandrohagan
Last active September 6, 2024 18:32
Show Gist options
  • Save alejandrohagan/6b00bfb531c0c1e33f4bc8675815bcde to your computer and use it in GitHub Desktop.
Save alejandrohagan/6b00bfb531c0c1e33f4bc8675815bcde to your computer and use it in GitHub Desktop.
# https://gilkalai.wordpress.com/2009/07/08/test-your-intuition-7/
#Consider the following game: you have a box that contains one white ball and one black ball.
#You choose a ball at random and then return it to the box.
#If you chose a white ball then a white ball is added to the box, and if you chose a black ball then a black ball is added to the box.
#Played over time, what is the probability that more than 80 percents of the chosen balls are white?
library(tidyverse)
## make simulation functions -- I couldhave done it one go but just made it more modular
pick_ball <- function(n){
res <- tibble(x=sample(c(1,0),n,replace=TRUE)) |> mean()
}
stimulate_pick_balls <- function(n){
res <- tibble(x=replicate(100000,pick_ball(n)))
prop_tbl <- res |> table() |> prop.table()
value_vec <- prop_tbl["0.8"] |> unname()
out <- tibble(x=value_vec,y=n)
return(out)
}
## sequence values to vary the "over time" piece
n_vec <- seq(10,50,5)
## do iterations
res <- map(
n_vec
,.f=\(x) stimulate_pick_balls(x)
)
## plot resutls
res |>
purrr::list_rbind() |>
ggplot(
aes(x=x,y=y)
)+
geom_point()+
geom_line()+
scale_y_reverse()+
scale_x_continuous(labels = scales::label_percent())+
theme_classic()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment