A C++ library of Markov Chain Monte Carlo (MCMC) methods
A C++ library of Markov Chain Monte Carlo (MCMC) methods
MCMCLib is a lightweight C++ library of Markov Chain Monte Carlo (MCMC) methods.
Features:
float) or double precision (double) library.A list of currently available algorithms includes:
Full documentation is available online: A PDF version of the documentation is available here.
The MCMCLib API follows a relatively simple convention, with most algorithms called in the following manner:
algorithm_id(, , , );The inputs, in order, are:
For example, the RWMH algorithm is called using:
bool rwmh(const ColVec_t& initial_vals, std::function target_log_kernel, Mat_t& draws_out, void* target_data);where ColVec_t is used to represent, e.g., arma::vec or Eigen::VectorXd types.
MCMCLib is available as a compiled shared library, or as header-only library, for Unix-alike systems only (e.g., popular Linux-based distros, as well as macOS). Use of this library with Windows-based systems, with or without MSVC, is not supported.
MCMCLib requires either the Armadillo or Eigen C++ linear algebra libraries. (Note that Eigen version 3.4.0 requires a C++14-compatible compiler.)
Before including the header files, define one of the following:
#define MCMC_ENABLE_ARMA_WRAPPERS
#define MCMC_ENABLE_EIGEN_WRAPPERSExample:
#define MCMC_ENABLE_EIGEN_WRAPPERS
#include "mcmc.hpp"The library can be installed on Unix-alike systems via the standard ./configure && make method.
First clone the library and any necessary submodules:
# clone mcmc into the current directory
git clone https://github.com/kthohr/mcmc ./mcmc
# change directory
cd ./mcmc
# clone necessary submodules
git submodule update --initSet (one) of the following environment variables before running configure:
export ARMA_INCLUDE_PATH=/path/to/armadillo
export EIGEN_INCLUDE_PATH=/path/to/eigenFinally:
# build and install with Eigen
./configure -i "/usr/local" -l eigen -p
make
make installThe final command will install MCMCLib into /usr/local.
Configuration options (see ./configure -h):
Primary
-h print help-i installation path; default: the build directory-f floating-point precision mode; default: double-l specify the choice of linear algebra library; choose arma or eigen-m specify the BLAS and Lapack libraries to link with; for example, -m "-lopenblas" or -m "-framework Accelerate"-o compiler optimization options; defaults to -O3 -march=native -ffp-contract=fast -flto -DARMA_NO_DEBUG-p enable OpenMP parallelization features (recommended)Secondary
-c a coverage build (used with Codecov)-d a 'development' build-g a debugging build (optimization flags set to -O0 -g)Special
--header-only-version generate a header-only version of MCMCLib (see below)MCMCLib is also available as a header-only library (i.e., without the need to compile a shared library). Simply run configure with the --header-only-version option:
./configure --header-only-versionThis will create a new directory, header_only_version, containing a copy of MCMCLib, modified to work on an inline basis. With this header-only version, simply include the header files (#include "mcmc.hpp) and set the include path to the head_only_version directory (e.g.,-I/path/to/mcmclib/header_only_version).
To use MCMCLib with an R package, first generate a header-only version of the library (see above). Then simply add a compiler definition before including the MCMCLib files.
#define MCMC_USE_RCPP_ARMADILLO
#include "mcmc.hpp"At this time, builds using RcppEigen are not supported as MCMCLib requires a version of Eigen >= v3.4.0.
To illustrate MCMCLib at work, consider the problem of sampling values of the mean parameter of a normal distribution.
Code:
…Compile with:
g++ -Wall -std=c++17 -O3 -march=native -ffp-contract=fast -I/path/to/eigen -I/path/to/autodiff -I/path/to/mcmc/include hmc_normal_autodiff.cpp -o hmc_normal_autodiff.cpp -L/path/to/mcmc/lib -lmcmcSee the documentation for more details on this topic.
Keith O'Hara
Apache Version 2
No open issues yet, or sync has not completed.