# Gibbs sampling with conjugate updates
# https://tidyecology.com/posts/gibbs-sampling-conjugate/
#
# The R code of this Tidy Ecology tutorial, chunk by chunk, in the order of the post.
# Run it top to bottom in a fresh R session. Code the post shows but does not run
# is commented out. The prose, and the numbers it quotes, are in the post itself.
# Post last modified: 2026-10-01

## ---- setup ----
library(ggplot2)
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE,
                      fig.align = "center", dpi = 150)
paper <- "#f5f4ee"; ink <- "#16241d"; forest <- "#275139"; sage <- "#93a87f"
line_col <- "#dad9ca"; faint <- "#5d6b61"; accent2 <- "#c9b458"; warm <- "#cda23f"; brick <- "#b5534e"
theme_te <- function(base = 12) theme_minimal(base_size = base) + theme(
  plot.background = element_rect(fill = paper, colour = NA),
  panel.background = element_rect(fill = paper, colour = NA),
  panel.grid.major = element_line(colour = line_col, linewidth = 0.3),
  panel.grid.minor = element_blank(),
  axis.title = element_text(colour = ink), axis.text = element_text(colour = faint),
  plot.title = element_text(colour = ink, face = "bold"),
  plot.subtitle = element_text(colour = faint),
  legend.title = element_text(colour = ink), legend.text = element_text(colour = faint),
  legend.position = "top",
  strip.text = element_text(colour = ink, face = "bold"))
theme_set(theme_te())

## ---- data ----
set.seed(2718)
n <- 12L
y <- rnorm(n, mean = 76, sd = 3.2)      # a small synthetic sample, wing length (mm)
ybar <- mean(y)
s2 <- var(y)                             # sample variance (denominator n - 1)
s  <- sqrt(s2)
round(c(n = n, ybar = ybar, s = s, s2 = s2), 3)

## ---- sampler ----
gibbs <- function(n_iter, y, init_sigma2 = 1) {
  n <- length(y); ybar <- mean(y)
  mu <- numeric(n_iter); sig2 <- numeric(n_iter)
  cur_sig2 <- init_sigma2
  for (t in seq_len(n_iter)) {
    cur_mu   <- rnorm(1, ybar, sqrt(cur_sig2 / n))                 # mu | sigma^2, y
    ss       <- sum((y - cur_mu)^2)
    cur_sig2 <- 1 / rgamma(1, shape = n / 2, rate = ss / 2)        # sigma^2 | mu, y
    mu[t] <- cur_mu; sig2[t] <- cur_sig2
  }
  data.frame(mu = mu, sigma2 = sig2)
}

## ---- run ----
n_iter <- 40000L; burn <- 2000L
set.seed(90210)
chain <- gibbs(n_iter, y, init_sigma2 = 1)
post  <- chain[(burn + 1):n_iter, ]
round(c(acf1_mu = acf(post$mu, 1, plot = FALSE)$acf[2],
        acf1_sigma2 = acf(post$sigma2, 1, plot = FALSE)$acf[2]), 3)

## ---- fig-staircase ----
sig <- sqrt(post$sigma2)
K <- 30
mu <- post$mu[1:K]; sg <- sig[1:K]
vx <- mu[1]; vy <- sg[1]
for (t in 2:K) { vx <- c(vx, mu[t], mu[t]); vy <- c(vy, sg[t-1], sg[t]) }
stair <- data.frame(x = vx, y = vy)
cloud <- data.frame(mu = post$mu, sigma = sig)[seq(1, nrow(post), by = 4), ]
ggplot() +
  geom_point(data = cloud, aes(mu, sigma), colour = sage, alpha = 0.20, size = 0.5) +
  geom_path(data = stair, aes(x, y), colour = ink, linewidth = 0.35) +
  geom_point(data = stair[1, ], aes(x, y), colour = brick, size = 2) +
  labs(x = expression(mu ~ "(mean wing length, mm)"), y = expression(sigma ~ "(mm)"),
       title = "Gibbs moves one coordinate at a time",
       subtitle = "green: posterior draws; line: 30 alternating conditional updates after burn-in")

## ---- marginals ----
mu_ci_gibbs <- quantile(post$mu, c(0.025, 0.975))
mu_ci_exact <- ybar + c(-1, 1) * qt(0.975, df = n - 1) * s / sqrt(n)   # exact t interval
mu_ci_naive <- ybar + c(-1, 1) * 1.96 * s / sqrt(n)                    # fix sigma at s, use normal
s2_ci_gibbs <- quantile(post$sigma2, c(0.025, 0.975))
s2_ci_exact <- (n - 1) * s2 / qchisq(c(0.975, 0.025), df = n - 1)      # exact for the variance
rbind(mu_gibbs = mu_ci_gibbs, mu_exact_t = mu_ci_exact, mu_naive = mu_ci_naive)

## ---- fig-marginal ----
xs <- seq(ybar - 4.5, ybar + 4.5, length.out = 400)
sc <- s / sqrt(n)
curv <- rbind(
  data.frame(x = xs, dens = dt((xs - ybar) / sc, df = n - 1) / sc, src = "exact posterior t (df 11)"),
  data.frame(x = xs, dens = dnorm(xs, ybar, sc), src = "naive fixed-sigma normal"))
curv$src <- factor(curv$src, levels = c("exact posterior t (df 11)", "naive fixed-sigma normal"))
ggplot() +
  geom_histogram(data = data.frame(mu = post$mu), aes(mu, after_stat(density)),
                 binwidth = 0.15, fill = sage, colour = paper, alpha = 0.6) +
  geom_line(data = curv, aes(x, dens, colour = src, linetype = src), linewidth = 0.85) +
  scale_colour_manual(values = c(forest, brick), name = NULL) +
  scale_linetype_manual(values = c("solid", "longdash"), name = NULL) +
  labs(x = expression(mu ~ "(mean wing length, mm)"), y = "density",
       title = "The mean's posterior is a t, not a normal",
       subtitle = "accounting for unknown variance widens the interval (heavier tails)")
