Learn R Programming

adaptMCMC (version 1.0.2)

MCMC.parallel: Parallel computation of MCMC()

Description

A wrapper function to generate several independent Markov chains by stetting up cluster on a multi-core machine. The function is based on the parallel package.

Usage

MCMC.parallel(p, n, init, n.chain = 4, n.cpu, packages = NULL, dyn.libs=NULL,
    scale = rep(1, length(init)), log = TRUE, adapt = !is.null(acc.rate),
    acc.rate = NULL, gamma = 0.55, list = FALSE, ...)

Arguments

p
function that returns the (log) probability density to sample from. Must have two or more dimensions.
n
number of samples.
init
vector with initial values.
n.chain
number of independent chains.
n.cpu
number of CPUs that should be used in parallel.
packages
vector with name of packages to load into each instance. (Typically, all packages on which p depends.)
dyn.libs
vector with name of dynamic link libraries (shared objects) to load into each instance. The libraries must be located in the working directory.
scale
vector with the variances or covariance matrix of the jump distribution.
log
logical. If TRUE, a log density is expected from p (strongly recommended).
adapt
if TRUE, adaptive sampling is used, if FALSE classic metropolis sampling, if a positive integer the adaption stops after adapt iterations.
acc.rate
desired acceptance rate (ignored if adapt=FALSE)
gamma
controls the speed of adaption. Should be between 0.5 and 1. A lower gamma leads to faster adaption.
list
logical. If TRUE a list of lits is returned otherwise a list of matrices with the samples.
...
further arguments passed to p

Value

  • A list with a list or matrix for each chain. See MCMC for details.

Details

This function is just a wrapper to use MCMC in parallel. It is based on parallel. Obviously, the application of this function makes only sense on a multi-core machine.

See Also

MCMC

Examples

Run this code
## ----------------------
## Banana shaped distribution

## log-pdf to sample from
p.log <- function(x) {
  B <- 0.03                              # controls 'bananacity'
  -x[1]^2/200 - 1/2*(x[2]+B*x[1]^2-100*B)^2
}

## ----------------------
## generate samples
## compute 4 independent chains on 2 CPU's (if available) in parallel

samp <- MCMC.parallel(p.log, n=200, init=c(x1=0, x2=1),
    n.chain=4, n.cpu=2, scale=c(1, 0.1),
    adapt=TRUE, acc.rate=0.234)

str(samp)

Run the code above in your browser using DataLab