# MCMC toolbox for Matlab

## Table of Contents

## Introduction

From this page you can download a set of Matlab function for some statistical MCMC analyses of mathematical models. This code might be useful to you if you are already familiar with Matlab and want to do MCMC analysis using it.

For a more comprehensive and better documented and maintained software for MCMC, see OpenBugs. There are also some MCMC functions in Mahtworks own Statistics Toolbox.

This toolbox provides tools to generate and analyse Metropolis-Hastings MCMC chain using multivariate Gaussian proposal distribution. The covariance matrix of the proposal distribution can be adapted during the simulation according to adaptive schemes described in the references.

The code can do the following

- Produce MCMC chain for user written -2*log(likelihood) and -2*log(prior) functions. These will be equal to sum-of-squares functions when using Gaussian likelihood and prior.
- In case of Gaussian error model, sample the model error variance from the conjugate inverse chi squared distribution.
- Do plots and statistical analyses based on the chain, such as basic statistics, convergence diagnostics, chain timeseries plots, 2 dimensional clouds of points, kernel densities, and histograms.
- Calculate densities, cumulative distributions, quantiles, and random variates for some useful common statistical distributions without using Mathworks own statistics toolbox.

The code is self consistent, no additional Matlab toolboxes are used. However, a quite recent version of Matlab is needed. The documentation might get better gradually, but in the mean while please look at the following page:

- Examples on using the toolbox for some statistical problems.

## Code

The main functions in the toolbox are the following.

`mcmcrun.m`

- Matlab function for the MCMC run. The user provides her own Matlab function to calculate the "sum-of-squares" function for the likelihood part, e.g. a function that calculates minus twice the log likelihood, -2log(p(θ;data)). Optionally a prior "sum-of-squares" function can also be given, returning -2log(p(θ)). See the example and help mcmcrun for more details.
`mcmcplot.m`

- This function plots some useful plots of the generated chain, such as chain time series, 2 dimensional marginal plots, kernel density estimates, and histograms. See help mcmcplot.
`mcmcpred.m`

- For certain types of models is is useful to plot predictive envelopes of model functions by sampling parameter values from the generated chain. This functions calls the model function repeatedly while sampling the unknowns from the chain. It calculates probability regions with respect to a "time" variable of the model. See the examples.
- Other
- Matlab command
`help mcmcstat`

should display the contents of the file`Contents.m`

.

NOTE: There is a similar Matlab code described in my DRAM page. That special DRAM code has fewer options then the general MCMC toolbox function. The DRAM code might be easier to read and alter if you are interested in inner workings of the code.

## Downloads

`mcmcstat.zip`

(updated 2013-01-15)- The toolbox files. Save them to a separate directory, and add the Matlab path to it.
`mcmcexamples.zip`

- Some example Matlab scripts, these are described in the examples page.

Comments are welcome!

## References

H. Haario, M. Laine, A. Mira and E. Saksman, 2006.
*DRAM: Efficient adaptive MCMC*,
Statistics and Computing **16**, pp. 339-354.
(doi:10.1007/s11222-006-9438-0)

H. Haario, E. Saksman and J. Tamminen, 2001.
*An adaptive Metropolis algorithm*
Bernoulli **7**, pp. 223-242.