/pyhmc

Hamiltonain Monte Carlo in Python

Primary LanguagePythonOtherNOASSERTION

pyhmc: Hamiltonian Monte Carlo in Python

Build Status License Docs

This package is a straight-forward port of the functions hmc2.m and hmc2_opt.m from the MCMCstuff matlab toolbox written by Aki Vehtari. The code is originally based on the functions hmc.m from the netlab toolbox written by Ian T Nabney. The portion of algorithm involving "windows" is derived from the C code for this function included in the Software for Flexible Bayesian Modeling written by Radford Neal.

The original Python port was made by Kilian Koepsell, and subsequently modernized by Robert T. McGibbon.

Authors

This software is distributed under the BSD License (see LICENSE file).

Example

If you wanted to draw samples from a 5 dimensional Gaussian, you would do something like:

import numpy as np
def logprob(x, ivar):
    logp = -0.5 * np.sum(ivar * x**2)
    grad = -ivar * x
    return logp, grad
from pyhmc import hmc
ivar = 1. / np.random.rand(5)
samples = hmc(logprob, x0=np.random.randn(5), args=(ivar,), n_samples=int(1e4))
# Using the beautiful $ pip install triangle_plot
import triangle
figure = triangle.corner(samples)
figure.savefig('triangle.png')

triangle