Skip to main content

Sample Stan or PyMC models

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nutpie-0.16.10.tar.gz (721.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

nutpie-0.16.10-cp314-cp314-win_amd64.whl (9.7 MB view details)

Uploaded CPython 3.14Windows x86-64

nutpie-0.16.10-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (9.2 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

nutpie-0.16.10-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (8.5 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

nutpie-0.16.10-cp314-cp314-macosx_11_0_arm64.whl (9.1 MB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

nutpie-0.16.10-cp314-cp314-macosx_10_12_x86_64.whl (9.8 MB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

nutpie-0.16.10-cp313-cp313-win_amd64.whl (9.7 MB view details)

Uploaded CPython 3.13Windows x86-64

nutpie-0.16.10-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (9.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

nutpie-0.16.10-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (8.5 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

nutpie-0.16.10-cp313-cp313-macosx_11_0_arm64.whl (9.1 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

nutpie-0.16.10-cp313-cp313-macosx_10_12_x86_64.whl (9.8 MB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

nutpie-0.16.10-cp312-cp312-win_amd64.whl (9.7 MB view details)

Uploaded CPython 3.12Windows x86-64

nutpie-0.16.10-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (9.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

nutpie-0.16.10-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (8.5 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

nutpie-0.16.10-cp312-cp312-macosx_11_0_arm64.whl (9.1 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

nutpie-0.16.10-cp312-cp312-macosx_10_12_x86_64.whl (9.8 MB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

File details

Details for the file nutpie-0.16.10.tar.gz.

File metadata

  • Download URL: nutpie-0.16.10.tar.gz
  • Upload date:
  • Size: 721.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.13.3

File hashes

Hashes for nutpie-0.16.10.tar.gz
Algorithm Hash digest
SHA256 6378830bc115fb34bcd0c92d1b5f34b567d13913e3fc408315c50ec803e8f3a9
MD5 78414e4bdd7f7adee67c889f03be92e9
BLAKE2b-256 3b2aeb4550f2d33f82d9a195892bf45e65a1bb9f9d252c7bf24db8946cebd43b

See more details on using hashes here.

File details

Details for the file nutpie-0.16.10-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.10-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 7901fea4c7292a808cc2d2cef8a72717f926610d907308db61d622a6fcfbd484
MD5 5ab3c849ab3216ced62aaf45aba7a0fe
BLAKE2b-256 04623d7bc6d919acc58114f7c6caa804ec6f242675694bf58edfcc8405329726

See more details on using hashes here.

File details

Details for the file nutpie-0.16.10-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.10-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f37d6d838c4624db019a4b34018ee78d1b43d06de1a65b65b3599dde8c21f8a3
MD5 2114260cc7caf8c507a6e6a31db76652
BLAKE2b-256 8349ea78fb66dbd817f855fd726e032c176d440ee4f6af3638a208037a0f1d17

See more details on using hashes here.

File details

Details for the file nutpie-0.16.10-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.10-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e63191b2822d8e39dbf86e5a3d601804201773742835983b06a139ee52cfb931
MD5 0b0266c198261408418b1c8962b774df
BLAKE2b-256 e4d6f1d222171cdcb3ac751bb054aefd3448196e72c6f65c55c7a8765b659f0b

See more details on using hashes here.

File details

Details for the file nutpie-0.16.10-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.10-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c25fd30004046ad6e995376634ab9cf31afffb367f0c1a24bd41bed4ee83b458
MD5 190d77cfaf57646494de8237b7e94474
BLAKE2b-256 2a8ccaeadb973cb4cc93021a3e93156ff358c1afe86eadeb61a732e67543abd8

See more details on using hashes here.

File details

Details for the file nutpie-0.16.10-cp314-cp314-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.10-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 0a0b9ab5dd4fffe595dc1d58452e9891bb65fd7aec30430da5b6db32b303a6e9
MD5 01b5a2f6f9c7b3d96d1eb1461e448010
BLAKE2b-256 246c9f08eda02b1c6b93cac2230ae1021a077ce25c4a7708237fd201e6232b9c

See more details on using hashes here.

File details

Details for the file nutpie-0.16.10-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.10-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 2cd40fd37999900d62da1732e628382ae95f96fcc75906ac54346d80b8145c75
MD5 6a9e79298bcb8eef656413b7475af99c
BLAKE2b-256 a3355ba5df85b991180e28416362c38ac5b00ebd5ee396f1aba07c12f4cd1626

See more details on using hashes here.

File details

Details for the file nutpie-0.16.10-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.10-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e6d444a41e7dcc068240b94714c439ba66320cd3dc405dce101d2f95aa964fff
MD5 9e13afbe2d7f48af223f756d587ed8f2
BLAKE2b-256 51b3e67044d1600a849ebaa6e0bcfcffb8925a7e4188dc2b03fa20c506e0bb7c

See more details on using hashes here.

File details

Details for the file nutpie-0.16.10-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.10-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 295d5751d44f7527d79eec36f6bf64f9fe9669b935fc0ff6dd444be629b65c4f
MD5 34ebd84637af1f4a1fda5e7e3577d415
BLAKE2b-256 978e9861d653c041c52467bdf195c951c40fb5908b6ebab6c793ddb1c651478b

See more details on using hashes here.

File details

Details for the file nutpie-0.16.10-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.10-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d9b77fc835af2c2ad197bf1e14e07bb0f4ee034f85ee15895b131778eae61ebe
MD5 818d56a3d704e868252982f6d3175503
BLAKE2b-256 b733bc6496f986255ea0cc91c4b72c0e1130c28e97771a9b157ebfbe670d6133

See more details on using hashes here.

File details

Details for the file nutpie-0.16.10-cp313-cp313-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.10-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 70336b7d62f29d24cea0ae10b682354ab5486cb1772056c15eb0196f9bf700e6
MD5 8439e789316bed540b265feac5da2b31
BLAKE2b-256 37e22e55b26b02ebad9ab58cf23423bac9acc6263962f7d4ae23477c8d4b71e0

See more details on using hashes here.

File details

Details for the file nutpie-0.16.10-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.10-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d50811bfe46db06e835b13d22e44a6769ffd5b8a206a2d78772f261a9533b74d
MD5 4264fdca7b174885f5189f4e0b4ab5a4
BLAKE2b-256 5661d678fae74772fde0cb89e4830abd18a46323a498910a31753fb4ff47c34f

See more details on using hashes here.

File details

Details for the file nutpie-0.16.10-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.10-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5b130b51d99d85ce830a5a142fe3576e9a521e02a6133aad3747c075549c63a3
MD5 5edc18328ae22aec790a1939bba19ecb
BLAKE2b-256 66a2fe668ef55c43714ab3a2e5439ded61e82ac48208bfeac17911f5479bd044

See more details on using hashes here.

File details

Details for the file nutpie-0.16.10-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.10-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 ed81cd7e51a0f146f8f33bb55c8ce5c0a1ba30991fabe774d956e68209bf5a7d
MD5 2c8dac267e75165bd0a621aa2fc692b3
BLAKE2b-256 41d5a4923db738c3f94accf40670a6450976aab4cfb9e34ea8412c71191cfb86

See more details on using hashes here.

File details

Details for the file nutpie-0.16.10-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.10-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c71ebe6ada63445acd2c467bb5575a47b8706897969f3aad048e51abbb3d9193
MD5 b1afe7e0d53d39f1ebe0fc3260895801
BLAKE2b-256 21fc36f09d420278078f4533c1f02b63e9a3313855668c4c9568041ae65f27c4

See more details on using hashes here.

File details

Details for the file nutpie-0.16.10-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.10-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b79c405b12031e6a8ee37301071037ca5ff9e2284b67d1a3038ed80f68955ff9
MD5 a4d0d7f957417eabbb540d960cc564d7
BLAKE2b-256 61afa036edde7dc4829a2ba34c5393f890ab61aab14508c7130cd7c9b49c11bf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page