Skip to main content

Sample Stan or PyMC models

Project description

nutpie: A fast sampler for Bayesian posteriors

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)

# 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.13.1.tar.gz (183.9 kB view details)

Uploaded Source

Built Distributions

nutpie-0.13.1-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl (9.5 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

nutpie-0.13.1-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl (9.7 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ ARM64

nutpie-0.13.1-cp312-none-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.12 Windows x86-64

nutpie-0.13.1-cp312-cp312-manylinux_2_28_x86_64.whl (9.5 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.28+ x86-64

nutpie-0.13.1-cp312-cp312-manylinux_2_28_aarch64.whl (9.7 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.28+ ARM64

nutpie-0.13.1-cp312-cp312-macosx_11_0_arm64.whl (1.4 MB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

nutpie-0.13.1-cp312-cp312-macosx_10_12_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

nutpie-0.13.1-cp311-none-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.11 Windows x86-64

nutpie-0.13.1-cp311-cp311-manylinux_2_28_x86_64.whl (9.5 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

nutpie-0.13.1-cp311-cp311-manylinux_2_28_aarch64.whl (9.7 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ ARM64

nutpie-0.13.1-cp311-cp311-macosx_11_0_arm64.whl (1.4 MB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

nutpie-0.13.1-cp311-cp311-macosx_10_12_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

nutpie-0.13.1-cp310-none-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.10 Windows x86-64

nutpie-0.13.1-cp310-cp310-manylinux_2_28_x86_64.whl (9.5 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

nutpie-0.13.1-cp310-cp310-manylinux_2_28_aarch64.whl (9.7 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ ARM64

nutpie-0.13.1-cp310-cp310-macosx_11_0_arm64.whl (1.4 MB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

nutpie-0.13.1-cp310-cp310-macosx_10_12_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.10 macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: nutpie-0.13.1.tar.gz
  • Upload date:
  • Size: 183.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.7.0

File hashes

Hashes for nutpie-0.13.1.tar.gz
Algorithm Hash digest
SHA256 225354701958c0fcf020e87a96335600f157777bbd5c6e64994ff5f12c3dec54
MD5 15173fc796ac51ea2bf0241888544b31
BLAKE2b-256 89c61a293f75b8171976efbc0b51566deee41118c73c3ae0467b8783fe7cb40c

See more details on using hashes here.

File details

Details for the file nutpie-0.13.1-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.13.1-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a4ed934f76ef789e441b2000446155d24dd9e066908aa3de1dd9821516439ee6
MD5 769c146c4ccb10d5c84094798502834a
BLAKE2b-256 71b1d4b603201f97b63f00fbf9f45791e17a6f341bb5140080f445a931780447

See more details on using hashes here.

File details

Details for the file nutpie-0.13.1-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for nutpie-0.13.1-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 6ed2c7dbdbfa7160b61746148ef981073d0631265aec1241ebddee0d03922ea7
MD5 0f2baaafa7c311fa0175b7cb144bc501
BLAKE2b-256 82e983527511f7730d5141f9302b4cdb527d4a6c1bd436f44365f981a2046072

See more details on using hashes here.

File details

Details for the file nutpie-0.13.1-cp312-none-win_amd64.whl.

File metadata

File hashes

Hashes for nutpie-0.13.1-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 1c2d9cf2386029ba71244a56ab802f9da6b9fe85dcb8ff9afd116bbd5cc88032
MD5 744ac731ff2ed30a162315f68884b865
BLAKE2b-256 ec2339b1a541850b56083756d00e713e906a7a1d8132042672f05c7271e6d534

See more details on using hashes here.

File details

Details for the file nutpie-0.13.1-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.13.1-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a079bf98a9ee678195c02f702895f519a145df807a9ce092ffb0de75938b4f0c
MD5 3b3023e390cbbe209347e97af09c47dc
BLAKE2b-256 352977e052b53a92545c06e5fb6cbfb6657d19849ad5632222c238048289f825

See more details on using hashes here.

File details

Details for the file nutpie-0.13.1-cp312-cp312-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for nutpie-0.13.1-cp312-cp312-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 98b8868c9f3ef6a91011d81590a73bcae950c06e775cbdad419615450eb6918b
MD5 2311baba7bf163a97dc7c546f7ce7fc1
BLAKE2b-256 c968b9352cae62f182be30cb766e7b009c95730ad3685e1a53fa286c434e3b62

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.13.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4f9cab8165634dfd201e8f1809281bed0e2f95cd99f7b166fc28db5b27c58ef9
MD5 5b127904612e80250137a92142f9d917
BLAKE2b-256 715c0608b8e36c66ff7b2f16eb15a3c9c7ce1fee601ab14f95252f94594b9a24

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.13.1-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 50f7471f431eb934bad34ecdb50d3064a19ade3325190a5b8c05e177f31a7d74
MD5 2d3509b19e81d2461344210a84fe74e7
BLAKE2b-256 2e24eb9ae157b6e45f1aff912f55e39bc3a22c048f30f69a53fb9a15a1c67dbd

See more details on using hashes here.

File details

Details for the file nutpie-0.13.1-cp311-none-win_amd64.whl.

File metadata

File hashes

Hashes for nutpie-0.13.1-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 86722be3d567e89cb2eb7de90ffbb45ce2b0eef93e74f22b1fc49f367cdf8f6d
MD5 023bdd2e87dc7f8a6463c496d0085559
BLAKE2b-256 4b662426a6e0b1b7224a42c2a678460ca64ae084e332f855d8b444990818301c

See more details on using hashes here.

File details

Details for the file nutpie-0.13.1-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.13.1-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 02601da8b7feef640a8ae6f335838689837ad0ac3b25c5b0dad32daa74e112e1
MD5 38680ad225f935193c4df9de3f3ba06a
BLAKE2b-256 0b3246fb4e4b943c6111997d59f863fb0de9254010b85ac729d348a283820a67

See more details on using hashes here.

File details

Details for the file nutpie-0.13.1-cp311-cp311-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for nutpie-0.13.1-cp311-cp311-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 5dab08b58818e378dc90d6c1a6d470d61fe8e48de3f05b2995342411b277766f
MD5 b236b0db4b811928d86f16639f525890
BLAKE2b-256 2f573f8b8bff480f963d38e319394476561f9655c4c56ce4996072d4e1ca67de

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.13.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6011f21450315f8f98133414360ee6d9ee07df401cf3c8dcfad3bf17bec0df4c
MD5 d56507456e7fcb95ef26ccbb845c59b6
BLAKE2b-256 f8657b6fa2eb0b073233f6f08f55480766497aa04cf01b7bc3c9f1d213efe83d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.13.1-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 32f12de62b5b5145a5123e4f4c31688a3b889173b3ae958184de0678b19febb5
MD5 c7ba09b24674ccd19701a05b70ae87b8
BLAKE2b-256 40a154263c596061520357f5db180ad244efd4c71115d1360f559a17c82e2bc5

See more details on using hashes here.

File details

Details for the file nutpie-0.13.1-cp310-none-win_amd64.whl.

File metadata

File hashes

Hashes for nutpie-0.13.1-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 ee05ea6a7062bfedfad99d1f472d299a2486ed9a8c8dfa9c9efd8c51a92c1ba2
MD5 f78ff3d40c2003f14e73c8ecc88bb9f2
BLAKE2b-256 65721a1989ab91116c45b4aacb86fc51b4bb5fcb45b10f072d3607c6406130a3

See more details on using hashes here.

File details

Details for the file nutpie-0.13.1-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.13.1-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 141a47590ab210a0a11b6fc20ce73c2cfee6ad8f56ad54e12a50088af75aea71
MD5 6f0c1674f33bdd10c93979c44addd444
BLAKE2b-256 6c4b0c55c1c34738b2eebf135bbffbe1657bb949f73d7e050e4fe8d9a334879d

See more details on using hashes here.

File details

Details for the file nutpie-0.13.1-cp310-cp310-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for nutpie-0.13.1-cp310-cp310-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 ab9757ebccef606c65a83e335235f7fb331315aac42d5d6b1213cd58d71a917d
MD5 b2da8d28998e297bf3d8dbb18fd4882d
BLAKE2b-256 550f14ff8122b37a47579f2004da702fa2cd2cc66912d219828b97ac67b330cd

See more details on using hashes here.

File details

Details for the file nutpie-0.13.1-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for nutpie-0.13.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8b6d3039328005932b1f9e96347d4094cbc26f4678909bdd588df465f2d7c7c0
MD5 a61896d7c76e04d4d8a246dc3a1db4ab
BLAKE2b-256 90445b43b91615e47e2d1c3ce2ca64db8ed57b7ff879553da3845e809997f678

See more details on using hashes here.

File details

Details for the file nutpie-0.13.1-cp310-cp310-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.13.1-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 81937c68dd0a1e3f1b3103aa475644fdca3daa681f4ce891e5d5becce13b7bd7
MD5 c78ca4b883573b3f37f04b50ee2e2c60
BLAKE2b-256 ba6ef34f90adfd1f0db393b311d84aac388044ccc970311623cc657bd71ca2ea

See more details on using hashes here.

Supported by

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