set.seed(8670)
comparison_pairs <- data.frame(y = runif(160), u = runif(160))
comparison_pairs$decision <- ifelse(
comparison_pairs$u <= 4 * comparison_pairs$y * (1 - comparison_pairs$y),
"Accepted", "Rejected"
)
comparison_examples <- data.frame(
y = c(0.2, 0.8), u = c(0.4, 0.8),
decision = c("Accepted", "Rejected"),
label = c("A: accept", "B: reject")
)
comparison_grid <- data.frame(y = seq(0, 1, length.out = 501))
comparison_colors <- c("Accepted" = "steelblue", "Rejected" = "grey55")
comparison_panel <- function(envelope, target_multiplier, title,
target_label, envelope_label, axis_label,
acceptance_label, y_breaks) {
points <- transform(comparison_pairs, ordinate = envelope * u)
examples <- transform(comparison_examples, ordinate = envelope * u)
curve <- transform(comparison_grid,
target = target_multiplier * y * (1 - y))
ggplot() +
geom_ribbon(data = curve, aes(y, ymin = 0, ymax = target),
fill = "steelblue", alpha = 0.12) +
geom_point(data = points, aes(y, ordinate, color = decision),
alpha = 0.55, size = 1.5) +
geom_line(data = curve, aes(y, target), color = "black", linewidth = 1) +
geom_hline(yintercept = envelope, color = "#B45F06",
linetype = "dashed", linewidth = 0.9) +
annotate("label", x = 0.5, y = 0.28 * envelope, label = target_label,
size = 3.6, color = "black", fill = "white", linewidth = 0) +
annotate("label", x = 0.5, y = 1.13 * envelope, label = envelope_label,
size = 3.7, color = "#9A4E00", fill = "white", linewidth = 0,
lineheight = 1.1) +
geom_point(data = examples, aes(y, ordinate, fill = decision),
shape = 21, color = "black", size = 3.3, stroke = 0.8,
show.legend = FALSE) +
geom_label(data = examples,
aes(y, ordinate + 0.09 * envelope, label = label),
size = 3.4, fill = "white", linewidth = 0.2) +
scale_color_manual(values = comparison_colors, breaks = names(comparison_colors)) +
scale_fill_manual(values = comparison_colors, guide = "none") +
scale_x_continuous(limits = c(0, 1), breaks = seq(0, 1, by = 0.25),
expand = expansion(mult = c(0.02, 0.02))) +
scale_y_continuous(limits = c(0, 1.25 * envelope), breaks = y_breaks,
expand = expansion(mult = c(0, 0))) +
labs(title = title, x = "Same proposal y", y = axis_label,
color = NULL, caption = acceptance_label) +
theme(legend.position = "bottom", legend.text = element_text(size = 11),
plot.title = element_text(size = 16),
plot.caption = element_text(size = 11, hjust = 0.5),
plot.margin = margin(8, 10, 4, 8))
}
normalized_comparison <- comparison_panel(
envelope = 1.5, target_multiplier = 6, title = "Normalized target",
target_label = "Target f(y) = 6y(1-y)\nTotal area = 1",
envelope_label = "Envelope Mg(y) = 3/2\nTotal area = 3/2",
axis_label = "Auxiliary ordinate V = 1.5U\n(normalized density scale)",
acceptance_label = "Acceptance probability = 1 / (3/2) = 2/3",
y_breaks = c(0, 0.3, 0.6, 0.9, 1.2, 1.5)
)
unnormalized_comparison <- comparison_panel(
envelope = 0.25, target_multiplier = 1, title = "Unnormalized target",
target_label = "Target q(y) = y(1-y)\nTotal area = 1/6",
envelope_label = "Envelope Cg(y) = 1/4\nTotal area = 1/4",
axis_label = "Auxiliary ordinate W = U/4\n(unnormalized target scale)",
acceptance_label = "Acceptance probability = (1/6) / (1/4) = 2/3",
y_breaks = seq(0, 0.25, by = 0.05)
)
# Both scales classify every shared (Y,U) pair identically.
stopifnot(identical(
1.5 * comparison_pairs$u <= dbeta(comparison_pairs$y, 2, 2),
0.25 * comparison_pairs$u <= comparison_pairs$y * (1 - comparison_pairs$y)
))
grid::grid.newpage()
grid::pushViewport(grid::viewport(layout = grid::grid.layout(
2, 2, heights = grid::unit(c(1, 0.085), "null")
)))
print(normalized_comparison, newpage = FALSE,
vp = grid::viewport(layout.pos.row = 1, layout.pos.col = 1))
print(unnormalized_comparison, newpage = FALSE,
vp = grid::viewport(layout.pos.row = 1, layout.pos.col = 2))
grid::grid.text(
"Same Y and U: identical accept/reject decisions. Right vertical scale = left / 6.",
gp = grid::gpar(fontsize = 12),
vp = grid::viewport(layout.pos.row = 2, layout.pos.col = 1:2)
)
grid::popViewport()