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

Uploaded Source

Built Distributions

nutpie-0.6.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl (3.6 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

nutpie-0.6.0-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl (3.6 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

nutpie-0.6.0-cp312-cp312-manylinux_2_28_x86_64.whl (3.6 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.28+ x86-64

nutpie-0.6.0-cp311-none-win_amd64.whl (469.2 kB view details)

Uploaded CPython 3.11 Windows x86-64

nutpie-0.6.0-cp311-cp311-manylinux_2_28_x86_64.whl (3.6 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

nutpie-0.6.0-cp311-cp311-macosx_11_0_arm64.whl (636.8 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

nutpie-0.6.0-cp311-cp311-macosx_10_7_x86_64.whl (669.2 kB view details)

Uploaded CPython 3.11 macOS 10.7+ x86-64

nutpie-0.6.0-cp310-none-win_amd64.whl (469.2 kB view details)

Uploaded CPython 3.10 Windows x86-64

nutpie-0.6.0-cp310-cp310-manylinux_2_28_x86_64.whl (3.6 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

nutpie-0.6.0-cp310-cp310-macosx_11_0_arm64.whl (636.8 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

nutpie-0.6.0-cp310-cp310-macosx_10_7_x86_64.whl (669.2 kB view details)

Uploaded CPython 3.10 macOS 10.7+ x86-64

nutpie-0.6.0-cp39-none-win_amd64.whl (469.1 kB view details)

Uploaded CPython 3.9 Windows x86-64

nutpie-0.6.0-cp39-cp39-manylinux_2_28_x86_64.whl (3.6 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.28+ x86-64

File details

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

File metadata

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

File hashes

Hashes for nutpie-0.6.0.tar.gz
Algorithm Hash digest
SHA256 f8fdc22216959a49effc13cd1eb0e869a4a4b6bb4605af7b588da04ab1e094d7
MD5 59da2e7db5166e52f02c279652a91480
BLAKE2b-256 5897f77529aece5ae07f28da4f7d3e45737643c36d4251039f873a98463a9189

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.6.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ae4d666db1d02187afe6685fb111c7803637e83ff3f0ca452e1dd624dd3b46d3
MD5 5772d0496b6dae9643b145017b684d6b
BLAKE2b-256 a576debfbc67bc5a950935bfc0da0b2fb0990db1b6e3a5eef43eb4c2a114a766

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.6.0-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 cd143dc48496f8e0f76e829fefb0e4e34d65f204fa79c6832c9fe589a239140b
MD5 a418525d46377887729ed1344a478de4
BLAKE2b-256 298a7b15fa931a4b4370b3f6cf36a04a4e0088703abe207842d30cb846c37e49

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.6.0-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 63e8cac0f3fcbaf05500a11b82f38bff0da2e29569fb7b854669dfb03cc70a03
MD5 4c46acf33b738f8c69785212bab873be
BLAKE2b-256 198c2ea52db7de9a47e7263eb19afec48f79cfee41a58efc4bca7991e36774e4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.6.0-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 b6fd1f87256cb1b3884bfeaa62a4255e79e4754b2eb975c94a1df8af305b6906
MD5 6e81bd9f0e162d97eaf433eada833269
BLAKE2b-256 b5234e3187c3bcc97c0ffcb25ca8f876601bfaea147946eafbe69ed1860296d2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.6.0-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c96363c7d90edffd5252cf8b61899d654feac9fcd833b1aa3a518175e7d2e344
MD5 c18a5251c856c6406fe05f395e0b028d
BLAKE2b-256 674bd6852c5fbe5cd5402087c456a017ec9318dd40bb2e8d61e133a946f5d8fa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.6.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6028ede5b1896ea492b74412de0d7afd9c1d0525ba553dec4244fd84cac733bb
MD5 f2f6d67d10f27470f657a54841f1613d
BLAKE2b-256 0618dfeb1f1ecd64135568d17ab83373e121f1a853c77da3fb25311c506e2ef8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.6.0-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 5b53b91f9d7f8e35ff877571bea76cb5ae742342fb0d5ef8f31841dc2e2e30f5
MD5 53c486a32161b240117a6cd6b0b975d4
BLAKE2b-256 5021b1a7b38acd3aae3156fcafeb650ecfc03988fefcca3f4fb1cc1fdeac819d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.6.0-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 51bfb2b171f17fefe7e7d896274f817198d848e0e3db0b101f71ccbbe117c2bd
MD5 4ee78429187202632cc75863d907a2d6
BLAKE2b-256 d1166af522d54157ae8e64ae1d7b3aacfb1b7874f4204eac26db1e98217d0a9f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.6.0-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 997497b8e9a11000d5acc45ee4fb9e431692a1107bc46522fc2c0f91e85789e9
MD5 c25b9ccbb67b00e7b5a445f2449ea537
BLAKE2b-256 4f293a039131db86763632c6bb9df190a33c8313b30ccb1793b06dac7cfa427b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.6.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0373d29ede1d243c8431f016d1e4039a6b96997d59c49c93e8f4011d4df5fa58
MD5 aa9bc843b513f9a7912635e9bb095524
BLAKE2b-256 7bd0be74cc2c6279400a0bdeb0aec4d3a361463ca903d7e13c6054ed8845d75a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.6.0-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 5ce2834f56ea4e5d1b6334e3b51df08821a48a10b845e156856cae06060f327e
MD5 335589e4c4f009c82572c4e9cdb01511
BLAKE2b-256 b91445b8aaf6e41c6c04ee9c23bf9d26e73686012adf37ed89b66669b40bfd04

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for nutpie-0.6.0-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 4040bdbe566e0d955c32e08455a54923cbc06ed30ef5eb04cf866a1ea7e53b85
MD5 caae0983ff2583a76f993281d5db3dfd
BLAKE2b-256 3d37e17f2e91f3f24b9fc2e4c2407e8046f2d5f0a5147e06e17eb8d5c9c4fb41

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.6.0-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 446af8bdf41a85047e6d359f613612ae62ae6fed576747c47034a6d4fdf3fdcd
MD5 c7d0fdb132c2414786dbd8de9dd8933b
BLAKE2b-256 01315f5b77abdf375b2cd8b73844e30f90f4b4b4c9b40b80c2014db360d64a1e

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