2.1 #9

# 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
sample(1:6, 4, replace = FALSE)
[1] 3 5 2 1
question_pool <- rep(c(1,0),c(4,2))

sample(question_pool, 4, replace = FALSE)
[1] 1 1 0 1
one_omega <- \() sample(question_pool, 4, replace = FALSE)
sum(one_omega() == 'y')
[1] 0
N <- 1000000

count_yes <-\(x) sum(x == 'y')

(many_omegas <-replicate(N,one_omega(), .as = tests)
  %>% mutate(y_count = map_int(tests,sum))
) %>% head()
# A tibble: 6 × 3
  .trial tests     y_count
   <dbl> <list>      <int>
1      1 <dbl [4]>       3
2      2 <dbl [4]>       3
3      3 <dbl [4]>       3
4      4 <dbl [4]>       3
5      5 <dbl [4]>       2
6      6 <dbl [4]>       2
(many_omegas
  %>% summarize('P(knows all 4)' = mean(y_count==4))
) %>% head()
# A tibble: 1 × 1
  `P(knows all 4)`
             <dbl>
1           0.0666