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.9.tar.gz (721.7 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.9-cp314-cp314-win_amd64.whl (9.7 MB view details)

Uploaded CPython 3.14Windows x86-64

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

Uploaded CPython 3.14macOS 11.0+ ARM64

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

Uploaded CPython 3.14macOS 10.12+ x86-64

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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.12+ x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

nutpie-0.16.9-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.9.tar.gz.

File metadata

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

File hashes

Hashes for nutpie-0.16.9.tar.gz
Algorithm Hash digest
SHA256 db82d43c3e84e454702b9c3276231e741e2ab6c173b09abe277ee52a75f847b3
MD5 0f599cc1af385ce427cadade780b8638
BLAKE2b-256 6612ed55a87daf5e410152e5083d1ed4bc87446d99c937f599db4bd7760d6f3f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nutpie-0.16.9-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 9.7 MB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.13.1

File hashes

Hashes for nutpie-0.16.9-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 130511ef22c4051a9d65cb47e59f22fc546bd096eff5e0a4dd3e2ba00ae574b9
MD5 24cc5ee49bcb72cd86dea39e3d7a3780
BLAKE2b-256 059831159bbc92e80d6f9f367760180aa59d6e0a11d3661ad3724fede2944bdc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.9-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 48ef02c10c9f637bcbe023ca16d17ca7d889d68c019a6da818dac08975495edb
MD5 c937ab7a737055f34473e5497d5f0ea6
BLAKE2b-256 f51fe8b503934cfa9e4634b165339fbb8dab8348cefa18b74fb1cbffcec109d7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.9-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 f3f171b2d774bd4177d36771b0622c3ef32d01f2864503507c8de1265206a2bf
MD5 2cd9f5f0bc2c3611d58abda45525eabe
BLAKE2b-256 e8dc6043b6abae2a5952c9df6ad2cf8356cb70e0921a97e8fdad569b49bf89ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.9-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cc34f50b4a35871e38992d632b800027b6b5b6e6514b99f30cd333c2b6931b17
MD5 280b2558c84bcae78b5a2e2482ca9cac
BLAKE2b-256 55aec7d1b8a3ba690ced21c397b51d648518afdb1c51d71d7c7841ed9432d639

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.9-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 233fe4ffda92a065534c98f4cd9b9ccce09103999414691acf522cb587c369ff
MD5 6ceb4364ef0b4cce4d28304bfe796f54
BLAKE2b-256 636fcfcb406726c04699279eef6ecfafe931fe710a1f3eaf8060da99d29198b0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nutpie-0.16.9-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 9.7 MB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.13.1

File hashes

Hashes for nutpie-0.16.9-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 dcc5bd3cc4778574040e70292d555a06ea2ad980b10cf4dfa4f2114a258b4090
MD5 9eae8ae3da15e2af8f3af9fc9de5c267
BLAKE2b-256 5c5b568a19f081f9f0fcbe079d3c4db83598279775923deaed72b26c4cfd19a1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.9-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 27b61beda240835b98424444b65961a052d678d0916d8114f4580d4a9d54b12f
MD5 f0f31af56d3a0657c29466b9edf1d4da
BLAKE2b-256 755a02071bc00557850e6a181b3e3df58a47ddf7314d51fc540ae2f3059760b5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.9-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 beba36240a525bde52c9e0ca86910be4995ab617e8792326568c27e50bb4c92e
MD5 90c306e8c3c903a8b5bd6bc08ccc1c3e
BLAKE2b-256 9deb03359d1234be2afa531d21146d9b0131723af0632f33d7405c1c8ec995b7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.9-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9ca3f24837ed333fd8f7bd8bebb69ecd5a791e2888b73a7a18d42be85f316179
MD5 810c753084227a63f45d5012af5a7de9
BLAKE2b-256 aaf18ae084b4880d97265817a978f6f58eae10e3d2319726a75f38ee50f91229

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.9-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 572db7807b9a0b564419fb459bbbdff38f974d541337c80c7b61f0db95bb3338
MD5 85a256232f2376f4de462a0bd8e77c65
BLAKE2b-256 e326ae869737f9ee2ac432b378e4c8583d0c010e01b9bf8127d0163f0c26af4b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nutpie-0.16.9-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 9.7 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.13.1

File hashes

Hashes for nutpie-0.16.9-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 5a32b4162a24d2b213512eee40785bab25673962be3e9f17faaaaad2cbd85a0a
MD5 23be8908eb8a9aaac7057659dc824336
BLAKE2b-256 4b27f63726610c17abe209c44944f026d75398eb5ef236c8cc8fbf2320fc9497

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.9-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 53c33e800a17b6c089e24f06a89ac93885da5055562a8d4c70d50cf20e21d504
MD5 997ee9b70af8eb3d4c2215fdba5ac55c
BLAKE2b-256 df9938a1fe36180be22f419277243fe15aa47fb3b05e03fc29d1cfdea6d4492f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.9-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e208dd6cc76b94ab86db73ae61b6deb758922219bd5d71ee090d396d5bee5501
MD5 81f4553543b1686b0b5319df84f26d40
BLAKE2b-256 93ec833721016d515c11c64e3367600da0a8038a438cd8cfbdc794027193b510

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.9-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 342d354420c0804a42b23f5d2dc316740da52968313fe24277fb1b236e81e04f
MD5 a75b92419dd42d9b563f870f167ba76e
BLAKE2b-256 d37254dea3f4aefefa72d2e95df7ef737c84089ea57641bd3a06113d267aeaee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.9-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 f9435ee458fe32a73f3847298c576d752e63ba4e0236af35fbd80280eb1fa914
MD5 7fa12d3182e2d61b43c918bc984a1b8d
BLAKE2b-256 7315d89d1d77b9b626f27a246a5934304ed5860ecdba738d96cb3e1023a928b3

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