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.

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

Uploaded Source

Built Distributions

nutpie-0.9.2-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl (3.3 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

nutpie-0.9.2-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl (3.3 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

nutpie-0.9.2-cp312-none-win_amd64.whl (500.9 kB view details)

Uploaded CPython 3.12 Windows x86-64

nutpie-0.9.2-cp312-cp312-manylinux_2_28_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.28+ x86-64

nutpie-0.9.2-cp312-cp312-macosx_11_0_arm64.whl (631.0 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

nutpie-0.9.2-cp312-cp312-macosx_10_12_x86_64.whl (647.5 kB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

nutpie-0.9.2-cp311-none-win_amd64.whl (501.4 kB view details)

Uploaded CPython 3.11 Windows x86-64

nutpie-0.9.2-cp311-cp311-manylinux_2_28_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

nutpie-0.9.2-cp311-cp311-macosx_11_0_arm64.whl (631.8 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

nutpie-0.9.2-cp311-cp311-macosx_10_12_x86_64.whl (648.4 kB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

nutpie-0.9.2-cp310-none-win_amd64.whl (501.4 kB view details)

Uploaded CPython 3.10 Windows x86-64

nutpie-0.9.2-cp310-cp310-manylinux_2_28_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

nutpie-0.9.2-cp310-cp310-macosx_11_0_arm64.whl (631.8 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

nutpie-0.9.2-cp310-cp310-macosx_10_12_x86_64.whl (648.4 kB view details)

Uploaded CPython 3.10 macOS 10.12+ x86-64

nutpie-0.9.2-cp39-none-win_amd64.whl (501.4 kB view details)

Uploaded CPython 3.9 Windows x86-64

nutpie-0.9.2-cp39-cp39-manylinux_2_28_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.28+ x86-64

File details

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

File metadata

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

File hashes

Hashes for nutpie-0.9.2.tar.gz
Algorithm Hash digest
SHA256 f917bf45166b6aaeb1563157206652d28fa3c34e97892b3f35435165724bf6d5
MD5 fde10168b124a7ab4a5ab44dc2dd5dc4
BLAKE2b-256 2dc6003c256c8cf5a635318aa196cedc0c75276cbedce727d9e3489508af23ad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.2-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 efeb187c5e3beeb4bf555a30578e1fdb32a672a66e49be1614d86dddb1715102
MD5 b39b57945da42b84b7779f48a510a58e
BLAKE2b-256 1efc953e0997abce05166323a499d36058f3c0dc18e33e970117b7797ed34460

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.2-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8392bd39c1d2b46afc9c4b047c0ec8b5fccb1a8d09ab360110239ace16b2b019
MD5 968bf747f5c45594b69cc1fc6de9cdf3
BLAKE2b-256 97d5b1753ba6f28cce63971d34b83ba40a22df3f0a0af3942e2dd4c4bc54ea30

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nutpie-0.9.2-cp312-none-win_amd64.whl
  • Upload date:
  • Size: 500.9 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.4.0

File hashes

Hashes for nutpie-0.9.2-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 fd983a3a4ac397315955bbe07f3fec7f626eeb9690e0295ee6d67eb7a40948ab
MD5 c78c4d48b231cb31ce6bdfd9f4cbbf15
BLAKE2b-256 11354d1f9d233865d420e1cc3b7bdaa81b37806ab5bfc4b013cdf676c26186f3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.2-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ad54154b37f1d4b11974fa1747ec39c2fe5662f38758be525c409ee102d08ac5
MD5 d796e4e23efa7064ff51e66458e2b087
BLAKE2b-256 57a35e70dd6b1854f95c7dc0308c0cd397f2ec4f782f7a4610d3bd6976a7e0f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a556712abe466d8c2fce0c4ce71285439885ad1fec5441f859647e3cb7348bb7
MD5 362dd1fdb7bdf9a02dec305a1c04ab3f
BLAKE2b-256 36f304effb3bc96429ad85740935a271b177cfc99255f13b88eafdb609133520

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.2-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 446e8a0ac806fce895e9316003d3b32f4bf4950be4fb8c0971b8f418669849f0
MD5 034eab09adc905d3db16374fb1900d94
BLAKE2b-256 0763d14f6f276ddd8c3a2d629b821bda91742b938b1810d0744f95a2e47e9979

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for nutpie-0.9.2-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 ab724cf073570248a50b1bbae3ac93c373ed7348027ad77ddb2d85cd2ac95219
MD5 6a9826468b89fa9673e92353f8be87ab
BLAKE2b-256 ab770134b2c5e083c3000b6677f501abcc36583d13fde8204853f1188a613a95

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.2-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8cf274cef0f888492fde95374d7131bb8328563bd2a7defe31a62453419c6db4
MD5 243e788cf04cfa939d0fbaf0be6ab5fe
BLAKE2b-256 5c38ebfa84b72765134e5be1c31d5e3c2639ffac1ecb7ce6e3cde9f18ee21ac7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 426e20ee8b8553b1908d0c249ebdb48226c58db990091090cb14c75556733f54
MD5 3dc27578e4d347570df8897b07be9422
BLAKE2b-256 ccf785484f34bf20726ab27b33746e3fe185bf2336d16ffc70039ff6cfa8eb9e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.2-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 68f9c38827d5086e1da600e98f4e1e902e1711b6780338c489f12dcbebf78cc3
MD5 c60ac4f2eede8f34811553d6c3249011
BLAKE2b-256 98110bfff7e5dca16c1169056b15e2f9f58b298f2298cb55731ea0deea9b3523

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for nutpie-0.9.2-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 23b5cc02333a39440ce9c851b830ab0d4a8ced43f75e5239cef1982f591a1742
MD5 4ee4aab0211731eeb0dc4f42d7b436ea
BLAKE2b-256 bea6f25d5dbe6086f3cc35a4f4dc2b8c9f19f1af9f8d9c1bd1971c9f7a742f87

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.2-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2910d4f6b61287e0e41ecd40ccf1a05d66f01c60313377f17c37d4b3c5c6c863
MD5 bd43eafaa756418ac7d5d1941c4d4d26
BLAKE2b-256 8530d58ba0df36f586123b4d3154c8d51ce874a6307d5f26564b499354e9d01f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2a5d92b2ad9b2a6946bb6e0b45725b9f205f2b6fd34061a3843e98f9920a46cb
MD5 d013a76cbba19473237ca727d38b74a8
BLAKE2b-256 a543418d0a801a7f3b8140618f0986306ccc144e5159af9411dd62c5dc2ba3b6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.2-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 41712de8089ba8c96c8fdc5f1376ff5c1a5ced423a870a19a1cab1ab9a3b2c23
MD5 d0afb0a0111a0e184e5945e52cc6a1b0
BLAKE2b-256 c526aeb6b93a01d6689eb77f23d316b9d1fcc9ab9bbf51e41a59260ea1da55f5

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for nutpie-0.9.2-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 e29af13f85a71b7512219f70e891653cbe3de7b72bab415b4675ba7afd7094d2
MD5 3bb5f4a1ae30eba891a3173ae5b3f788
BLAKE2b-256 b2e5c478134e5bd37be839e2a88f5a593b669691e312d7c08fafecafb4f88c87

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.2-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 538c05cf31104b92ece1adeb80a1456fc46dccdc6a908a4c2b28d7ba319eb8ff
MD5 81eeccb4b67871c87aa22f49f8d67b5c
BLAKE2b-256 54a75c1714d07e4e6a1f0c352c29403e31a8984c945079af843e53306d2a8693

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