metropolis: Metropolis MCMC

Description Usage Arguments Details Examples

View source: R/metropolis.R

Description

The metropolis algorithm is a special case of the metropolis-hastings algorithm, namely where the proposal distribution is symmetric.

Usage

1
2
3
4
5
6
  metropolis <- function(
    rproposal,
    prob,
    niter,
    init,
    log_prob = FALSE)

Arguments

rproposal

rproposal(previous_val) generates a value from the proposal distribution

prob

prob(val_proposed)/prob(val_t_minus_1) forms the acceptance probability

niter

number of iterations to perform

init

vector of initial values

log_prob

whether or not prob function specifies log probabilities

Details

This function returns a niter x d matrix of values where d is the dimension of init and the dimension of each element from rproposal. The returned matrix contains all generated values of the metropolis walk.

Examples

1
2
3
4
5
6
7
8
  vals <- metropolis(
    rproposal = function(val){ rnorm(1, mean = val, sd = 1){} },
    prob = posterior_prob,
    niter = 10000,
    init = 0)

  library(coda)
  plot(as.mcmc(vals))

dcbdan/s525 documentation built on May 19, 2019, 10:48 p.m.