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

Uploaded Source

Built Distributions

nutpie-0.11.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl (4.8 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

nutpie-0.11.0-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl (4.8 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ ARM64

nutpie-0.11.0-cp312-none-win_amd64.whl (702.4 kB view details)

Uploaded CPython 3.12 Windows x86-64

nutpie-0.11.0-cp312-cp312-manylinux_2_28_x86_64.whl (4.8 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.28+ x86-64

nutpie-0.11.0-cp312-cp312-manylinux_2_28_aarch64.whl (4.8 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.28+ ARM64

nutpie-0.11.0-cp312-cp312-macosx_11_0_arm64.whl (804.2 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

nutpie-0.11.0-cp312-cp312-macosx_10_12_x86_64.whl (832.0 kB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

nutpie-0.11.0-cp311-none-win_amd64.whl (699.8 kB view details)

Uploaded CPython 3.11 Windows x86-64

nutpie-0.11.0-cp311-cp311-manylinux_2_28_x86_64.whl (4.8 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

nutpie-0.11.0-cp311-cp311-manylinux_2_28_aarch64.whl (4.8 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ ARM64

nutpie-0.11.0-cp311-cp311-macosx_11_0_arm64.whl (802.8 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

nutpie-0.11.0-cp311-cp311-macosx_10_12_x86_64.whl (831.0 kB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

nutpie-0.11.0-cp310-none-win_amd64.whl (699.9 kB view details)

Uploaded CPython 3.10 Windows x86-64

nutpie-0.11.0-cp310-cp310-manylinux_2_28_x86_64.whl (4.8 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

nutpie-0.11.0-cp310-cp310-manylinux_2_28_aarch64.whl (4.8 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ ARM64

nutpie-0.11.0-cp310-cp310-macosx_11_0_arm64.whl (802.9 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

nutpie-0.11.0-cp310-cp310-macosx_10_12_x86_64.whl (831.0 kB view details)

Uploaded CPython 3.10 macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for nutpie-0.11.0.tar.gz
Algorithm Hash digest
SHA256 3dcb7e0896fd611eb11d93f515959c590773316456cc972a6e9c5fab99a67f91
MD5 2bc07121900ac1a89ba5474d2b42d08c
BLAKE2b-256 b2ec33b6893eccb618decd53f80fd98753ff9eb81aca9163e108165053ae948a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c05bb2968ca6624f81ffad0f495451fb0ede94a2d8da17b9f3f2b0e235203842
MD5 91948bb2e1f10c8eec3662d7b275deb2
BLAKE2b-256 82aa62f300c58ebc45c56d497341a00c3b20db8156f46146b9165f4f5d42e950

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.0-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 6d567957d685cff1607f389e88d531d0db3a0a512b410e8cba5c521e0195fc70
MD5 46ff428b00c76126f8d42a23b44fea76
BLAKE2b-256 a0d7015ac9670e1ba4938f4c0ea986e9dcde25df746e172340a4f29438586772

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.0-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 e579c835908ee3de82c53f3cee4106107b46847bf6530f2ac319ac123bae0aac
MD5 43535ef4326029dae188a39ee713a8ff
BLAKE2b-256 85820bfb8dcdd0fd5dcade4ad9c99dd47840889a1a76e27fc866b2f17e3225af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.0-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 dcb012fc42a9aa80da4d6ca3907886979dbcd0e36adbb21b01c40a4122970f11
MD5 45a1df46fa1ac67f1b1662c1ea956ba0
BLAKE2b-256 198de42f1b5f4b858d84906edf125e26845c9fc2b415bc8f80ee250aa240df5a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.0-cp312-cp312-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 b4c9a81199b6971e92332cfa15a8946137888a37ae271d2249d373513752d900
MD5 3063773c17b36175c49afb3391104c9c
BLAKE2b-256 5389cbe5e6c780be1f2f2a38eea7494053e2d46b6e23b820cd5139b8e0dd142a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1f5824384a02770323ef0ef969f9540b9005ab9ab16f36f67aaf7471763e211a
MD5 35d600bf7e8e79d7769c13e2c35fcf18
BLAKE2b-256 349061a66b31d6e9b9327ae0441bbad99adbff3c3bebac5da297f1cea0671c54

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.0-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c187af27d051dc9b891b84e031878f60d78d4e76495cdfea94f204f65e20d411
MD5 7894ee34593580463c01acbca46d04c8
BLAKE2b-256 4875f2a5b72f38cb9dedcba4aedad4e09d69133a85f5913d7783fd648d43cff1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.0-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 e1a7630d00bc3772d5b4134d82624d4731ab295ac5bd2271b8dc87a4b6063672
MD5 0190d91234211ee65dc331f209785a61
BLAKE2b-256 32aadff8022cd0f3307cb50deeb11edd764fb048c3211f5ba1f7dfb0fa172b19

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.0-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2bb26b8565f2e271cd067f43ff9e8d2df5b7302c3586ce1cdfabd51830026e48
MD5 83052d0d1f7dcc7d14b8a8a7921b86db
BLAKE2b-256 3b9f387f37e5e5b344b7d87395c166c9cf3b262da12bfd94468633a727b4293a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.0-cp311-cp311-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 965009c75ad6283cd0014a074d6f8c222f859d2e8cb623d82c79dcf7d77e6db9
MD5 d8bdcc09d6dadbcc812ac096ef9b6772
BLAKE2b-256 d57613cc941e8a9c6834b960920b8e0bfd93933ef7e68cc9aec8402e69d47632

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 93fa9f7bdd1432092bdbc7be1511ac91b593ccf0859524791e0f26b14ade0f3c
MD5 6b65e29dfbebab925403d60214f088fd
BLAKE2b-256 cc4d1305cc02cade1e7b7b30a99fd59aadc719a69f345d02138bc0f7e4375d0f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.0-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e4d39548cc0171f4bbe64a48d9320ef0622769fe405db983ef41acccf605eda6
MD5 2ae5c27c87a0624927c7ff52725ca8f4
BLAKE2b-256 0ff95ddfd87f0658efb6c547b38c17cc8346f7b6e994dab3478c14592a80bdb0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.0-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 5d6c0fa912155f90b502ae1ec5d3e23188146e148cba5b58594309a11ee1baf2
MD5 ea842ccc36b9d8241c54aec7f1b25f6e
BLAKE2b-256 4378f2d250a58a9ac55fffa4bcbf9b8b1257b512622010214f4fc521ba369dd5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.0-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 340b4c200beec9648070c41bc39c4f52a21a8cfeda75113e42fb2dec99f39b91
MD5 977992c78d5b4f99b02d3bbc0e7c3c1d
BLAKE2b-256 386cbcfc01a6b7138533d61084c7b5d9b93401f7ea7dc3930be1a6ffeb2309cf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.0-cp310-cp310-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 25df2962510e2d439656ee89eba9b5284effb2896560b60be77ae7791f95c0ad
MD5 5e411fde1a60c4ee92f5b30407223f06
BLAKE2b-256 82de620c917a657b1e2e8af2e248b7474fd47f07e565823e3b9f86961dfbd9a9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f1992cebf71440083416fe97d0750d8040e7a66a4331ab3a58bb7a48310c43ae
MD5 1db61c9c715a9f707e5e1071ed6488ef
BLAKE2b-256 271978e61f832dcf98b4451794d22c287ef4ed9be65936fe0f3fc32210beda4a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.0-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 239d2d12c0789f37a4732022ad4b80c610d832f9d7876522185e3c3b9e3dedb2
MD5 7e84a2ac7f4fbb569920acddd4be832d
BLAKE2b-256 8ad7913a2c31ea5a8aebe2fd1f27ef5b0744948508543833a4a83d0064f0cd7c

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