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.11.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.11-cp314-cp314-win_amd64.whl (9.7 MB view details)

Uploaded CPython 3.14Windows x86-64

nutpie-0.16.11-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.11-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.11-cp314-cp314-macosx_11_0_arm64.whl (9.1 MB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

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

Uploaded CPython 3.14macOS 10.12+ x86-64

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

Uploaded CPython 3.13Windows x86-64

nutpie-0.16.11-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.11-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.11-cp313-cp313-macosx_11_0_arm64.whl (9.1 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.12+ x86-64

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

Uploaded CPython 3.12Windows x86-64

nutpie-0.16.11-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.11-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.11-cp312-cp312-macosx_11_0_arm64.whl (9.1 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

nutpie-0.16.11-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.11.tar.gz.

File metadata

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

File hashes

Hashes for nutpie-0.16.11.tar.gz
Algorithm Hash digest
SHA256 e00bf6c004a345ca95d50a57aa794b1b6a2c39270987da131011e7f672589487
MD5 6fe2c30f34b0c409e2d88e9494a37d48
BLAKE2b-256 fe11f53d7618c4e3c54a9d5c410ac42711499f5a2554c28a4b9f03c4d0439d5e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.11-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 f6b7e49894882fb0d5949be3fdb14c4ec990c9f62ecbf3310060ee53ae8f3530
MD5 f78423b22d6aebed55f4dba1e378e69d
BLAKE2b-256 795897c6c2976bb7a483cb8314ddbfbe298fd1957a5d128e70c71ba8e9f34c78

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.11-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 abf6a66c895ccaf884d33aec4093df8af1947c3ca01105bcdf4aaffaa0e89972
MD5 5238cb096d4bb94e9dbd90454c854e63
BLAKE2b-256 cede90fcb6511f06980c639e5c4ce905c4abb079ce3118758e4b0ce1aff910a4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.11-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 714759c5a6f2bd33e19c96412cedc4c29afe333c1023b8ccb0c7ba07ccbdd1a0
MD5 1ea978079172da293cc2d6cf66b3feaa
BLAKE2b-256 b90c9f44539e11074cd2a366191fbf5b3c18764c122ac85d19b54e622b6bd4ae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.11-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 740c7b73591583f0383cd1cca63d36abd1d14a3ad2adb3afed07496b09cc4b54
MD5 657fa7b9340677433a958aefb655374c
BLAKE2b-256 16169ed76db7fd208c74dd9613ac2c0c9b062d409ae5176122cb904ace61abe2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.11-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d080abe9510591a4d52cc8095544d6bbc3ec6a54de76ed1771c212c7e6f1acfe
MD5 87efb3b8db1b910950fb2c7d77d233db
BLAKE2b-256 7f18513b1f2bec678bf739447a155414760e374dc772e65510b057768de3c809

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.11-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 3c93116eae6a19d8436408a6b6745fd10c778707552bf3be4c3db4118f308c63
MD5 a429a803ce9d3cc4fbc6b163d4aaa0d6
BLAKE2b-256 7042a27209862624bcd96bbcf06ab2b09f7eedd9190a8094737ae5ec2d8cdc37

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.11-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cf006a9f1372dd21147c0ca511702658ac094155b52223ac00ce1f17a800d5d8
MD5 bc60caa2035545223a2269ccba9fb10f
BLAKE2b-256 120bf772965ad238214ade1d7b0dcf017dd1320a3db007ac2d4f2a0933a14016

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.11-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 0534b9ca2f840d1f5448d1abb7b00d109195e54e3c1b66fd48b204d741e6a3e5
MD5 7891aa5f3add8da8879b068188cc68d4
BLAKE2b-256 6b785ae182d5e9bdbb0a83d55bf4c4c0241bf1b1d76cd8f454d64cb2f93edadf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.11-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d8586bb001198b2944547ebcf7b60869af41629476478d3d015dc6ff05afcd20
MD5 39de5420232fc45775a902618d979c0a
BLAKE2b-256 b1b9efc6c43aca0fad920e6ea60f1b8c9bb6c47f4adcc5e5438a7d6c0c406cbe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.11-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 946e8fa1e0c35dd5d90ac6a018639f7f0b9750918a103bf04c4922b39f6ad463
MD5 05cdf6e96e64d8d93f17ad0c48344c2c
BLAKE2b-256 00045d6ae4432507c5e214028ff86983dd894985fcc002d208b0cb5c91b14f52

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.11-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 5d6d501066e111ca021a55de03ac080122794b95750e32a62ef9fb3fae77bea0
MD5 356e6c585dfc3e582c82daeea3aed0ee
BLAKE2b-256 8b25b27fe3521b50254cb4841ad19508501388a963cc4bd3446e0e84f51fc9d4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.11-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 39c383b2638368b20ce85319458ff914dab0bc5e87588ffec68879719e3c7dcf
MD5 ff79935d585863971431045bc3db65b8
BLAKE2b-256 4a8e0796cf8c12799a1ea074171dbd97e875cc8a75af20b7600dc8744a54ea47

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.11-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 fac130a35a9c044d1ec8dce909b7ffbecd17d0bc3498250be658abad12f0cdc9
MD5 450d4668b13bbd5057aa127a8d8d6648
BLAKE2b-256 8f7413203b2f76a95a5da04f194fa397b47501682b953582dcaac8539cd3a381

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.11-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 33fca4a297b06881945c75bab0e6466d5b428d4fd7955c6f90c62f39fa277952
MD5 0e734422c0d9c2ae9abbeb44d802ec26
BLAKE2b-256 b09db6843a1b5c98b52024e671b0e11eb2e8583de016c5973f15eb5bff87a242

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.11-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 4c888815e35936a510570dc31bca00ad716aec035b8d5975b00fd056c2cb431b
MD5 482cb2a99c6e0abca119090a55ca7061
BLAKE2b-256 c128f112ef823626cab9cf2359852dd52da47951b6102468a3f799c2f29809a1

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