2.6 Practice
The practice problems for Chapter 2 reinforce the garden of forking data and the mechanics of Bayesian updating. They ask you to compute posteriors by hand, explore the effect of different priors, and compare grid approximation to analytical solutions.
Easy problems
2E1. Which of the following is a valid expression of Bayes' theorem?
(a)
(b)
(c)
2E2. Rewrite Bayes' theorem in terms of the likelihood, the prior, and the posterior. What is the role of each component?
2E3. Using the grid approximation for the globe tossing problem (6 waters in 9 tosses), compute the posterior probability that .
# 2E3: Compute the posterior probability that p < 0.5
p_grid <- seq(0, 1, length.out = 1000)
prior <- rep(1, 1000)
likelihood <- dbinom(6, size = 9, prob = p_grid)
posterior <- (likelihood * prior) / sum(likelihood * prior)
# Probability that p < 0.5
prob_less_than_half <- sum(posterior[p_grid < 0.5])
cat("P(p < 0.5) =", round(prob_less_than_half, 4), "\n")2E4. Suppose you observe 3 waters in 3 tosses. Using a uniform prior, what is the posterior distribution? How does it differ from the case with 6 waters in 9 tosses?
Medium problems
2M1. Compute the posterior distribution for the globe tossing problem with each of the following priors. Plot all three posteriors on the same graph.
(a) Uniform prior:
(b) Step prior: if , else
(c) Triangular prior: , peaked at
# 2M1: Compare posteriors with different priors
p_grid <- seq(0, 1, length.out = 500)
likelihood <- dbinom(6, size = 9, prob = p_grid)
# (a) Uniform
prior*a <- rep(1, 500)
post_a <- (likelihood * prior*a) / sum(likelihood * prior_a)
# (b) Step: 0 if p < 0.5, 1 otherwise
prior*b <- ifelse(p_grid < 0.5, 0, 1)
post_b <- (likelihood * prior*b) / sum(likelihood * prior_b)
# (c) Triangular: peaked at 0.5
prior*c <- 1 - abs(p_grid - 0.5) * 2
post*c <- (likelihood * prior_c) / sum(likelihood * prior_c)
# Plot
plot(p_grid, post_a, type = "l", lwd = 2, col = "steelblue",
xlab = "p", ylab = "Posterior density",
main = "2M1: Posteriors with different priors")
lines(p_grid, post_b, lwd = 2, col = "firebrick")
lines(p_grid, post_c, lwd = 2, col = "darkgreen")
legend("topright",
legend = c("(a) Uniform", "(b) Step", "(c) Triangular"),
col = c("steelblue", "firebrick", "darkgreen"), lwd = 2)2M2. For each of the priors in 2M1, compute the posterior mean and 95% credible interval. How much does the prior choice matter with 9 data points?
2M3. Suppose you observe 8 waters in 15 tosses. Using a uniform prior, compute the posterior using grid approximation. How does the posterior differ from the 6-in-9 case?
Hard problems
2H1. Suppose there are two species of globe: one with and one with . You don't know which species you have. You toss the globe 3 times and get W, W, W. Using a uniform prior over the two species, compute the posterior probability that you have the species.
# 2H1: Two species of globe
# Species A: p = 0.5, Species B: p = 0.7
# Prior: P(A) = P(B) = 0.5
# Data: W, W, W (3 waters in 3 tosses)
p_A <- 0.5
p_B <- 0.7
prior_A <- 0.5
prior_B <- 0.5
# Likelihood: P(WWW | species) = p^3
likelihood_A <- p_A^3
likelihood_B <- p_B^3
# Unnormalized posterior
unstd*A <- likelihood_A * prior*A
unstd_B <- likelihood_B * prior_B
# Normalized posterior
post_A <- unstd_A / (unstd_A + unstd_B)
post_B <- unstd_B / (unstd_A + unstd_B)
cat("P(Species A | WWW) =", round(post_A, 4), "\n")
cat("P(Species B | WWW) =", round(post_B, 4), "\n")
cat("\nAfter 3 waters, the p=0.7 species is more likely\n")2H2. Continuing 2H1, now toss the globe 3 more times and get L, W, L. Update the posterior. What is the posterior probability of each species after all 6 tosses?
2H3. Implement the sequential updating algorithm for the globe tossing problem. Start with a uniform prior and update after each toss: W, L, W, W, W, L, W, L, W. Plot the posterior after each update.