1. Once again, consider the experiment of rolling two fair 6-sided dice. Let
be the max of the two rolls.
A. Write R code to:
· Graph the analytic pmf
· Graph the analytic CDF
B. Simulate 10,000 realizations of
. Then:
· Plot the simulated pmf
· Plot the simulated CDF
Find the simulated mean, variance, and median and verify that they well-approximate the analytic values.
#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
Attaching package: 'purrrfect'
The following objects are masked from 'package:base':
replicate, tabulate
pmf <- tibble(m = 1:6, p = (2*m - 1)/36, cdf = m^2/36)
ggplot(pmf, aes(m, p)) + geom_col()
ggplot(pmf, aes(m, cdf)) + geom_step()
*Part B
one_omega <- \() max(sample(1:6, 2, replace = TRUE))
(many_omegas <- replicate(10000, one_omega(), .as = m) %>% mutate(m = map_dbl(m, 1)))
# A tibble: 10,000 × 2
.trial m
<dbl> <dbl>
1 1 4
2 2 4
3 3 6
4 4 5
5 5 6
6 6 3
7 7 5
8 8 3
9 9 4
10 10 6
# ℹ 9,990 more rows
many_omegas %>% count(m) %>% mutate(p = n / sum(n)) %>% ggplot(aes(m, p)) + geom_col()
many_omegas %>% ggplot(aes(m)) + stat_ecdf(geom = 'step')
(many_omegas
%>% summarize(mean = mean(m), variance = var(m), median = median(m))
)
# A tibble: 1 × 3
mean variance median
<dbl> <dbl> <dbl>
1 4.48 1.94 5