metropolis {mcmc}R Documentation

Metropolis Algorithm

Description

Markov chain Monte Carlo for continuous random vector using a Metropolis algorithm.

Usage

metropolis(x, ...)
metropolis.default(func, state, niter, env=environment(),
    control=list(scaling=1), ...)
metropolis.metropolis(o, niter)

Arguments

func an R function that evaluates the unnormalized probability density of the desired equilibrium distribution of the Markov chain. First argument is the state vector of the Markov chain. Other arguments arbitrary.
state a real vector, the initial state of the Markov chain.
niter the number of iterations.
env the environment for the function func.
control A list of control parameters. See Details.
o an object of type "metropolis" from a preceding run. In this form of the function call all arguments are taken from the o object except niter.

Details

Runs a "random-walk" Metropolis algorithm with multivariate normal proposal producing a Markov chain with equilibrium distribution having a specified unnormalized density. Distribution must be continuous. Support of the distribution is the support of the density specified by argument func. The initial state must satisfy func(state, ...) > 0.

The control argument is a list that can supply any of the following components:

scaling
controls the proposal step size. If scalar or vector, the proposal is x + scaling * z where x is the current and z is a standard normal random vector. If matrix, the proposal is x + scaling %*% z.
spacing
controls the subsampling of the Markov chain. A list with two components. The value list("constant", n) where n is an integer uses every n-th iteration. The value list("geometric", p) where 0 <= p <= 1 uses spacings that are geometric random variables with parameter p.

Value

an object of class "metropolis", which is a list containing at least the following components:

accept fraction of Metropolis proposals accepted.
path length(state) by niter matrix, the sample path.
seed value of .Random.seed before the run.
initial value of argument state, initial state of Markov chain.
final final state of Markov chain, same as path[ , niter].
time running time of Markov chain from system.time().
control from input.
density argument func from input.
extra list containing all ... arguments from input.
env from input.

Examples

h <- function(x) if (all(x >= 0) && sum(x) <= 10) return(1) else return(0)
out <- metropolis(h, rep(0, 5), 10000)
out <- metropolis(out, 5000)
out$accept
plot(out$path[1, ])
hist(out$path[1, ])

[Package Contents]