Metropolis sampling of a continuous distribution
Implements Metropolis sampling for an arbitrary continuous probability distribution
metropolis(logpost, current, C, iter, ...)
logpost
: function definition of the log probability functioncurrent
: starting value of algorithmC
: half-width of proposal intervaliter
: number of iterations...
: other inputs needed in logpost functionS: vector of simulated values
accept_rate: acceptance rate of algorithm
Jim Albert
lpost <- function(theta, s){ dnorm(s$ybar, theta, s$se, log = TRUE) + dcauchy(theta, s$loc, s$scale, log = TRUE) } s <- list(ybar = 20, se = 0.4, loc = 10, scale = 2) post <- metropolis(lpost, 10, 20, 100, s)