PS_6_2

Question 6 - Simulation of Q1

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
N <- 10000

sim_df <- data.frame(Y = rbeta(N, 1, 2)) %>%
  mutate(
    U = 1 - 2*Y,
    V = 1/Y,
    W = sqrt(Y)
  ) %>%
  mutate(
    fY = dbeta(Y, 1, 2),
    fU_analytic = (1 + U) / 2,
    fV_analytic = 2*(V - 1) / V^3,
    fW_analytic = 4*W*(1 - W^2)
  )
head(sim_df)
           Y           U         V         W        fY fU_analytic  fV_analytic
1 0.37447957  0.25104087  2.670373 0.6119474 1.2510409   0.6255204 0.1754396475
2 0.08507045  0.82985910 11.754963 0.2916684 1.8298591   0.9149296 0.0132426560
3 0.02166518  0.95666964 46.157013 0.1471910 1.9566696   0.9783348 0.0009184216
4 0.16384736  0.67230528  6.103242 0.4047806 1.6723053   0.8361526 0.0448946355
5 0.80505280 -0.61010559  1.242155 0.8972473 0.3898944   0.1949472 0.2526944660
6 0.47910112  0.04179777  2.087242 0.6921713 1.0417978   0.5208989 0.2391320509
  fW_analytic
1   1.5311423
2   1.0674241
3   0.5760082
4   1.3538336
5   0.6996634
6   1.4422050

U Overlay

ggplot(sim_df, aes(x = U)) +
  geom_histogram(aes(y = after_stat(density)), bins = 50, fill = 'cornflowerblue', color = 'black') +
  geom_line(aes(y = fU_analytic), linewidth = 1, color = 'red') +
  labs(x = 'U', y = 'Density', title = 'U = 1 - 2Y') +
  theme_classic()

V Overlay

ggplot(sim_df, aes(x = V)) +
  geom_histogram(aes(y = after_stat(density)), bins = 50, fill = 'cornflowerblue', color = 'black') +
  geom_line(aes(y = fV_analytic), linewidth = 1, color = 'red') +
  xlim(c(1, 10)) +
  labs(x = 'V', y = 'Density', title = 'V = 1/Y') +
  theme_classic()
Warning: Removed 1907 rows containing non-finite outside the scale range
(`stat_bin()`).
Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_bar()`).
Warning: Removed 1907 rows containing missing values or values outside the scale range
(`geom_line()`).

W Overlay

ggplot(sim_df, aes(x = W)) +
  geom_histogram(aes(y = after_stat(density)), bins = 50, fill = 'cornflowerblue', color = 'black') +
  geom_line(aes(y = fW_analytic), linewidth = 1, color = 'red') +
  labs(x = 'W', y = 'Density', title = 'W = sqrt(Y)') +
  theme_classic()