knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
require(mc2)
require(ggplot2)
require(ggpubr)

Target distribution

We begin by defining a target log posterior density.

log_prob <- function(x){donut(x)}
log_prob(c(2,3))

Metropolis algorithm

First we sample using the Metropolis algoritm.

SIG   <- c(0.1,0.5,1.0)
ITER  <- rep(1000, 3)
PLT   <- list()
for(j in 1:3){
  x0     <- stats::rnorm(2)
  sig    <- SIG[j]
  iter   <- ITER[j]
  chain  <- metropolis(log_prob, x0, iter, sig)
  title  <- paste0('Metropolis, sig = ', sig, ', iter = ', iter)
  p      <- plt.surface(donut, title = title)
  PLT[[j]] <- add.path(p, chain$x, lines = FALSE, alpha = 0.3)
}

ggarrange(plotlist = PLT, nrow = 1, ncol = 3)


jtimonen/mc2 documentation built on Jan. 20, 2020, 10:37 a.m.