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 pymc

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 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.

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 'bridgestan[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.8.0.tar.gz (165.4 kB view details)

Uploaded Source

Built Distributions

nutpie-0.8.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl (3.7 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

nutpie-0.8.0-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl (3.7 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

nutpie-0.8.0-cp312-cp312-manylinux_2_28_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.28+ x86-64

nutpie-0.8.0-cp311-none-win_amd64.whl (497.9 kB view details)

Uploaded CPython 3.11 Windows x86-64

nutpie-0.8.0-cp311-cp311-manylinux_2_28_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

nutpie-0.8.0-cp311-cp311-macosx_11_0_arm64.whl (660.2 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

nutpie-0.8.0-cp311-cp311-macosx_10_7_x86_64.whl (689.9 kB view details)

Uploaded CPython 3.11 macOS 10.7+ x86-64

nutpie-0.8.0-cp310-none-win_amd64.whl (497.9 kB view details)

Uploaded CPython 3.10 Windows x86-64

nutpie-0.8.0-cp310-cp310-manylinux_2_28_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

nutpie-0.8.0-cp310-cp310-macosx_11_0_arm64.whl (660.2 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

nutpie-0.8.0-cp310-cp310-macosx_10_7_x86_64.whl (689.9 kB view details)

Uploaded CPython 3.10 macOS 10.7+ x86-64

nutpie-0.8.0-cp39-none-win_amd64.whl (498.1 kB view details)

Uploaded CPython 3.9 Windows x86-64

nutpie-0.8.0-cp39-cp39-manylinux_2_28_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.28+ x86-64

File details

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

File metadata

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

File hashes

Hashes for nutpie-0.8.0.tar.gz
Algorithm Hash digest
SHA256 874f3615542024c944795473c71e2bfc905890267ed0a6cc87de95c7906a2774
MD5 bfc8f5360dd522606f555bb25943d699
BLAKE2b-256 7095aaa0419a76948e1751643f6a50556c5c34bf92327a38ff2b753d81394b3a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.8.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3e1fb32f7b5653ec3325be788ce6c68fd57825a9dd74ec46ea80ba96a26fb52f
MD5 9b85cb412325fc0b66e90809015e629d
BLAKE2b-256 85f00fc0a0a674ac186c8f96af3f0015d050cca790e4017f6905fa8c1e078197

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.8.0-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e4c77c8512202b2b49ba7edfe3fde35db4f8ce1ec7c4b7f41811ff68abc9bcec
MD5 8fbc3104baa1a09ef72531faa3cbb097
BLAKE2b-256 33471d78279cf170238edda1d852f415eba2542b1767bf736b7037061f78c0dd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.8.0-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0f6af7936809025a8ef72018848bcf3c8be4b529fa4e4f550d5555d48a36111d
MD5 f10038d0783196c2eeb60a368b240235
BLAKE2b-256 84026db53391bd58b091ae0c914f638bb75ccadc2393dd4e1496da8e38f5818a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nutpie-0.8.0-cp311-none-win_amd64.whl
  • Upload date:
  • Size: 497.9 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.2.3

File hashes

Hashes for nutpie-0.8.0-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 6f630e258e8d77cb9e94b12484149ef12154d58d8c8010152e67ca1a0c3cd512
MD5 65069352113ec209e22ec871366793d5
BLAKE2b-256 050d3ca50e43e6269593b8e1276623b7ea6546d7f166268076b712de523b08eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.8.0-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 99db8227bc8fe178efe2c986dd70ef194493f03ce6b20538c7639b0e985edcaf
MD5 35fd86786c75ed0bcf0ccf119ba0e3da
BLAKE2b-256 b57d8bdbae5ecdd047794d00aa3d2b39d31b48a4a369e1a65fd62d542f230eb0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.8.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a4dd90e299ea597dd6bb539b1e4f4ba9430d5fd1679c3e1ffbfe4602abebdbe3
MD5 6167885c776a5c329ef12a5d9645c62f
BLAKE2b-256 c7a1a8cfa21c6c048150a772df05b225aef70a0848271ec4ad0f971fde75cf73

See more details on using hashes here.

File details

Details for the file nutpie-0.8.0-cp311-cp311-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.8.0-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 35ce2e828e6bc66a4d8002fb3bc6dcf5e072c04d81a01fd593be3ec62033bbff
MD5 9ebc49545e19e9e60f90032cd541f422
BLAKE2b-256 868a021c80019d07a817696fb746fafae5334510711c562d1ce86e420ba30093

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nutpie-0.8.0-cp310-none-win_amd64.whl
  • Upload date:
  • Size: 497.9 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.2.3

File hashes

Hashes for nutpie-0.8.0-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 ee30ad392582038ca49d68e2c9c588491902d768c5384375bf3e6d959a40917b
MD5 97c4f8d48a4f7e1225811fdbff2bf741
BLAKE2b-256 e27acec9b92085dbe705fecc88d86ffada06bf25655d58e1ee3bccd563f683bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.8.0-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 071b6add04b7489d230a86f8732e52b5fa447a9d7a19d0d5192a6b3ad47d9da1
MD5 4bae3e472ead85298168a68b2ba9e78a
BLAKE2b-256 1bedae533f4576667b4e965a71e153422bdc535c4cbd353c8172302a1e2adc1c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.8.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e562d57488e984d687078ddcbdcc66b7fcf9bfbe01b50a1b70e15cee78d3522d
MD5 659db920cc330bdf3bcade83250ea7f3
BLAKE2b-256 d01086538a2f0ff2f5745826502a82c0f371a87668c84f16c15185cef73f1a28

See more details on using hashes here.

File details

Details for the file nutpie-0.8.0-cp310-cp310-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.8.0-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 952aa7b86e3c9bcb738c95a4b696fbbf4cfb87691d2891a356292e8d9ac677eb
MD5 8e1a700085fba7dbdec81073644b716b
BLAKE2b-256 100cd6f90c93e9bdbcef5e57c193dc9099ebcbf17ed27a3b3143bb2d117ba252

See more details on using hashes here.

File details

Details for the file nutpie-0.8.0-cp39-none-win_amd64.whl.

File metadata

  • Download URL: nutpie-0.8.0-cp39-none-win_amd64.whl
  • Upload date:
  • Size: 498.1 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.2.3

File hashes

Hashes for nutpie-0.8.0-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 0da1c30317502827fadd2335c3274cf15b888f0786f1ffad0b8b6ab2a3701406
MD5 913d67d50a9fbe0694cd00156bf13c78
BLAKE2b-256 3b2e71fa1fe9b58e3bec9f439a83bcbd83915d8152d1c6ecc42d8826c40712af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.8.0-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 84c73b198bbf97811397cdb2de4904699b7ffac0a41c2dc9c57f245fbc8152e8
MD5 2b4b428f19d2274d5cb596a85fdf8931
BLAKE2b-256 2e1db938c049bb2c7a22a3feb4103d9e48211ab3a6d89e5ea6ffd470d02b3d2f

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