Sample Stan or PyMC models
Project description
nutpie: A fast sampler for bayesian posteriors
Installation
nutpie can be installed using conda or mamba from conda-forge with
mamba install -c conda-forge nutpie pymc
Or using pip:
pip install nutpie
To install it from source, install a rust compiler and maturin and then
maturin develop --release
If you want to use the nightly simd implementation for some of the math functions,
switch to rust nightly and then install with the simd_support
feature in then
nutpie directory:
rustup override set nightly
maturin develop --release --features=simd_support
Usage with PyMC
First, PyMC and numba need to be installed, for example using
mamba install pymc numba
We need to create a model:
import pymc as pm
import numpy as np
import nutpie
import pandas as pd
import seaborn as sns
# Load the radon dataset
data = pd.read_csv(pm.get_data("radon.csv"))
data["log_radon"] = data["log_radon"].astype(np.float64)
county_idx, counties = pd.factorize(data.county)
coords = {"county": counties, "obs_id": np.arange(len(county_idx))}
# Create a simple hierarchical model for the radon dataset
with pm.Model(coords=coords, check_bounds=False) as pymc_model:
intercept = pm.Normal("intercept", sigma=10)
# County effects
raw = pm.ZeroSumNormal("county_raw", dims="county")
sd = pm.HalfNormal("county_sd")
county_effect = pm.Deterministic("county_effect", raw * sd, dims="county")
# Global floor effect
floor_effect = pm.Normal("floor_effect", sigma=2)
# County:floor interaction
raw = pm.ZeroSumNormal("county_floor_raw", dims="county")
sd = pm.HalfNormal("county_floor_sd")
county_floor_effect = pm.Deterministic(
"county_floor_effect", raw * sd, dims="county"
)
mu = (
intercept
+ county_effect[county_idx]
+ floor_effect * data.floor.values
+ county_floor_effect[county_idx] * data.floor.values
)
sigma = pm.HalfNormal("sigma", sigma=1.5)
pm.Normal(
"log_radon", mu=mu, sigma=sigma, observed=data.log_radon.values, dims="obs_id"
)
We then compile this model and sample form the posterior:
compiled_model = nutpie.compile_pymc_model(pymc_model)
trace_pymc = nutpie.sample(compiled_model)
trace_pymc
now contains an arviz InferenceData
object, including sampling
statistics and the posterior of the variables defined above.
Usage with Stan
In order to sample from stan model, bridgestan
needs to be installed.
A pip package is available, but right now this can not be installed using conda.
pip install bridgestan
When we install nutpie with pip, we can also specify that we want optional dependencies for Stan models using
pip install 'bridgestan[stan]'
In addition, a C++ compiler needs to be available. For details see the stan docs.
We can then compile a Stan model, and sample using nutpie:
import nutpie
code = """
data {
real mu;
}
parameters {
real x;
}
model {
x ~ normal(mu, 1);
}
"""
compiled = nutpie.compile_stan_model(code=code)
# Provide data
compiled = compiled.with_data(mu=3.)
trace = nutpie.sample(compiled)
Advantages
nutpie uses nuts-rs
, a library written in rust, that implements NUTS as in
pymc and stan, but with a slightly different mass matrix tuning method as
those. It often produces a higher effective sample size per gradient
evaluation, and tends to converge faster and with fewer gradient evaluation.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file nutpie-0.8.1.tar.gz
.
File metadata
- Download URL: nutpie-0.8.1.tar.gz
- Upload date:
- Size: 165.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.2.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a4019233a0402f38ea897793dccf944f449df608ac8113d7ea4cf8512d371ba2 |
|
MD5 | b80a3a4155b1f2cb19a0cb10f94db418 |
|
BLAKE2b-256 | a0895311b3f53aff58dfc15145da92217bbe9b87447dcb83b7f2540e10f3040c |
File details
Details for the file nutpie-0.8.1-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
.
File metadata
- Download URL: nutpie-0.8.1-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 3.7 MB
- Tags: PyPy, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.2.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d212eddbe4e7e6330303c5949daab451ca27df560f8341ede08a2fbea72ec56f |
|
MD5 | d56f53b357c108b9bc240abee5aa9bb3 |
|
BLAKE2b-256 | 83127d1f4ef48c1a640d2d1b46a3d9592ed750fce07cfb98c9b9499c8bd7b35f |
File details
Details for the file nutpie-0.8.1-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
.
File metadata
- Download URL: nutpie-0.8.1-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 3.7 MB
- Tags: PyPy, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.2.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a3f32df4c57f98308377172b6d5748c22f30a73fef04a835f2c8bf77764aa5d |
|
MD5 | 763d2660778f2a18ca123879efd848c7 |
|
BLAKE2b-256 | 614aee5615c3d85678ee439f261a65339266ac3e12ab233538964b0a5ec5f270 |
File details
Details for the file nutpie-0.8.1-cp312-cp312-manylinux_2_28_x86_64.whl
.
File metadata
- Download URL: nutpie-0.8.1-cp312-cp312-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 3.7 MB
- Tags: CPython 3.12, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.2.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13312d1b0b034b26315d5c8d8d4f47bb51eafcba35c933fc4aed0ce1026db56f |
|
MD5 | 35b4172da5e09edb16a245556e0a6be8 |
|
BLAKE2b-256 | bbac7a704b7c8cb623e10ec4d9fcc98b691c1b096d02080e169980cd8cdf6d12 |
File details
Details for the file nutpie-0.8.1-cp311-none-win_amd64.whl
.
File metadata
- Download URL: nutpie-0.8.1-cp311-none-win_amd64.whl
- Upload date:
- Size: 497.9 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.2.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cbbbf8d5fe37049ae221fb9b0644752a8d8ccf0a890c0af54ce8bd4a837bb4cd |
|
MD5 | 1c0258db7cfa16f12797a81bb06a8efe |
|
BLAKE2b-256 | 9bab43553882ec597b1126d3ca3ab134e8dd1cfe5427d495b05e0f272046335b |
File details
Details for the file nutpie-0.8.1-cp311-cp311-manylinux_2_28_x86_64.whl
.
File metadata
- Download URL: nutpie-0.8.1-cp311-cp311-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 3.7 MB
- Tags: CPython 3.11, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.2.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc833000d9866a147a29248de25bffdf0cc2837f74774fbd1724258c32a4b7c2 |
|
MD5 | b037843a901a19a68b80e929b60bd825 |
|
BLAKE2b-256 | f43eee9c3c5c7838b0776aec36a6d7adce349afc69500304d1c4b22f7bb6a2b7 |
File details
Details for the file nutpie-0.8.1-cp311-cp311-macosx_11_0_arm64.whl
.
File metadata
- Download URL: nutpie-0.8.1-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 660.0 kB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.2.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd93f09ba618acc4a1e65fe217ac558e29797eef7a85bdda9200533bbed14644 |
|
MD5 | 1cddaba74989889f629bd952399ada4d |
|
BLAKE2b-256 | 2d6c8c6e9d883cb1d0c89ec057b4dee0b7d711d97aee7167b9a683504e6e9682 |
File details
Details for the file nutpie-0.8.1-cp311-cp311-macosx_10_7_x86_64.whl
.
File metadata
- Download URL: nutpie-0.8.1-cp311-cp311-macosx_10_7_x86_64.whl
- Upload date:
- Size: 689.2 kB
- Tags: CPython 3.11, macOS 10.7+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.2.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0541352541294fe723d876dd85f7d6c6ac3a85acc9e3caf00759f0be75ae2be6 |
|
MD5 | 55deb66c6a9d881e83f59e46a52c3ace |
|
BLAKE2b-256 | b13dd6b9c3430478243b98eaace10aecfe05934737674ddcbfca5bb9cf2c7d1f |
File details
Details for the file nutpie-0.8.1-cp310-none-win_amd64.whl
.
File metadata
- Download URL: nutpie-0.8.1-cp310-none-win_amd64.whl
- Upload date:
- Size: 497.8 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.2.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7de91d0b758e55b9d1d3ad285a0b924cba8d6b10c3427ae13b4c3b546d154b8 |
|
MD5 | 8af96caa1e699d9a2b108703c6b2d054 |
|
BLAKE2b-256 | 55711c7c9cb197e77ff739a424c016c83e567a712741af7691e7170b943b0e6e |
File details
Details for the file nutpie-0.8.1-cp310-cp310-manylinux_2_28_x86_64.whl
.
File metadata
- Download URL: nutpie-0.8.1-cp310-cp310-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 3.7 MB
- Tags: CPython 3.10, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.2.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 14243a0c24263bd1d5b7a8d72a89a1bb1ff93b4ea0950c6996ddd787846563fb |
|
MD5 | 9cd87deda8e6f69ab38a6f94d0bee9d4 |
|
BLAKE2b-256 | 0f0ef3ffd4bdf621ce50c8a2588e211668d80d04d06cc580d193c07fbf0c94dd |
File details
Details for the file nutpie-0.8.1-cp310-cp310-macosx_11_0_arm64.whl
.
File metadata
- Download URL: nutpie-0.8.1-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 660.0 kB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.2.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a30b9b0de3cf7b5ba148458dc62faee1d8616ece9c76ae8740b57b4850b09048 |
|
MD5 | 4329d27df86dfdaf391d232115d20ee0 |
|
BLAKE2b-256 | 16a25541184863a558c859077d5a25fdde1bd25bf39b4c91f793e151fb2ba220 |
File details
Details for the file nutpie-0.8.1-cp310-cp310-macosx_10_7_x86_64.whl
.
File metadata
- Download URL: nutpie-0.8.1-cp310-cp310-macosx_10_7_x86_64.whl
- Upload date:
- Size: 689.2 kB
- Tags: CPython 3.10, macOS 10.7+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.2.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0cd289515dc57cba64d4fe0f3dc2d937f9653ab35f1e674046222b12f7d7f5ad |
|
MD5 | dd573274b8b8cafbc7d5309c3611292b |
|
BLAKE2b-256 | 952a82e833d60d457d158f20035a30d81eaa058b77cce8975dbfddcfb242cf6f |
File details
Details for the file nutpie-0.8.1-cp39-none-win_amd64.whl
.
File metadata
- Download URL: nutpie-0.8.1-cp39-none-win_amd64.whl
- Upload date:
- Size: 498.1 kB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.2.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7564ec158c28b16d7d8fb3c1f419cc9f6503a774a16d2ec49cd46db4051e422 |
|
MD5 | eb646d9d4e25816c87c5f8bd3e95c7b0 |
|
BLAKE2b-256 | c8fbf501d16a4fab1963ea1d3a1504b455636d481e9b541ce429771f690a95cf |
File details
Details for the file nutpie-0.8.1-cp39-cp39-manylinux_2_28_x86_64.whl
.
File metadata
- Download URL: nutpie-0.8.1-cp39-cp39-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 3.7 MB
- Tags: CPython 3.9, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.2.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0bca69dbf683fb87d1a627cdaf6399bedfd5db54ee1dc9be4cfb90118750de3 |
|
MD5 | 6389d900c07427cb4e15e393015d24c2 |
|
BLAKE2b-256 | c9bbcb08b4ab76ede33e9bc16e577851157b9001f1916893010e2f9f7dd5661e |