A Python package collecting awesome MCMC sampling algorithms.
Explore the docs »
Examples
|
Report Bug
|
Request Feature
- Racecar is a lightweight Python library for sampling distributions in high dimensions using cutting-edge algorithms. It can also be used for rapid prototyping of novel methods and application to large problems.
- Pass a function evaluating the log posterior and/or its gradient, and away you go. Ideal for usage with big data applications, neural networks, regression, mixture modelling, and all sorts of Bayesian inference and sampling problems.
- Easily to use and simple to extend with new methods and use cases.
- Designed for use with stochastic gradients in mind.
We will sample points from the one-dimensional distribution using the random walk Metropolis algorithm, and then plot the results.
# Import racecar and numpy
import racecar as rc
import numpy as np
# Define the target log posterior function
# Note we only need it up to a constant multiple, so we do not need to
# know its normalization constant.
def log_posterior(x):
return {
'llh' : -( np.cos(2*x) + x**2/12 )
}
# Create the sampler object and use Random Walk Metropolis
initial_condition = [0]
learning_rate = 0.5
S = rc.sampler(initial_condition, learning_rate, log_posterior, algo="RWMetropolis")
# Sample some points, outputting arrays of the position and the log posterior
number_of_points = 100000
Pos_traj, LLH_traj = S.sample(number_of_points, output=['pos','llh'])
# Plot the results using matplotlib
Some more detailed examples are given in the Jupyter notebooks below
You can install the package from source by cloning this repo and using setup.py
, the only dependencies are on numpy
and scipy
. Otherwise it is available on pip via
pip install racecar
Distributed under the MIT License. See LICENSE
for more information.
Made by Charles Matthews - www.cmatthe.ws - [email protected]
Project Link: https://github.com/c-matthews/racecar