knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
require(mc2) require(ggplot2) require(ggpubr)
We begin by defining a target log posterior density.
log_prob <- function(x){donut(x)} log_prob(c(2,3))
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.