# Start of script ---- # Draw 5 cards from a deck of 52 cards and estimate the probability that # you observe all Red cards in a hand of 5 cards. # Using code from the Tidyverse in R. ---- library(tidyverse) # Draw 5 cards from a deck of 52 cards ---- cards <- tibble(card = c("RH2", "RH3", "RH4", "RH5", "RH6", "RH7", "RH8", "RH9", "RH10", "RHJ", "RHQ", "RHK", "RHA", "RD2", "RD3", "RD4", "RD5", "RD6", "RD7", "RD8", "RD9", "RD10", "RDJ", "RDQ", "RDK", "RDA", "BS2", "BS3", "BS4", "BS5", "BS6", "BS7", "BS8", "BS9", "BS10", "BSJ", "BSQ", "BSK", "BSA", "BC2", "BC3", "BC4", "BC5", "BC6", "BC7", "BC8", "BC9", "BC10", "BCJ", "BCQ", "BCK", "BCA")) cards_draw <- cards |> sample_n(5) cards_draw # Estimate the probability that you observe all Red cards in a hand of 5 cards ----- # Using a regex function from the Tidyverse R package stringr cards_draw |> filter(str_detect(card, "R")) cards_draw |> filter(str_detect(card, "R")) |> summarise(rows = n()) == 5 event <- replicate(10000, { cards_draw <- cards |> sample_n(5) cards_draw |> filter(str_detect(card, "R")) |> summarise(rows = n()) == 5 }) mean(event) # Answer ---- choose(26,5)/choose(52,5) # Using a process bar library(pbapply) # R package for progress bars on replicate event <- pbreplicate(10000, { cards_draw <- cards |> sample_n(5) cards_draw |> filter(str_detect(card, "R")) |> summarise(rows = n()) == 5 }) mean(event) # Answer ---- choose(26,5)/choose(52,5) # Use parallel processing in R. ---- library(future.apply) # R package with a parallel replicate function library(tictoc) # R package for timing code execution of code # Compare single core speed with the above code. tic() plan(sequential) event <- future_replicate(10000, { cards_draw <- cards |> sample_n(5) cards_draw |> filter(str_detect(card, "R")) |> summarise(rows = n()) == 5 }) toc() mean(event) # Answer ---- choose(26,5)/choose(52,5) # Compare multi-core speed with the above code. tic() plan(multisession) event <- future_replicate(50000, { cards_draw <- cards |> sample_n(5) cards_draw |> filter(str_detect(card, "R")) |> summarise(rows = n()) == 5 }) toc() mean(event) # Answer ---- choose(26,5)/choose(52,5) # End of script ----