sim4.3

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(ggplot2)

p <- 1 - exp(-1/3)
df_12 <- tibble(X = ceiling(rexp(10000, rate = 1/3)))

ggplot(df_12, aes(x = X)) +
  stat_ecdf(geom = "step", color = "steelblue", linewidth = 1) +
  geom_point(
    data = tibble(x = 1:max(df_12$X), y = pgeom(x-1, p)), 
    aes(x = x, y = y), color = "darkred", size = 2
  ) +
  labs(title = "Problem 12: CDF", y = "F(x)") +
  theme_minimal()

df_13 <- tibble(Y = -log(runif(10000)) / 0.5)

# Plot 1: Overlaid Densities
ggplot(df_13, aes(x = Y)) +
  geom_histogram(aes(y = after_stat(density)), bins = 50, fill = "lightgray", color = "white") +
  stat_function(fun = dexp, args = list(rate = 0.5), color = "darkred", linewidth = 1) +
  labs(title = "Problem 13: EXP(0.5)", y = "Density") +
  theme_minimal()

ggplot(df_13, aes(x = Y)) +
  stat_ecdf(color = "steelblue", linewidth = 1) +
  stat_function(fun = pexp, args = list(rate = 0.5), color = "darkred", linetype = "dashed", linewidth = 1) +
  labs(title = "Problem 13: CDF vs EXP(0.5)", y = "F(y)") +
  theme_minimal()