A Matlab interface for JAGS

This interface allows JAGS ("Just Another Gibbs Sampler") to be used in combination with Matlab. JAGS is a program for Bayesian hierarchical models using Markov chain Monte Carlo (MCMC) inference methods. JAGS is similar to the OpenBUGS and WinBUGS programs but can operate on a number of platforms including Windows and Max OS X.

This Matlab interface is based on matbugs written by Kevin Murphy and Maryam Mahdaviani for WinBUGS and OpenBUGS. This JAGS interface uses very similar syntax.

One of the main advantages of this interface is that the MCMC chains can run in parallel if the Matlab Parallel Computing Toolbox is installed. This can lead to near-linear speedups in simulation time. For example, running 8 chains in parallel on a 8-core machine can lead to a speedup factor of almost 8. 

This version of MATJAGS was tested on JAGS version 3.3.0, with Matlab version R2013A. It was designed for a windows platform but the latest code should also run on a Mac.


  1. Download the files for the matjags interface
    • Store the files in some working directory
  2. Install JAGS
    • Visit the JAGS development site and follow instructions to install the latest windows version of JAGS. This version of MATJAGS was tested on version 3.0.0
  3. [WINDOWS USERS] Place the JAGS bin directory in the Windows path
    • The directory where the JAGS executable is stored should be placed in the windows path.
    • In Windows 7, go to Control Panel, System and Security, System and click on "Advanced System Settings" followed by "Environment Variables". Under System variables, click on Path, and add the jags path to the string. This could look something like "C:\Program Files\JAGS\JAGS-3.3.0\x64\bin" or whatever the path is. Note that you should put a semicolon at the end of the existing path before pasting in the JAGS path.
    • If you have Matlab up and running already, quit Matlab and start it again to make sure that the new path is used by Matlab.
    • You can test whether the system path is updated by starting a dos prompt window (type "cmd" in the "search programs and files" box when you click the windows start button and type in "JAGS". If the path is updated, it should start the JAGS program).
  4. Place the folder where matjags.m is stored on your Matlab path. [WINDOWS USERS: in Windows 7, you'll have to run Matlab as administrator to change the Matlab path. If matjags.m is not stored on the Matlab path, parallel execution might lead to errors]
  5. Test MATJAGS
    • Run one of the example scripts for MATJAGS included in the package. It might be helpful if the folder where MATJAGS is stored is placed on the Matlab path. [WINDOWS USERS: to change the matlab path in Windows 7, you'll have to run Matlab as an administrator -- right click the matlab program icon, and choose "run as administrator"]

Example Scripts and Output

Script Description Required software
exampleJAGS1 running multiple MCMC chains serially for rate inference problem  
exampleJAGS2 running multiple MCMC chains in parallel for rate inference problem Parallel Computing Toolbox
exampleJAGS3 comparing JAGS to WinBUGS for the rate inference problem Matbugs (included) and WinBUGS
exampleJAGS4 calculate the speedup factor when running the MCMC chains in parallel Parallel Computing Toolbox
exampleJAGS5 comparing JAGS to WinBUGS for the kappa inference problem Matbugs (included) and WinBUGS


10/31/2011    Matjags was updated to work with Mac platforms (thanks to Mike Kalish for helpful suggestions)

Inquiries about the Matlab interface

Mark Steyvers