Science Score: 77.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
✓DOI references
Found 49 DOI reference(s) in README -
✓Academic publication links
Links to: arxiv.org, zenodo.org -
✓Committers with academic emails
1 of 3 committers (33.3%) from academic institutions -
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (14.8%) to scientific vocabulary
Keywords
Keywords from Contributors
Repository
Manifold Markov chain Monte Carlo methods in Python
Basic Info
- Host: GitHub
- Owner: matt-graham
- License: mit
- Language: Python
- Default Branch: main
- Homepage: https://matt-graham.github.io/mici/
- Size: 36.8 MB
Statistics
- Stars: 232
- Watchers: 13
- Forks: 27
- Open Issues: 4
- Releases: 15
Topics
Metadata Files
README.md
Mici is a Python package providing implementations of Markov chain Monte Carlo (MCMC) methods for approximate inference in probabilistic models, with a particular focus on MCMC methods based on simulating Hamiltonian dynamics on a manifold.
Features
Key features include
- a modular design allowing use of a wide range of inference algorithms by mixing and matching different components, and making it easy to extend the package,
- a pure Python code base with minimal dependencies, allowing easy integration within other code,
- built-in support for several automatic differentiation frameworks, including JAX and Autograd, or the option to supply your own derivative functions,
- implementations of MCMC methods for sampling from distributions on embedded manifolds implicitly-defined by a constraint equation and distributions on Riemannian manifolds with a user-specified metric,
- computationally efficient inference via transparent caching of the results of expensive operations and intermediate results calculated in derivative computations allowing later reuse without recalculation,
- memory efficient inference for large models by memory-mapping chains to disk, allowing long runs on large models without hitting memory issues.
Installation
To install and use Mici the minimal requirements are a Python 3.10+ environment with NumPy and SciPy installed. The latest Mici release on PyPI (and its dependencies) can be installed in the current Python environment by running
sh
pip install mici
To instead install the latest development version from the main branch on Github run
sh
pip install git+https://github.com/matt-graham/mici
If available in the installed Python environment the following additional packages provide extra functionality and features
- ArviZ: if ArviZ is
available the traces (dictionary) output of a sampling run can be directly
converted to an
arviz.InferenceDatacontainer object usingarviz.convert_to_inference_dataor implicitly converted by passing the traces dictionary as thedataargument to ArviZ API functions, allowing straightforward use of the ArviZ's extensive visualisation and diagnostic functions. - Autograd: if available Autograd will
be used to automatically compute the required derivatives of the model
functions (providing they are specified using functions from the
autograd.numpyandautograd.scipyinterfaces). To sample chains in parallel usingautogradfunctions you also need to install multiprocess. This will causemultiprocess.Poolto be used in preference to the in-builtmutiprocessing.Poolfor parallelisation as multiprocess supports serialisation (via dill) of a much wider range of types, including of Autograd generated functions. Both Autograd and multiprocess can be installed alongside Mici by runningpip install mici[autograd]. - JAX: if available JAX will be used to
automatically compute the required derivatives of the model functions (providing
they are specified using functions from the
jaxinterface). To sample chains parallel using JAX functions you also need to install multiprocess, though note due to JAX's use of multithreading which is incompatible with forking child processes, this can result in deadlock. Both JAX and multiprocess can be installed alongside Mici by runningpip install mici[jax]. Alternatively if using a free-threaded build of Python such as Python 3.13t, thread-based parallelism can be used instead which both avoids issues with usingpickleto serialize JAX objects and avoids deadlocks when forking processes. - SymNum: if available SymNum will be used to
automatically compute the required derivatives of the model functions (providing
they are specified using functions from the
symnum.numpyinterface). Symnum can be installed alongside Mici by runningpip install mici[symnum].
Why Mici?
Mici is named for Augusta 'Mici' Teller, who along with Arianna Rosenbluth developed the code for the MANIAC I computer used in the seminal paper Equations of State Calculations by Fast Computing Machines which introduced the first example of a Markov chain Monte Carlo method.
Related projects
Other Python packages for performing MCMC inference include PyMC, PyStan (the Python interface to Stan), Pyro / NumPyro, TensorFlow Probability, emcee, Sampyl and BlackJAX.
Unlike PyMC, PyStan, (Num)Pyro and TensorFlow Probability which are complete probabilistic programming frameworks including functionality for defining a probabilistic model / program, but like emcee, Sampyl and BlackJAX, Mici is solely focused on providing implementations of inference algorithms, with the user expected to be able to define at a minimum a function specifying the negative log (unnormalized) density of the distribution of interest.
Further while PyStan, (Num)Pyro and TensorFlow Probability all push the sampling loop into external compiled non-Python code, in Mici the sampling loop is run directly within Python. This has the consequence that for small models in which the negative log density of the target distribution and other model functions are cheap to evaluate, the interpreter overhead in iterating over the chains in Python can dominate the computational cost, making sampling much slower than packages which outsource the sampling loop to a efficient compiled implementation.
Overview of package
API documentation for the package is available
here. Mici provides both a high-level functional
interface and a more
customizable but verbose object-oriented interface. The functions
mici.sample_hmc_chains
and
mici.sample_constrained_hmc_chains
in the high-level interface, allow straightforward sampling from distributions on
unconstrained and constrained spaces respectively. These functions default to using an
adaptive HMC sampler which dynamically sets trajectory lengths, which should work well
for many problems.
Alternatively users can explicitly create instances of the underlying classes used to
implement MCMC sampling schemes in Mici to allow greater control over their behaviour.
The three main user-facing modules within the mici package are the systems,
integrators and samplers modules and you will generally need to create an instance
of one class from each module when using the object-oriented interface.
mici.systems -
Hamiltonian systems encapsulating model functions and their derivatives
EuclideanMetricSystem- systems with a metric on the position space with a constant matrix representation,GaussianEuclideanMetricSystem- systems in which the target distribution is defined by a density with respect to the standard Gaussian measure on the position space allowing analytically solving for flow corresponding to the quadratic components of Hamiltonian (Shahbaba et al., 2014),RiemannianMetricSystem- systems with a metric on the position space with a position-dependent matrix representation (Girolami and Calderhead, 2011),SoftAbsRiemannianMetricSystem- system with SoftAbs eigenvalue-regularized Hessian of negative log target density as metric matrix representation (Betancourt, 2013),DenseConstrainedEuclideanMetricSystem- Euclidean-metric system subject to holonomic constraints (Hartmann and Schütte, 2005; Brubaker, Salzmann and Urtasun, 2012; Lelièvre, Rousset and Stoltz, 2019) with a dense constraint function Jacobian matrix,
mici.integrators -
symplectic integrators for Hamiltonian dynamics
LeapfrogIntegrator- explicit leapfrog (Störmer-Verlet) integrator for separable Hamiltonian systems (Leimkulher and Reich, 2004),ImplicitLeapfrogIntegrator- implicit (or generalized) leapfrog integrator for Hamiltonian systems with non-separable component (Leimkulher and Reich, 2004),ImplicitMidpointIntegrator- implicit midpoint integrator for general Hamiltonian systems (Leimkulher and Reich, 2004),SymmetricCompositionIntegrator- family of symplectic integrators for Hamiltonians that can be split in to components with tractable flow maps, with specific two-, three- and four-stage instantations due to Blanes, Casas and Sanz-Serna (2014),ConstrainedLeapfrogIntegrator- constrained leapfrog integrator for Hamiltonian systems subject to holonomic constraints (Andersen, 1983; Leimkuhler and Reich, 1994).
mici.samplers - MCMC
samplers for peforming inference
StaticMetropolisHMC- static integration time Hamiltonian Monte Carlo with Metropolis accept step (Duane et al., 1987),RandomMetropolisHMC- random integration time Hamiltonian Monte Carlo with Metropolis accept step (Mackenzie, 1989),DynamicSliceHMC- dynamic integration time Hamiltonian Monte Carlo with slice sampling from trajectory, equivalent to the original 'NUTS' algorithm (Hoffman and Gelman, 2014).DynamicMultinomialHMC- dynamic integration time Hamiltonian Monte Carlo with multinomial sampling from trajectory, equivalent to the current default MCMC algorithm in Stan (Hoffman and Gelman, 2014; Betancourt, 2017).
Notebooks
The manifold MCMC methods implemented in Mici have been used in several research projects. Below links are provided to a selection of Jupyter notebooks associated with these projects as demonstrations of how to use Mici and to illustrate some of the settings in which manifold MCMC methods can be computationally advantageous.
| Manifold lifting: scaling MCMC to the vanishing noise regime | |
|---|---|
| Open non-interactive version with nbviewer |
|
| Open interactive version with Binder |
|
| Open interactive version with Google Colab |
|
| Manifold MCMC methods for inference in diffusion models | |
|---|---|
| Open non-interactive version with nbviewer |
|
| Open interactive version with Binder |
|
| Open interactive version with Google Colab |
|
Example: sampling on a torus

A simple complete example of using the package to compute approximate samples from a
distribution on a two-dimensional torus embedded in a three-dimensional space is given
below. The computed samples are visualized in the animation above. Here we use
SymNum to automatically construct functions to
calculate the required derivatives (gradient of negative log density of target
distribution and Jacobian of constraint function), sample four chains in parallel using
multiprocessing, use ArviZ to calculate
diagnostics and use Matplotlib to plot the samples.
```Python import arviz import matplotlib.pyplot as plt import mici import numpy as np import symnum import symnum.numpy as snp import matplotlib.animation as animation
Define fixed model parameters
R = 1.0 # toroidal radius ∈ (0, ∞) r = 0.5 # poloidal radius ∈ (0, R) α = 0.9 # density fluctuation amplitude ∈ [0, 1)
State dimension
dim_q = 3
Define constraint function such that the set {q : constr(q) == 0} is a torus
@symnum.numpify(dim_q) def constr(q): x, y, z = q return snp.array([((x2 + y2) ** 0.5 - R) ** 2 + z2 - r2])
Define negative log density for the target distribution on torus
(with respect to 2D 'area' measure for torus)
@symnum.numpify(dimq) def neglog_dens(q): x, y, z = q θ = snp.arctan2(y, x) ϕ = snp.arctan2(z, x / snp.cos(θ) - R) return snp.log1p(r * snp.cos(ϕ) / R) - snp.log1p(snp.sin(4 * θ) * snp.cos(ϕ) * α)
Seed a random number generator
rng = np.random.default_rng(seed=1234)
Sample initial positions on torus using parameterisation (θ, ϕ) ∈ [0, 2π)²
x, y, z = (R + r * cos(ϕ)) * cos(θ), (R + r * cos(ϕ)) * sin(θ), r * sin(ϕ)
nchain = 4 θinit, ϕinit = rng.uniform(0, 2 * np.pi, size=(2, nchain)) qinit = np.stack( [ (R + r * np.cos(ϕinit)) * np.cos(θinit), (R + r * np.cos(ϕinit)) * np.sin(θinit), r * np.sin(ϕinit), ], -1, )
Define function to extract variables to trace during sampling
def trace_func(state): x, y, z = state.pos return {"x": x, "y": y, "z": z}
Sample chains in parallel with 500 adaptive warm up iterations in which the
integrator step size is tuned, followed by 2000 non-adaptive iterations
finalstates, traces, stats = mici.sampleconstrainedhmcchains( nwarmupiter=500, nmainiter=2000, initstates=qinit, neglogdens=neglogdens, constr=constr, backend="symnum", seed=rng, nworker=nchain, tracefuncs=[trace_func], )
Print average accept probability and number of integrator steps per chain
for c in range(n_chain): print(f"Chain {c}:") print(f" Average accept prob. = {stats['accept_stat'][c].mean():.2f}") print(f" Average number steps = {stats['n_step'][c].mean():.1f}")
Print summary statistics and diagnostics computed using ArviZ
print(arviz.summary(traces))
Visualize concatentated chain samples as animated 3D scatter plot
fig, ax = plt.subplots( figsize=(4, 4), subplotkw={"projection": "3d", "projtype": "ortho"}, ) (points3d,) = ax.plot(*(np.concatenate(traces[k]) for k in "xyz"), ".", ms=0.5) ax.axis("off") for setlim in [ax.setxlim, ax.setylim, ax.setzlim]: setlim((-1, 1))
def update(i): angle = 45 * (np.sin(2 * np.pi * i / 60) + 1) ax.viewinit(elev=angle, azim=angle) return (points3d,)
anim = animation.FuncAnimation(fig, update, frames=60, interval=100) plt.show() ```
References
- Andersen, H.C., 1983. RATTLE: A “velocity”
version of the SHAKE algorithm for molecular dynamics calculations.
Journal of Computational Physics, 52(1), pp.24-34.
- Duane, S., Kennedy, A.D., Pendleton, B.J. and
Roweth, D., 1987. Hybrid Monte Carlo. Physics letters B, 195(2),
pp.216-222.
- Mackenzie, P.B., 1989. An improved
hybrid Monte Carlo method. Physics Letters B, 226(3-4), pp.369-371.
- Horowitz, A.M., 1991. A generalized
guided Monte Carlo algorithm. Physics Letters B, 268(CERN-TH-6172-91),
pp.247-252.
- Leimkuhler, B. and Reich, S., 1994.
Symplectic integration of constrained Hamiltonian systems. Mathematics of
Computation, 63(208), pp.589-605.
- Leimkuhler, B. and Reich, S., 2004.
Simulating Hamiltonian dynamics (Vol. 14). Cambridge University Press.
- Hartmann, C. and Schütte, C., 2005. A
constrained hybrid Monte‐Carlo algorithm and the problem of calculating the
free energy in several variables. ZAMM ‐ Journal of Applied Mathematics and
Mechanics, 85(10), pp.700-710.
- Girolami, M. and Calderhead, B., 2011.
Riemann manifold Langevin and Hamiltonian Monte Carlo methods. Journal of
the Royal Statistical Society: Series B (Statistical Methodology), 73(2), pp.123-214.
- Brubaker, M., Salzmann, M. and
Urtasun, R., 2012. A family of MCMC methods on implicitly defined
manifolds. In Artificial intelligence and statistics (pp. 161-172).
- Betancourt, M., 2013. A general metric
for Riemannian manifold Hamiltonian Monte Carlo. In Geometric science of
information (pp. 327-334).
- Hoffman, M.D. and Gelman, A., 2014. The
No-U-turn sampler: adaptively setting path lengths in Hamiltonian Monte
Carlo. Journal of Machine Learning Research, 15(1), pp.1593-1623.
- Shahbaba, B., Lan, S., Johnson, W.O. and
Neal, R.M., 2014. Split Hamiltonian Monte Carlo. Statistics and
Computing, 24(3), pp.339-349.
- Blanes, S., Casas, F., & Sanz-Serna, J. M., 2014.
Numerical integrators for the Hybrid Monte Carlo method.
SIAM Journal on Scientific Computing, 36(4), A1556-A1580.
- Betancourt, M., 2017. A conceptual
introduction to Hamiltonian Monte Carlo.
- Lelièvre, T., Rousset, M. and Stoltz, G., 2019. Hybrid Monte Carlo methods for sampling probability measures on
submanifolds. In Numerische Mathematik, 143(2), (pp.379-421).
Owner
- Name: Matt Graham
- Login: matt-graham
- Kind: user
- Company: University College London
- Website: http://matt-graham.github.io
- Repositories: 9
- Profile: https://github.com/matt-graham
Research data scientist at UCL
Citation (CITATION.cff)
cff-version: 1.2.0
message: "If you use this software, please cite it as below."
type: software
authors:
- family-names: Graham
given-names: Matthew M.
orcid: "https://orcid.org/0000-0001-9104-7960"
repository-code: "https://github.com/matt-graham/mici"
url: "https://matt-graham.github.io/mici"
title: "Mici: Manifold Markov chain Monte Carlo methods in Python"
keywords:
- "inference"
- "sampling"
- "Markov chain Monte Carlo"
- "MCMC"
- "Hamiltonian Monte Carlo"
- "HMC"
doi: 10.5281/zenodo.3517301
license: MIT
GitHub Events
Total
- Watch event: 19
- Delete event: 10
- Issue comment event: 3
- Push event: 25
- Pull request event: 20
- Create event: 8
Last Year
- Watch event: 19
- Delete event: 10
- Issue comment event: 3
- Push event: 25
- Pull request event: 20
- Create event: 8
Committers
Last synced: 9 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| Matt Graham | m****m@g****m | 442 |
| Matt Graham | m****m@e****k | 8 |
| dependabot[bot] | 4****] | 4 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 6 months ago
All Time
- Total issues: 5
- Total pull requests: 40
- Average time to close issues: 4 months
- Average time to close pull requests: about 11 hours
- Total issue authors: 5
- Total pull request authors: 2
- Average comments per issue: 1.0
- Average comments per pull request: 0.3
- Merged pull requests: 34
- Bot issues: 0
- Bot pull requests: 6
Past Year
- Issues: 0
- Pull requests: 35
- Average time to close issues: N/A
- Average time to close pull requests: about 12 hours
- Issue authors: 0
- Pull request authors: 2
- Average comments per issue: 0
- Average comments per pull request: 0.26
- Merged pull requests: 30
- Bot issues: 0
- Bot pull requests: 6
Top Authors
Issue Authors
- twiecki (1)
- sethaxen (1)
- sanjeevmk (1)
- junpenglao (1)
Pull Request Authors
- matt-graham (38)
- dependabot[bot] (10)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 1
-
Total downloads:
- pypi 125 last-month
- Total dependent packages: 0
- Total dependent repositories: 2
- Total versions: 15
- Total maintainers: 1
pypi.org: mici
MCMC samplers based on simulating Hamiltonian dynamics on a manifold.
- Documentation: https://mici.readthedocs.io/
- License: MIT License
-
Latest release: 0.3.0
published over 1 year ago
Rankings
Maintainers (1)
Dependencies
- arviz >=0.8
- autograd >=1.3
- matplotlib >=3.2
- multiprocess >=0.70
- numpy >=1.17
- scipy >=1.1
- xxhash >=1.4.4
- numpy >=1.17