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.5.tar.gz (709.2 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.5-cp314-cp314-win_amd64.whl (8.9 MB view details)

Uploaded CPython 3.14Windows x86-64

nutpie-0.16.5-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.5 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

nutpie-0.16.5-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (8.0 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

nutpie-0.16.5-cp314-cp314-macosx_11_0_arm64.whl (8.2 MB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

nutpie-0.16.5-cp314-cp314-macosx_10_12_x86_64.whl (8.9 MB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

nutpie-0.16.5-cp313-cp313-win_amd64.whl (8.9 MB view details)

Uploaded CPython 3.13Windows x86-64

nutpie-0.16.5-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.5 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

nutpie-0.16.5-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (7.9 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

nutpie-0.16.5-cp313-cp313-macosx_11_0_arm64.whl (8.2 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

nutpie-0.16.5-cp313-cp313-macosx_10_12_x86_64.whl (8.9 MB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

nutpie-0.16.5-cp312-cp312-win_amd64.whl (8.9 MB view details)

Uploaded CPython 3.12Windows x86-64

nutpie-0.16.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.5 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

nutpie-0.16.5-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (7.9 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

nutpie-0.16.5-cp312-cp312-macosx_11_0_arm64.whl (8.2 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

nutpie-0.16.5-cp312-cp312-macosx_10_12_x86_64.whl (8.9 MB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

nutpie-0.16.5-cp311-cp311-win_amd64.whl (8.9 MB view details)

Uploaded CPython 3.11Windows x86-64

nutpie-0.16.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.5 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

nutpie-0.16.5-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (7.9 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64

nutpie-0.16.5-cp311-cp311-macosx_11_0_arm64.whl (8.2 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

nutpie-0.16.5-cp311-cp311-macosx_10_12_x86_64.whl (8.9 MB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

nutpie-0.16.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

nutpie-0.16.5-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (7.9 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ ARM64

File details

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

File metadata

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

File hashes

Hashes for nutpie-0.16.5.tar.gz
Algorithm Hash digest
SHA256 2be8c0c55e6823244d08ad4eb3eb52a5538091734f6f61dc5f8f1aa534df845b
MD5 b48f5a3c64b60642ee0a6c39136e7b9a
BLAKE2b-256 a5390f15a810a4a2d345b210739e48bb1a8cb83f2db4a0f9b41f9040738f3ecb

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for nutpie-0.16.5-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 b3dff21c7a0f4944529163e328c0a873a2790db8ee1a9c1b793e0799a6bb6e40
MD5 7350c327f9488bca0e8d6c71daa5d30b
BLAKE2b-256 44ba5c96077e28608428f699e8dba9224db8879576a3930cdd32ab7ba32c6f5b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.5-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 870074501b1e6330a6ba3c1b982dba593309e8eb4294e7a823f33ef191923704
MD5 c72b71d60f8eefc63f65d0ab393fcfeb
BLAKE2b-256 34979c3a04fed27e673dec5662469a563540c11a2bbca0caad00a30bd0afe52f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.5-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 776a328e716478308076e2b6fed7062e90b831c62d18eafa11ac1e4d2bc5fc44
MD5 9a2d206bfabfab4c221e3a6b9cb4532a
BLAKE2b-256 c6411aa94d4fabfb648c784fb083df9b26b5522ae1658da9f2a641b655d7c13a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.5-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5806ee05b3a83f254d0818df8b548a0e21960b72d3f9071d2afb9f2dc457890b
MD5 d0d9d1246f95c353b8b8047c1cdc6c01
BLAKE2b-256 5ca5ea66b1f812dd6c9a6e2710eac77374fe396eb241f39f5874a20be552a9bd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.5-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 db062f9e4504196db81d68b886f324457dc017610cc27298b09335b4e9d0ee20
MD5 9694ed771fa63cc0398f686c2abb26a1
BLAKE2b-256 4e3413fa86657d082f5f15423b7e951d3cfac3093868d064f5ab904c51aceb1b

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for nutpie-0.16.5-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 4559653aa04bd7f75ae8f5863be6023d40d026a22bba40656155530d3b8bf28a
MD5 b0f969d8e7d34b2ecc26fc41d365ac22
BLAKE2b-256 26e25afe31d30113f07599cfbe329257de59dab67843e0ab2f81ab2f72d37afd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.5-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 23a9501486e61cd562e86eb9dd1e0a00b751622758265787536dbd3b23c1f458
MD5 6d62ad0d421fa80fa596502c25d138e3
BLAKE2b-256 dbcedb3dc173af061f19839413db740454a564fd00e8bc8c4cd92208095a0f61

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.5-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 46990cbe1a0812efa0603c7376ca8d78dd79242a5db01564286df7e3a9c8a381
MD5 9a79e02782b0bfba5f89d637031b1e07
BLAKE2b-256 8fa6505a74abf131a035a78bdc75f35217b0710695a2b7f0b8fcdee80f92c375

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.5-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5104d8817e4decde709d9e3482c4e58ae9bdc5813d1ceded473046b0ebced2ea
MD5 3a7939d3506e4b0881421600dfb33e1a
BLAKE2b-256 17c6a08d73c04da1f76c59accd3fd02e6df7b3010f8c527a0ef08a0522fec959

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.5-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 94a694d7f78e44aa2fd0e98017aa700999c9c39cf19def67eaef6006e50bf2fe
MD5 3f58f57d8eed94762339beafd9842dbf
BLAKE2b-256 fa7271266628d07ea535797632896853916e3948686ac8684b7a71778c70e618

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for nutpie-0.16.5-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 7ecd67201b322c7fc38d8ba21e84464b2904810902329d51238df0eba0f62896
MD5 2e487d3acbe63c13a07a4ee3279167d8
BLAKE2b-256 004e7396d96ba2a94907c644cfd5b8af5c1cadac6a5eb03b3a73e3cddfd4b2e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a4cc27d8b4704c13dc1881b6435258de4263005ca784a3f636899f463a532a29
MD5 8fab901f9311b63937e3193ef0b5716b
BLAKE2b-256 6972ac557a95448e9e4897980e7fe90d953ed5dc4554e68217bf914f66c2152b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.5-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a98844e61800423ef6f067df6ce20b29276afe0986db2a20251f940b69c66409
MD5 73d19a302768b382bc412d096cc602ae
BLAKE2b-256 6b9683a69318e09d3c725d32fde8aee1269ef2446d8b0c34a23adb69775fa71f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.5-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f6782b6aa34571716296618f9fe7d667e5313677ed149ebbcd8c19504d15436f
MD5 cdf17d6e065d9c64f901512ccafa2a38
BLAKE2b-256 8d440d3f1ec5d33320a324ad9e0f20c48b6cddca716e0d0a9882e0a4a3beeb37

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.5-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 012534695e43099a74f2f207bbd52eea8dbdbe6c0648e701c452ba63f49dd5b7
MD5 e74f340bbaa19dd2705432cd241656a6
BLAKE2b-256 1c9f054264c5a17b618563b4f44f13e9fd5d56a3057f8557c27fd54c56f05f15

See more details on using hashes here.

File details

Details for the file nutpie-0.16.5-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: nutpie-0.16.5-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 8.9 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.11.5

File hashes

Hashes for nutpie-0.16.5-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 679f3d6661bdf585c609c2cabdbfe7d41d6691f6b17dd6cb767e15ea11035e42
MD5 fe883b883f617532d24be5ff6986fc89
BLAKE2b-256 76851ae1ea88cabe1d0019a438e6fcb59090dc06d5d472f9ecebb678464b3d9d

See more details on using hashes here.

File details

Details for the file nutpie-0.16.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c5ad03190dca941b97e97678dca5fa26da6e07d01ed360e9fe4fc0b589d85589
MD5 1119a46256d23aa317eda80b160fa47e
BLAKE2b-256 f4edd21e1dbd0de29980c6eebf6f054fb9bdea2b8e69615ac0fb4258bb250c1a

See more details on using hashes here.

File details

Details for the file nutpie-0.16.5-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.5-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c106078be5e246673e4789d01e693ee0a2bfb04aef9f92c2990d37c0ae8dad5c
MD5 ac531e483796d4983b1dc14adcfb35bc
BLAKE2b-256 59e494804707e459bcbb67786560a83d14ff394d52cf109f3c0e8da854f038b4

See more details on using hashes here.

File details

Details for the file nutpie-0.16.5-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.5-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c1f45fb159b7a1ea410428ae932db856559d9950aeccf9bdebd668e1cf064757
MD5 6ec64da6552c5bedf212c3d58585ce82
BLAKE2b-256 632c92db2169360f1d8635dff70540ba7fc3e73b46a003abf35a57567dd47c0e

See more details on using hashes here.

File details

Details for the file nutpie-0.16.5-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.5-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 50663c44abea6257d934d7210ed7c203b47138892e85c7afeca6630114848512
MD5 2b791705bfd706c26b9037cdb99eb07d
BLAKE2b-256 8ac6f4a4086691761e4d655827a9500942507d21462715ca0ad9f08d336fb3f0

See more details on using hashes here.

File details

Details for the file nutpie-0.16.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8d5e36171ca0c48dfb958031f287c535fc1b423879bd480280302449a89bcec3
MD5 d20e0ddfbace06095cb9f677435ea4e8
BLAKE2b-256 6f503c1f18d9d56dd263aa34d05ece94a97bbd874f92ecd241462436077d6e6d

See more details on using hashes here.

File details

Details for the file nutpie-0.16.5-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.5-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 7491d2431de9d5a9bd07e19172bd385aee381e0100a630fb46267a9cf2c59b2b
MD5 8803f4ac67c65f98ec199a62f05ad014
BLAKE2b-256 ad4736bd4dfbbe50b714ef3e6693d35687e19f9c7ebbf38934e975d8b80af661

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