Skip to content

A Julia implementation of the RAM algorithm (Vihola, 2012)

License

Notifications You must be signed in to change notification settings

anthofflab/RobustAdaptiveMetropolisSampler.jl

Repository files navigation

RobustAdaptiveMetropolisSampler

Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public. codecov

Overview

This package implements the robust adaptive metropolis (RAM) sampler described in Vihola (2012) for the Julia language.

Usage

The RAM_sample function runs a MCMC sampler on a given log target function. The arguments for the functions are as follows:

RAM_sample(logtarget, x0, M0, n; opt_α=0.234, γ=2/3, q=Normal(), show_progress=true)
  • logtarget this must be a callable that accepts one parameter which is a vector of values to evaluate the log target function on. The function passed must return the log value of the target function.
  • x0 is a vector of initial values at which the sampler will start the MCMC algorithm. The length of the vector controls the dimensionality of the problem.
  • M0 is the initial co-variance matrix that the sampler should use to scale the new proposal. M0 can be passed in many different ways:
  1. a scalar: an isotropic covariance matrix with diagonal elements abs2(M0).
  2. an AbstractVector: a diagonal covariance matrix with diagonal elements abs2.(M0).
  3. an AbstractMatrix (or a Diagnoal or an AbstractPDMat): a value of any of these types will be interpreted directly as the covariance matrix.
  • n: the number of elements to be sampled, i.e. the length of the chain.
  • opt_α: the target acceptance rate the algorithm is trying to hit.
  • γ: a parameter for the computation of the step size sequence.
  • q: the proposal distribution.
  • show_progress: a flag that controls whether a progress bar is shown.
  • output_log_probability_x: a flag that controls whether to include output for the log-posterior scores from each Markov chain iteration.

The function returns a NamedTuple with three (or optionally four) elements:

  • chain: a Matrix with the result chain. Each row is one sample, the columns correspond to the dimensions of the problem.
  • acceptance_rate: the acceptance rate for the overall chain.
  • M: the last co-variance matrix used in the algorithm.
  • log_probabilities_x: the log-posterior score from each Markov chain iteration. Each element of log_probabilities_x corresponds to a row from chain.

A simple example of using the function is

using Distributions, RobustAdaptiveMetropolisSampler

chain, accrate, S = RAM_sample(
    p -> logpdf(Normal(3., 2), p[1]), # log target function
    [0.],                             # Initial value
    0.5,                              # Use an isotropic covariance matrix with diagonal elements abs2(0.5)
    100_000                           # Number of runs
)

About

A Julia implementation of the RAM algorithm (Vihola, 2012)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages