p_grid <- seq(from = 0, to = 1, length.out = 100)
W = water L = land n = total toss
observations <- list(
"W, W, W" = c(W = 3, n = 3),
"W, W, W, L" = c(W = 3, n = 4),
"L, W, W, L, W, W, W" = c(W = 5, n = 7)
)
# 2M1 — Uniform prior
prior_uniform <- rep(1, length(p_grid))
# 2M2 — Step prior: 0 if p < 0.5, constant if p >= 0.5
prior_step <- ifelse(p_grid < 0.5, 0, 1)
grid_posterior <- function(W, n, prior) {
likelihood <- dbinom(W, size = n, prob = p_grid)
unstd_posterior <- likelihood * prior
posterior <- unstd_posterior / sum(unstd_posterior)
return(posterior)
}
results <- data.frame()
for (obs_name in names(observations)) {
W <- observations[[obs_name]]["W"]
n <- observations[[obs_name]]["n"]
post_uniform <- grid_posterior(W, n, prior_uniform)
post_step <- grid_posterior(W, n, prior_step)
df <- data.frame(
p = p_grid,
posterior = c(post_uniform, post_step),
prior_type = rep(c("2M1: Uniform prior", "2M2: Step prior (p >= 0.5)"), each = length(p_grid)),
obs = obs_name
)
results <- rbind(results, df)
}
cat("\n=== Summary Statistics ===\n\n")
##
## === Summary Statistics ===
summary_stats <- function(posterior, label) {
map_val <- p_grid[which.max(posterior)]
mean_val <- sum(p_grid * posterior)
# 89% HPDI — narrowest interval containing 89% of mass
sorted_idx <- order(posterior, decreasing = TRUE)
cum_mass <- 0
included <- c()
for (i in sorted_idx) {
cum_mass <- cum_mass + posterior[i]
included <- c(included, i)
if (cum_mass >= 0.89) break
}
hpdi_lo <- p_grid[min(included)]
hpdi_hi <- p_grid[max(included)]
cat(sprintf("%-40s MAP=%.2f Mean=%.2f 89%% HPDI=[%.2f, %.2f]\n",
label, map_val, mean_val, hpdi_lo, hpdi_hi))
}
for (obs_name in names(observations)) {
W <- observations[[obs_name]]["W"]
n <- observations[[obs_name]]["n"]
summary_stats(grid_posterior(W, n, prior_uniform),
paste0("2M1 | ", obs_name))
summary_stats(grid_posterior(W, n, prior_step),
paste0("2M2 | ", obs_name))
cat("\n")
}
## 2M1 | W, W, W MAP=1.00 Mean=0.80 89% HPDI=[0.58, 1.00]
## 2M2 | W, W, W MAP=1.00 Mean=0.83 89% HPDI=[0.64, 1.00]
##
## 2M1 | W, W, W, L MAP=0.75 Mean=0.67 89% HPDI=[0.40, 0.95]
## 2M2 | W, W, W, L MAP=0.75 Mean=0.73 89% HPDI=[0.53, 0.91]
##
## 2M1 | L, W, W, L, W, W, W MAP=0.72 Mean=0.67 89% HPDI=[0.44, 0.91]
## 2M2 | L, W, W, L, W, W, W MAP=0.72 Mean=0.71 89% HPDI=[0.52, 0.87]
p(W|E)=0.7 p(W|M)= 0 p(L|E) =0.3 p(L|M= 1
p E|L = (p L|E * p E ) / (p L|E * p E )+ (p L|M * p M ) = 0.30.5) / 1.30.5 = 3/13 = 0.2307692308 => (proved)
Each individual side is a separate, equally likely outcome. There are 6 sides total across 3 cards, and we need to count only the ones that could have landed face up as black.
=> 1 : eliminate WW. The all-white card has zero ways to show a black side. It’s gone. => 2 : count the ways. The remaining two cards produce exactly 3 equally likely black-face-up situations:
BB card, side 1 up → hidden side is Black BB card, side 2 up → hidden side is Black BW card, black side up → hidden side is White
=> 3 : divide. Of those 3 ways, 2 have a black hidden side. So:
Pr(other side = Black | black face up) = 2/3
2 BB card, side 1 up → hidden side is Black 2 BB card, side 2 up → hidden side is Black BW card, black side up → hidden side is White
=>Of those 5 ways, 4 have a black hidden side. So:
Pr(other side = Black | black face up) = 4/5
BB, side 1 up × pull weight 1 → hidden = Black BB, side 2 up × pull weight 1 → hidden = Black BW, black side up × pull weight 2 → hidden = White
=> total weight = 4
Pr(other side = Black | black face up) =2/4 =1/2