Define the Grid

p_grid <- seq(from = 0, to = 1, length.out = 100)

Define the observation sets

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 approximation function

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)
}

Compute all posteriors and collect into a data frame

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)
}

Summary statistics for each scenario

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]

Suppose there are two globes, one for Earth and one for Mars. The Earth globe is 70% covered in water. The Mars globe is 100% land. Further suppose that one of these globes—you don’t know which—was tossed in the air and produced a “land” observation. Assume that each globe was equally likely to be tossed. Show that the posterior probability that the globe was the Earth, conditional on seeing “land” (Pr(Earth|land)), is 0.23.

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)

Suppose you have a deck with only three cards. Each card has two sides, and each side is either black or white. One card has two black sides. The second card has one black and one white side. The third card has two white sides. Now suppose all three cards are placed in a bag and shuffled. Someone reaches into the bag and pulls out a card and places it flat on a table. A black side is shown facing up, but you don’t know the color of the side facing down. Show that the probability that the other side is also black is 2/3. Use the counting method (Section 2 of the chapter) to approach this problem. This means counting up the ways that each card could produce the observed data (a black side facing up on the table).

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

Now suppose there are four cards: B/B, B/W, W/W, and another B/B. Again suppose a card is drawn from the bag and a black side appears face up. Again calculate the probability that the other side is black.

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

Imagine that black ink is heavy, and so cards with black sides are heavier than cards with white sides. As a result, it’s less likely that a card with black sides is pulled from the bag. So again assume there are three cards: B/B, B/W, and W/W. After experimenting a number of times, you conclude that for every way to pull the B/B card from the bag, there are 2 ways to pull the B/W card and 3 ways to pull the W/W card. Again suppose that a card is pulled and a black side appears face up. Show that the probability the other side is black is now 0.5. Use the counting method, as before.

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