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

Uploaded Source

Built Distributions

nutpie-0.9.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.9.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.9.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.9.0-cp311-none-win_amd64.whl (505.0 kB view details)

Uploaded CPython 3.11 Windows x86-64

nutpie-0.9.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.9.0-cp311-cp311-macosx_11_0_arm64.whl (676.0 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

nutpie-0.9.0-cp311-cp311-macosx_10_7_x86_64.whl (706.4 kB view details)

Uploaded CPython 3.11 macOS 10.7+ x86-64

nutpie-0.9.0-cp310-none-win_amd64.whl (505.0 kB view details)

Uploaded CPython 3.10 Windows x86-64

nutpie-0.9.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.9.0-cp310-cp310-macosx_11_0_arm64.whl (676.0 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

nutpie-0.9.0-cp310-cp310-macosx_10_7_x86_64.whl (706.4 kB view details)

Uploaded CPython 3.10 macOS 10.7+ x86-64

nutpie-0.9.0-cp39-none-win_amd64.whl (505.2 kB view details)

Uploaded CPython 3.9 Windows x86-64

nutpie-0.9.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.9.0.tar.gz.

File metadata

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

File hashes

Hashes for nutpie-0.9.0.tar.gz
Algorithm Hash digest
SHA256 02331901b00cfcf97288db127c1b92ac288b6b8c5d91327c12e75b0883166e8d
MD5 fd9aceb246ce9ff9cac8c122bcafa837
BLAKE2b-256 0bc6da521b67ea9814ac81041c8a69872b17a2656ae2676fd2ba29f87256c2bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d49d9b7a792e3bb82bf622918599a5f9f24ec54e642ac45d9a3f82010c34957d
MD5 8d3e438c92451f2eaad1fbd1cba71a96
BLAKE2b-256 f50c7d99291ef79245b42219160a307c1c76569d2065ba5407ef194b8a118579

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.0-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6ad2c964150d3c1182e96a38afe00843af5e818dfd2881a2aa7266553de6ec18
MD5 d751e476c52a1ab5cbf31b624c18e2ac
BLAKE2b-256 94c8b3afa439e216fb2ed9e44c4e01d693c3ef5d64f3d105994491de1bdd6dad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.0-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2da7bc1848cdc8803f5171f5389690d2a3104ee6b85540d2a467ba4bc2d73147
MD5 f00230a9828473e28649c191d47dce53
BLAKE2b-256 b8e695e17d16621ff0aa08f75790392c60b104c99e9ad75f06ec79694592da6f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nutpie-0.9.0-cp311-none-win_amd64.whl
  • Upload date:
  • Size: 505.0 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.9.0-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 585d1064d1a6ef3362af410154920e8b36836d7a6f0650708c7a90c62c4dff67
MD5 68fe69c34f1ef5b20f1a91afd7a76e2d
BLAKE2b-256 b3792813223fd926eef908a157ce6d42448cf85656c8b421f3d18f578ee279f6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.0-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 07c3120b9faa4eedf796df78fe259b68a1dc6122c5bf09a2223b4dbde90bc3db
MD5 8047e8e2f7130c763c381bd5b281a559
BLAKE2b-256 789a750a0077954e34fe15225986702fea30dedf16fcc8f6a0c579387c903587

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 258e0b68fdb44250efc2ac48db64e665d330bc1f58ff16d509874754ac636842
MD5 a32d6f8148ebd55637fd67c4173c3747
BLAKE2b-256 7b1123e07a1cf9fef605adab9f9f673e36a1e81a6d7598d47b7e9cbf93918e60

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.0-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 d803058ceaa876891795f4818ca0d930d49ef46d116b4d3cb57834fe49c21cc6
MD5 c0c1574a0239b2db364919afbeb4463a
BLAKE2b-256 f2c8b0cdc0e8e71cd51356356463c299ccedbc07b943f2b5fdc57a8e5ba1c793

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nutpie-0.9.0-cp310-none-win_amd64.whl
  • Upload date:
  • Size: 505.0 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.9.0-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 2457f6be4edd7d907c317a8315c8560546b35b1ee57c05ac59d88a729103f6d4
MD5 b5fe2ca88b7d1de0f388a9228b8536f5
BLAKE2b-256 07952c5cfee58317decc6c5bb070fec597d9efd728f3673a897e3d01d867805d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.0-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7cdf0dbaf8934119cb185c95bf6afda0bd4cc091c99bb46d271ca48ad11f1de2
MD5 87111d90576c4c27130f14fbb4049d46
BLAKE2b-256 c2d1f60386340c62a133c9b0fa35bbc4f02620ff506eda9bd97b4df4ac47be68

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bf7b985f6656a5266031ca9f59660ab6ed2cd8c9136b413298f903b69994c16d
MD5 3977b49128cb817b6c5e17d09f238df0
BLAKE2b-256 861e649259e8e2c892f738cc017d718bcfd26ca4a9b4d286391668d49fbfb015

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.0-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 a79360d689b1341c05e54b230395b1aebc6514db5eb7ea9d8004714a946de2f4
MD5 5c0873a82c07d9ef5af2166e4e12f0b3
BLAKE2b-256 a99395a5c5bbf2d7bb450621af36cef6c691ab45309bcf4f3a583f56b821341b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nutpie-0.9.0-cp39-none-win_amd64.whl
  • Upload date:
  • Size: 505.2 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.9.0-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 cae1e944c7fd4d74782942f3d38e0a410c893da0e47f107d1eab9b50a04a9849
MD5 3903bf3e926aa7af4be23e6bf0c7f381
BLAKE2b-256 b70faa2a55abb7bafb9620c52a0b7d666362b246eab6c15a7100fad755b8ac80

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.0-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4b4fff02ad4a2047c9fd38246c66d417c85b303e262f5dde06c6ea0812ec69a6
MD5 0d9fcf59444af9b67d67d73672dabf0d
BLAKE2b-256 bf43421346b22d57ec69592fd42b0db15776d467e045d61c159e0a72155161fe

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