Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alejandrohagan/ebbcbf7010e6ff02224f20a58765991d to your computer and use it in GitHub Desktop.
Save alejandrohagan/ebbcbf7010e6ff02224f20a58765991d to your computer and use it in GitHub Desktop.
@littmath August 24 Riddle
##You have 10000 coins. 9999 of them are fair; one is rigged so that it always lands on heads.
##You choose a coin at random and flip it 10 times; it’s heads all ten times.
## what is likelihood that the coin is rigged?
ht_list <- rep(list(c("H", "T")), 9999)
ht_list[[10000]] <- c("H", "H")
coins <- tibble(
x=ht_list
) |>
mutate(
coin_id=row_number()
,coin_indicator=if_else(coin_id==10000,1,0)
)
flip_coin <- function(){
coins |>
rowwise() |>
mutate(
ten_flips=list(sample(x,size=10,replace=TRUE))
,ten_flips=list(str_flatten(ten_flips))
) |>
unnest(ten_flips) |>
filter(
ten_flips=="HHHHHHHHHH"
) |>
summarise(
mean=mean(coin_indicator)
)
}
results_tbl <- tibble(results=replicate(1000,expr = flip_coin()))
mean_vec <- results_tbl |> unnest(results) |> pull(results) |> mean()
results_tbl |> unnest(results) |>
ggplot(aes(x=results))+
geom_histogram()+
geom_vline(xintercept = mean_vec,col="midnightblue",size=1)+
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