Running the Gibbs sampler

Section of Markov chains, Gibbs sampling, and Metropolis–Hastings, part of Bayesian statistics

Running the Gibbs sampler

Type Section
Taught in Markov chains, Gibbs sampling, and Metropolis–Hastings
Part Bayesian statistics

The full conditionals above are the part to derive. Once they are correct, the sampler itself is a short computation. The newest value of x_1 must be used in the update of x_2; using the old value in both lines would define a different transition rule.

gibbs_exp <- function(iterations) {
  out <- matrix(0, iterations, 2)  # start at (0, 0)
  for (t in 2:iterations) {
    out[t, 1] <- rexp(1, rate = out[t - 1, 2] + 1)
    out[t, 2] <- rexp(1, rate = out[t, 1] + 1)
  }
  out
}
set.seed(3500)
draw <- gibbs_exp(20000)
keep <- draw[-seq_len(1000), ]
matplot(keep, type = "l", lty = 1)
acf(keep[, 1])

The trace plot and autocorrelation function are not substitutes for the stationarity argument. They address the separate practical question of whether this finite run appears to have explored the target well enough for the posterior summary at hand.

Used in

See also

Markov chain · Stationarity and detailed balance · Detailed balance implies stationarity · Systematic-scan Gibbs sampler · Warm-up is not a convergence proof · Metropolis–Hastings update · Metropolis–Hastings invariance · Markov chains and equilibrium

Glossary and index