sim6.2

set.seed(2026)
n <- 10000

# 1. Simulate Y ~ Beta(1, 2) and compute transformations
y <- rbeta(n, shape1 = 1, shape2 = 2)
u <- 1 - 2 * y
v <- 1 / y
w <- sqrt(y)

par(mfrow = c(1, 3), mar = c(4.5, 4.5, 3, 1))

# 2. U = 1 - 2Y on (-1, 1): f_U(u) = (1 + u) / 2
hist(u, breaks = 40, freq = FALSE, col = "lightblue", border = "white",
     xlim = c(-1, 1), main = expression(paste("U = 1 - 2Y on (-1, 1)")),
     xlab = "u", ylab = "Density")
curve((1 + x) / 2, from = -1, to = 1, col = "firebrick", lwd = 2, add = TRUE)

# 3. V = 1/Y on (1, Inf): f_V(v) = 2(v - 1) / v^3
# Cap x-axis for readable histogram bins; curve still integrates to 1 over (1, Inf)
v_plot <- v[v <= 25]
hist(v_plot, breaks = 40, freq = FALSE, col = "lightgreen", border = "white",
     xlim = c(1, 25), main = expression(paste("V = 1/Y (zoomed to v <= 25)")),
     xlab = "v", ylab = "Density")
curve(2 * (x - 1) / (x^3), from = 1, to = 25, col = "firebrick", lwd = 2, add = TRUE)

# 4. W = sqrt(Y) on (0, 1): f_W(w) = 4w(1 - w^2)
hist(w, breaks = 40, freq = FALSE, col = "plum", border = "white",
     xlim = c(0, 1), main = expression(paste("W = sqrt(Y) on (0, 1)")),
     xlab = "w", ylab = "Density")
curve(4 * x * (1 - x^2), from = 0, to = 1, col = "firebrick", lwd = 2, add = TRUE)

par(mfrow = c(1, 1))