Skip to main content

nutpie: A fast sampler for Bayesian posteriors

The nutpie package provides a fast NUTS sampler for PyMC and Stan models.

See the documentation for more details.

Installation

nutpie can be installed using Conda or Mamba from conda-forge with

mamba install -c conda-forge nutpie

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 -c conda-forge 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.

We can also control the sampler in a non-blocking way:

# The sampler will now run the the background
sampler = nutpie.sample(compiled_model, blocking=False)

# Pause and resume the sampling
sampler.pause()
sampler.resume()

# Wait for the sampler to finish (up to timeout seconds)
sampler.wait(timeout=0.1)
# Note that not passing any timeout to `wait` will
# wait until the sampler finishes, then return the InferenceData object:
idata = sampler.wait()

# or we can also abort the sampler (and return the incomplete trace)
incomplete_trace = sampler.abort()

# or cancel and discard all progress:
sampler.cancel()

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 'nutpie[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.

Metadata

Release files for nutpie 0.16.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nutpie 0.16.11
File Size Uploaded
nutpie-0.16.11.tar.gz 721.8 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for nutpie 0.16.11
File
nutpie-0.16.11-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details
nutpie-0.16.11-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.17+ x86-64 Details
nutpie-0.16.11-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.14 CPython 3.14 Linux glibc 2.17+ ARM64 Details
nutpie-0.16.11-cp314-cp314-macosx_11_0_arm64.whl CPython 3.14 CPython 3.14 macOS 11.0+ ARM64 Details
nutpie-0.16.11-cp314-cp314-macosx_10_12_x86_64.whl CPython 3.14 CPython 3.14 macOS 10.12+ x86-64 Details
nutpie-0.16.11-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details
nutpie-0.16.11-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.17+ x86-64 Details
nutpie-0.16.11-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.13 CPython 3.13 Linux glibc 2.17+ ARM64 Details
nutpie-0.16.11-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details
nutpie-0.16.11-cp313-cp313-macosx_10_12_x86_64.whl CPython 3.13 CPython 3.13 macOS 10.12+ x86-64 Details
nutpie-0.16.11-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
nutpie-0.16.11-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.17+ x86-64 Details
nutpie-0.16.11-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.12 CPython 3.12 Linux glibc 2.17+ ARM64 Details
nutpie-0.16.11-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
nutpie-0.16.11-cp312-cp312-macosx_10_12_x86_64.whl CPython 3.12 CPython 3.12 macOS 10.12+ x86-64 Details

Total release size: 139.6 MB

Release files / nutpie-0.16.11.tar.gz

Download URL nutpie-0.16.11.tar.gz
Size 721.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e00bf6c004a345ca95d50a57aa794b1b6a2c39270987da131011e7f672589487
BLAKE2b-256 checksum
How to use checksums
fe11f53d7618c4e3c54a9d5c410ac42711499f5a2554c28a4b9f03c4d0439d5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.14.1

Release files / nutpie-0.16.11-cp314-cp314-win_amd64.whl

Download URL nutpie-0.16.11-cp314-cp314-win_amd64.whl
Size 9.7 MB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
f6b7e49894882fb0d5949be3fdb14c4ec990c9f62ecbf3310060ee53ae8f3530
BLAKE2b-256 checksum
How to use checksums
795897c6c2976bb7a483cb8314ddbfbe298fd1957a5d128e70c71ba8e9f34c78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.14.1

Release files / nutpie-0.16.11-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL nutpie-0.16.11-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 9.2 MB
Tags CPython 3.14 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
abf6a66c895ccaf884d33aec4093df8af1947c3ca01105bcdf4aaffaa0e89972
BLAKE2b-256 checksum
How to use checksums
cede90fcb6511f06980c639e5c4ce905c4abb079ce3118758e4b0ce1aff910a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.14.1

Release files / nutpie-0.16.11-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL nutpie-0.16.11-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 8.5 MB
Tags CPython 3.14 Linux glibc 2.17+ ARM64
SHA-256 checksum
How to use checksums
714759c5a6f2bd33e19c96412cedc4c29afe333c1023b8ccb0c7ba07ccbdd1a0
BLAKE2b-256 checksum
How to use checksums
b90c9f44539e11074cd2a366191fbf5b3c18764c122ac85d19b54e622b6bd4ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.14.1

Release files / nutpie-0.16.11-cp314-cp314-macosx_11_0_arm64.whl

Download URL nutpie-0.16.11-cp314-cp314-macosx_11_0_arm64.whl
Size 9.1 MB
Tags CPython 3.14 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
740c7b73591583f0383cd1cca63d36abd1d14a3ad2adb3afed07496b09cc4b54
BLAKE2b-256 checksum
How to use checksums
16169ed76db7fd208c74dd9613ac2c0c9b062d409ae5176122cb904ace61abe2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.14.1

Release files / nutpie-0.16.11-cp314-cp314-macosx_10_12_x86_64.whl

Download URL nutpie-0.16.11-cp314-cp314-macosx_10_12_x86_64.whl
Size 9.8 MB
Tags CPython 3.14 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
d080abe9510591a4d52cc8095544d6bbc3ec6a54de76ed1771c212c7e6f1acfe
BLAKE2b-256 checksum
How to use checksums
7f18513b1f2bec678bf739447a155414760e374dc772e65510b057768de3c809
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.14.1

Release files / nutpie-0.16.11-cp313-cp313-win_amd64.whl

Download URL nutpie-0.16.11-cp313-cp313-win_amd64.whl
Size 9.7 MB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
3c93116eae6a19d8436408a6b6745fd10c778707552bf3be4c3db4118f308c63
BLAKE2b-256 checksum
How to use checksums
7042a27209862624bcd96bbcf06ab2b09f7eedd9190a8094737ae5ec2d8cdc37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.14.1

Release files / nutpie-0.16.11-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL nutpie-0.16.11-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 9.2 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
cf006a9f1372dd21147c0ca511702658ac094155b52223ac00ce1f17a800d5d8
BLAKE2b-256 checksum
How to use checksums
120bf772965ad238214ade1d7b0dcf017dd1320a3db007ac2d4f2a0933a14016
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.14.1

Release files / nutpie-0.16.11-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL nutpie-0.16.11-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 8.5 MB
Tags CPython 3.13 Linux glibc 2.17+ ARM64
SHA-256 checksum
How to use checksums
0534b9ca2f840d1f5448d1abb7b00d109195e54e3c1b66fd48b204d741e6a3e5
BLAKE2b-256 checksum
How to use checksums
6b785ae182d5e9bdbb0a83d55bf4c4c0241bf1b1d76cd8f454d64cb2f93edadf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.14.1

Release files / nutpie-0.16.11-cp313-cp313-macosx_11_0_arm64.whl

Download URL nutpie-0.16.11-cp313-cp313-macosx_11_0_arm64.whl
Size 9.1 MB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
d8586bb001198b2944547ebcf7b60869af41629476478d3d015dc6ff05afcd20
BLAKE2b-256 checksum
How to use checksums
b1b9efc6c43aca0fad920e6ea60f1b8c9bb6c47f4adcc5e5438a7d6c0c406cbe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.14.1

Release files / nutpie-0.16.11-cp313-cp313-macosx_10_12_x86_64.whl

Download URL nutpie-0.16.11-cp313-cp313-macosx_10_12_x86_64.whl
Size 9.8 MB
Tags CPython 3.13 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
946e8fa1e0c35dd5d90ac6a018639f7f0b9750918a103bf04c4922b39f6ad463
BLAKE2b-256 checksum
How to use checksums
00045d6ae4432507c5e214028ff86983dd894985fcc002d208b0cb5c91b14f52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.14.1

Release files / nutpie-0.16.11-cp312-cp312-win_amd64.whl

Download URL nutpie-0.16.11-cp312-cp312-win_amd64.whl
Size 9.7 MB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
5d6d501066e111ca021a55de03ac080122794b95750e32a62ef9fb3fae77bea0
BLAKE2b-256 checksum
How to use checksums
8b25b27fe3521b50254cb4841ad19508501388a963cc4bd3446e0e84f51fc9d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.14.1

Release files / nutpie-0.16.11-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL nutpie-0.16.11-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 9.2 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
39c383b2638368b20ce85319458ff914dab0bc5e87588ffec68879719e3c7dcf
BLAKE2b-256 checksum
How to use checksums
4a8e0796cf8c12799a1ea074171dbd97e875cc8a75af20b7600dc8744a54ea47
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.14.1

Release files / nutpie-0.16.11-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL nutpie-0.16.11-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 8.5 MB
Tags CPython 3.12 Linux glibc 2.17+ ARM64
SHA-256 checksum
How to use checksums
fac130a35a9c044d1ec8dce909b7ffbecd17d0bc3498250be658abad12f0cdc9
BLAKE2b-256 checksum
How to use checksums
8f7413203b2f76a95a5da04f194fa397b47501682b953582dcaac8539cd3a381
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.14.1

Release files / nutpie-0.16.11-cp312-cp312-macosx_11_0_arm64.whl

Download URL nutpie-0.16.11-cp312-cp312-macosx_11_0_arm64.whl
Size 9.1 MB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
33fca4a297b06881945c75bab0e6466d5b428d4fd7955c6f90c62f39fa277952
BLAKE2b-256 checksum
How to use checksums
b09db6843a1b5c98b52024e671b0e11eb2e8583de016c5973f15eb5bff87a242
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.14.1

Release files / nutpie-0.16.11-cp312-cp312-macosx_10_12_x86_64.whl

Download URL nutpie-0.16.11-cp312-cp312-macosx_10_12_x86_64.whl
Size 9.8 MB
Tags CPython 3.12 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
4c888815e35936a510570dc31bca00ad716aec035b8d5975b00fd056c2cb431b
BLAKE2b-256 checksum
How to use checksums
c128f112ef823626cab9cf2359852dd52da47951b6102468a3f799c2f29809a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.14.1
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page