6.1

#9

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.2     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ 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
library(purrrfect)

Attaching package: 'purrrfect'

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

    replicate, tabulate

Sim Study

lambdas <- c(0.5, 1, 1.5, 2)

sim_df <- expand.grid(lambda = lambdas) %>%
  add_trials(10000) %>%   
  mutate(
    X = rexp(n(), rate = lambda),
    Y = rexp(n(), rate = lambda),
    U = X + Y
  )
head(sim_df)
# A tibble: 6 × 5
  lambda .trial     X     Y     U
   <dbl>  <dbl> <dbl> <dbl> <dbl>
1    0.5      1 1.85  5.32   7.18
2    0.5      2 2.04  0.700  2.74
3    0.5      3 0.956 0.536  1.49
4    0.5      4 4.61  2.95   7.55
5    0.5      5 4.31  0.701  5.01
6    0.5      6 1.22  1.26   2.49

Plotting

ggplot(data = sim_df %>% filter(lambda == 0.5)) +
  geom_histogram(aes(x = U, y = after_stat(density)),
                 fill = "cornflowerblue", color = "black",
                 binwidth = 0.12, boundary = 0) +
  geom_function(fun = \(u) dgamma(u, shape = 2, rate = 0.5),
                color = "red", linewidth = 1.3) +
  xlim(0, 10) + ylim(0, 0.75) +
  labs(title = expression(lambda == 0.5),
       x = expression(u == x + y), y = expression(f[U](u))) +
  theme_classic(base_size = 14)
Warning: Removed 388 rows containing non-finite outside the scale range
(`stat_bin()`).
Warning: Removed 1 row containing missing values or values outside the scale range
(`geom_bar()`).

ggplot(data = sim_df %>% filter(lambda == 1)) +
  geom_histogram(aes(x = U, y = after_stat(density)),
                 fill = "cornflowerblue", color = "black",
                 binwidth = 0.12, boundary = 0) +
  geom_function(fun = \(u) dgamma(u, shape = 2, rate = 1),
                color = "red", linewidth = 1.3) +
  xlim(0, 10) + ylim(0, 0.75) +
  labs(title = expression(lambda == 1),
       x = expression(u == x + y), y = expression(f[U](u))) +
  theme_classic(base_size = 14)
Warning: Removed 9 rows containing non-finite outside the scale range
(`stat_bin()`).
Warning: Removed 1 row containing missing values or values outside the scale range
(`geom_bar()`).

ggplot(data = sim_df %>% filter(lambda == 1.5)) +
  geom_histogram(aes(x = U, y = after_stat(density)),
                 fill = "cornflowerblue", color = "black",
                 binwidth = 0.12, boundary = 0) +
  geom_function(fun = \(u) dgamma(u, shape = 2, rate = 1.5),
                color = "red", linewidth = 1.3) +
  xlim(0, 10) + ylim(0, 0.75) +
  labs(title = expression(lambda == 1.5),
       x = expression(u == x + y), y = expression(f[U](u))) +
  theme_classic(base_size = 14)
Warning: Removed 1 row containing missing values or values outside the scale range
(`geom_bar()`).

ggplot(data = sim_df %>% filter(lambda == 2)) +
  geom_histogram(aes(x = U, y = after_stat(density)),
                 fill = "cornflowerblue", color = "black",
                 binwidth = 0.12, boundary = 0) +
  geom_function(fun = \(u) dgamma(u, shape = 2, rate = 2),
                color = "red", linewidth = 1.3) +
  xlim(0, 10) + ylim(0, 0.75) +
  labs(title = expression(lambda == 2),
       x = expression(u == x + y), y = expression(f[U](u))) +
  theme_classic(base_size = 14)
Warning: Removed 3 rows containing missing values or values outside the scale range
(`geom_bar()`).