2.1 Simulation Study

Simulate the last warmup problem. A professor has written 6 possible exam questions, of which a student has thoroughly studied 4. Four questions are selected at random for the exam. What is the probability the student can solve all four of the problems on the exam?

# message: FALSE
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(purrrfect)

Attaching package: 'purrrfect'

The following objects are masked from 'package:base':

    replicate, tabulate

Problem 8:

p_heads = 0.4
n_flips = 8

one_omega = \() sample(c("H", "T"), n_flips, replace = TRUE, prob = c(p_heads, 1 - p_heads))

N = 10000

sim8 = tibble(omega = base::replicate(N, one_omega(), simplify = FALSE)) %>%
  mutate(
    n_heads = map_int(omega, \(flips) sum(flips == "H")),
    last_two_heads = map_lgl(omega, \(flips) all(tail(flips, 2) == "H"))
  )

sim8 %>%
  summarise(prob_at_least_half = mean(n_heads >= 4))
# A tibble: 1 × 1
  prob_at_least_half
               <dbl>
1              0.406
sim8 %>%
  summarise(prob_last_two_heads = mean(last_two_heads))
# A tibble: 1 × 1
  prob_last_two_heads
                <dbl>
1               0.158

Problem 9:

studied = rep(c(TRUE, FALSE), c(4, 2))

one_omega = \() sample(studied, 4, replace = FALSE)
studiedcount = \(omega) sum(omega)

N = 100000

sim9 = tibble(omega = base::replicate(N, one_omega(), simplify = FALSE)) %>%
  mutate(ycount = map_int(omega, studiedcount))

sim9 %>%
  summarise(prop_all_studied = mean(ycount == 4))
# A tibble: 1 × 1
  prop_all_studied
             <dbl>
1           0.0659