Packages
Warning: package 'ggplot2' was built under R version 4.5.2
Warning: package 'tibble' was built under R version 4.5.3
Warning: package 'tidyr' was built under R version 4.5.3
Warning: package 'dplyr' was built under R version 4.5.3
Warning: package 'stringr' was built under R version 4.5.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.1 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.6.0
✔ ggplot2 4.0.0 ✔ tibble 3.3.1
✔ lubridate 1.9.4 ✔ tidyr 1.3.2
✔ purrr 1.1.0
── 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
Attaching package: 'purrrfect'
The following objects are masked from 'package:base':
replicate, tabulate
Problem 8
An unfair coin has a 40% chance of landing heads. The coin is flipped 8 times.
a. What is the probability that at least half of the flips are heads?
p_head <- 0.4
one_trial <- \() sample (c ('H' ,'T' ), 8 , prob = c (p_head, 1 - p_head), replace = TRUE )
over_half <- \(x) sum (x == 'H' ) >= 4
(
replicate (N, one_trial (), .as = flips)
|> mutate (at_least_half = map_lgl (flips, over_half))
|> summarize (prob_at_least_half = mean (at_least_half))
)
# A tibble: 1 × 1
prob_at_least_half
<dbl>
1 0.403
What is the probability the last two flips are both heads?
last_two <- \(x) x[7 ] == 'H' & x[8 ] == 'H'
(
replicate (N, one_trial (), .as = flips)
|> mutate (last_two_heads = map_lgl (flips, last_two))
|> summarize (p_last_two_heads = mean (last_two_heads))
)
# A tibble: 1 × 1
p_last_two_heads
<dbl>
1 0.156
Problem 9
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?
#Functions used
selection <- rep (c ('S' ,'NS' ), c (4 ,2 ))
count_S <- \(x) sum (x == 'S' )
one_test <- \() sample (selection, 4 , replace = FALSE )
#Replicating and getting result
(replicate (N, one_test (), .as = tests)
|> mutate (correct_q = map_int (tests, count_S))
|> mutate (all_good = correct_q == 4 )
|> summarize (percent_all = mean (all_good))
)
# A tibble: 1 × 1
percent_all
<dbl>
1 0.0618