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.10.0.tar.gz (171.2 kB view details)

Uploaded Source

Built Distributions

nutpie-0.10.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl (3.5 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

nutpie-0.10.0-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl (3.5 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

nutpie-0.10.0-cp312-cp312-manylinux_2_28_x86_64.whl (3.5 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.28+ x86-64

nutpie-0.10.0-cp312-cp312-macosx_11_0_arm64.whl (667.1 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

nutpie-0.10.0-cp312-cp312-macosx_10_12_x86_64.whl (681.3 kB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

nutpie-0.10.0-cp311-cp311-manylinux_2_28_x86_64.whl (3.5 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

nutpie-0.10.0-cp311-cp311-macosx_11_0_arm64.whl (667.1 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

nutpie-0.10.0-cp311-cp311-macosx_10_12_x86_64.whl (682.1 kB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

nutpie-0.10.0-cp310-cp310-manylinux_2_28_x86_64.whl (3.5 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

nutpie-0.10.0-cp310-cp310-macosx_11_0_arm64.whl (667.1 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

nutpie-0.10.0-cp310-cp310-macosx_10_12_x86_64.whl (682.0 kB view details)

Uploaded CPython 3.10 macOS 10.12+ x86-64

nutpie-0.10.0-cp39-cp39-manylinux_2_28_x86_64.whl (3.5 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.28+ x86-64

File details

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

File metadata

  • Download URL: nutpie-0.10.0.tar.gz
  • Upload date:
  • Size: 171.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.8

File hashes

Hashes for nutpie-0.10.0.tar.gz
Algorithm Hash digest
SHA256 d75b6bafa5a9d9534e456d6a09d61b41ad20a492c9f47528516d08ce89420770
MD5 297eba3a1ae5d80afbbcb10c8f10779a
BLAKE2b-256 b1e03fa6b85e944798f1583f77f7e321c22d78cfdb60b509c50d959dfd626bc5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.10.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9044d161b4501d9ce86fd1cb968cd874cf1ee453cb71d618207b87907633810e
MD5 c4ec3fc6ff902d9e1f07f5fc1913ea74
BLAKE2b-256 246e327772aab7701c84f965e55029fd52b5005918b53e297ae718ed3f4b1d03

See more details on using hashes here.

File details

Details for the file nutpie-0.10.0-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.10.0-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7e4b3666735a37d44d5c0f445c8776b811214a9f33d94e756a02990a8d2eb5b3
MD5 3c5b858908ac7b71adeb3cdef3d02ff2
BLAKE2b-256 6750ce37268504fd26514e6c2639619c6362256daa3969b8a9ddbe2086484cc2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.10.0-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c64c54da5fb5a89c8537e63ca99c98b4b7bc023565d8888ff4fd4dcc8f1d3f9d
MD5 d29b9d6173a75090cba45da9a642d6a2
BLAKE2b-256 713f6384c39450fa5ab6af0d5d3d8b14cf1cfebd16ae7a274ed6b2f9696c1240

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.10.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 009b9b192d3682b8f010cfdfecad272447b72a129fa70c01b15dee2f40a096cb
MD5 6836e88d269f83a9a744d2ff5c23824e
BLAKE2b-256 b183808b35bd04679993111b3f4dbff93ecc1bada83fc3bf6a299b3907fe8498

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.10.0-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 005c8e31a23fd95fc7e3aa4ee6bf9f6bee15ca63f0f5bfc99fcfaf7b0d3dc65d
MD5 91232c80940f5c2d7aaa6ab94ba24703
BLAKE2b-256 1bc52acebb71ced80371438d81f2d7d50dbd977f7de911935ff8535b51eb9d4c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.10.0-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 987808e9aa50459383a8a36d4d1ef5f11cd510a55d1068db9b7f519b6b063704
MD5 0ab7bc5d1aee7f2bd818c96afb1e8fd2
BLAKE2b-256 91b81460ac65b4e04a3d502ecf278da4a929d6b0c4107e35b65f62deb65976b6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.10.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f62c1f37d45f1bf9a2862ed182f7c9b03e2dc3f08bf88705fa33ff2e63d12728
MD5 7495c29bd5468f7399bbabc3c18714c0
BLAKE2b-256 902fc0bfea7184fadd3bae4538e20aeca1797e4613ebb10443d0689821ee0771

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.10.0-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c6836b0a85ca552263049c8eb7039b5ab77221a43e00ce234322617aac02a5f7
MD5 4339e166d0871695557088e4211a8511
BLAKE2b-256 0d276d56816f2bc410082a35ac377f9fa6a69a3e57c1f195b6b633c96f69057e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.10.0-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 54331292ccf2bb0b7fe6cbe465fa81d2d853aed823e96c60333cd449275bb5f3
MD5 010447e88fb89312371e70c6344fd465
BLAKE2b-256 670b2f1389603dbe41bb9b51a1eb1ec3db52260814d8e3635f8cdb21afe793d8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.10.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7d13d936ada677a5754a7312db5342894fd09202b5d2ec956ebdd9c50b325596
MD5 01b4cc094d1b64e4ab635c3866dfb7e2
BLAKE2b-256 37cd32a7a0ff289062b5424e9200fcf4d1f1b85c633de8d2196d295547de2f2b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.10.0-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3a0c24cca6c3357bb4c9b9be72d489b52f0dfc898db65d747c8634d9112ce905
MD5 3932dab74667d3731fbc075ec98c6308
BLAKE2b-256 6ce8e1561a63d6cea8fa8bf6ce560ed2ec2d8fbab415df3deeb19965f30f178b

See more details on using hashes here.

File details

Details for the file nutpie-0.10.0-cp39-cp39-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.10.0-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 82d63f7c5afc0296bf047525d500e8380146c5d86a7226e8d6abeb1d116a554d
MD5 d9fb96e9f92374349cc06f2210da5229
BLAKE2b-256 4c20bd9dd2a6f3690ab8bd2da76699ff68ff7b0f5a337e24d8421479c8bcd15f

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