nutpie is a high-performance library designed for Bayesian inference, that provides efficient sampling algorithms for probabilistic models. It can sample models that are defined in PyMC or Stan (numpyro and custom hand-coded likelihoods with gradient are coming soon). It provides:
- Faster sampling than either the PyMC or Stan default samplers. (An average ~2x speedup on
posteriordbcompared to Stan) - All the diagnostic information of PyMC and Stan and some more.
- GPU support for PyMC models through
jax. - A more informative progress bar.
- Access to the incomplete trace during sampling.
- Experimental normalizing flow adaptation for more efficient sampling of difficult posteriors.
For more details on the algorithms used in nutpie, see the paper Preconditioning Hamiltonian Monte Carlo by minimizing Fisher Divergence. If you use nutpie in your research, please see the citation instructions.
Quickstart: PyMC
Install nutpie with pip, uv, pixi, or conda:
For usage with pymc:
# One of
pip install "nutpie[pymc]"
uv add "nutpie[pymc]"
pixi add nutpie pymc numba
conda install -c conda-forge nutpie pymc numbaAnd then sample with
import nutpie
import pymc as pm
with pm.Model() as model:
mu = pm.Normal("mu", mu=0, sigma=1)
obs = pm.Normal("obs", mu=mu, sigma=1, observed=[1, 2, 3])
compiled = nutpie.compile_pymc_model(model)
trace = nutpie.sample(compiled)Sampler Progress
Total Chains: 6
Active Chains: 0
Finished Chains: 6
Sampling for now
Estimated Time to Completion: now
| Progress | Draws | Divergences | Step Size | Gradients/Draw |
|---|---|---|---|---|
| 1400 | 0 | 1.19 | 1 | |
| 1400 | 0 | 1.36 | 1 | |
| 1400 | 0 | 1.24 | 1 | |
| 1400 | 0 | 1.35 | 3 | |
| 1400 | 0 | 1.30 | 3 | |
| 1400 | 0 | 1.25 | 3 |
For more information, see the detailed PyMC usage guide.
Quickstart: Stan
Stan needs access to a compiler toolchain, for which you can find installation instructions here. You can then install nutpie through pip or uv:
# One of
pip install "nutpie[stan]"
uv add "nutpie[stan]"import nutpie
model = """
data {
int<lower=0> N;
vector[N] y;
}
parameters {
real mu;
}
model {
mu ~ normal(0, 1);
y ~ normal(mu, 1);
}
"""
compiled = (
nutpie
.compile_stan_model(code=model)
.with_data(N=3, y=[1, 2, 3])
)
trace = nutpie.sample(compiled)BridgeStan not found at location specified by $BRIDGESTAN environment variable, downloading version 2.8.0 to /home/runner/.bridgestan/bridgestan-2.8.0
Done!
Sampler Progress
Total Chains: 6
Active Chains: 0
Finished Chains: 6
Sampling for now
Estimated Time to Completion: now
| Progress | Draws | Divergences | Step Size | Gradients/Draw |
|---|---|---|---|---|
| 1400 | 0 | 1.38 | 3 | |
| 1400 | 0 | 1.28 | 1 | |
| 1400 | 0 | 1.25 | 3 | |
| 1400 | 0 | 1.32 | 1 | |
| 1400 | 0 | 1.40 | 1 | |
| 1400 | 0 | 1.25 | 1 |
For more information, see the detailed Stan usage guide.