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

Uploaded Source

Built Distributions

nutpie-0.12.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl (5.8 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

nutpie-0.12.0-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl (5.8 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ ARM64

nutpie-0.12.0-cp312-none-win_amd64.whl (872.7 kB view details)

Uploaded CPython 3.12 Windows x86-64

nutpie-0.12.0-cp312-cp312-manylinux_2_28_x86_64.whl (5.9 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.28+ x86-64

nutpie-0.12.0-cp312-cp312-manylinux_2_28_aarch64.whl (5.8 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.28+ ARM64

nutpie-0.12.0-cp312-cp312-macosx_11_0_arm64.whl (969.3 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

nutpie-0.12.0-cp312-cp312-macosx_10_12_x86_64.whl (998.9 kB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

nutpie-0.12.0-cp311-none-win_amd64.whl (869.8 kB view details)

Uploaded CPython 3.11 Windows x86-64

nutpie-0.12.0-cp311-cp311-manylinux_2_28_x86_64.whl (5.8 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

nutpie-0.12.0-cp311-cp311-manylinux_2_28_aarch64.whl (5.8 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ ARM64

nutpie-0.12.0-cp311-cp311-macosx_11_0_arm64.whl (967.9 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

nutpie-0.12.0-cp311-cp311-macosx_10_12_x86_64.whl (998.4 kB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

nutpie-0.12.0-cp310-none-win_amd64.whl (869.9 kB view details)

Uploaded CPython 3.10 Windows x86-64

nutpie-0.12.0-cp310-cp310-manylinux_2_28_x86_64.whl (5.8 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

nutpie-0.12.0-cp310-cp310-manylinux_2_28_aarch64.whl (5.8 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ ARM64

nutpie-0.12.0-cp310-cp310-macosx_11_0_arm64.whl (967.9 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

nutpie-0.12.0-cp310-cp310-macosx_10_12_x86_64.whl (998.5 kB view details)

Uploaded CPython 3.10 macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for nutpie-0.12.0.tar.gz
Algorithm Hash digest
SHA256 ee564c947aa1d5ece3d12528b8f85020c17fab4e2b05e705d34050074fcd00cc
MD5 2c83743b2452e16819647ca5e75ec55d
BLAKE2b-256 d425d9856fdffe5ad0e2fcf8a5606e6b4af186ad3bb77ef3dd74775049a477bd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.12.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b2c2a13ebd2a98146c97da6dab4c80d19773af862ff5c1cb51c233d56a3743ca
MD5 4aeba418aa812059b001833e3c31c79d
BLAKE2b-256 2e9d07a5501697a539d5fae14e7ad7fcf2db53c1b87eb0cb13c670e36ba63413

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.12.0-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 c286f7b94ac35830fd38702afaa8ed38714d1f36941cfb6d28cf9c3e9ec23957
MD5 4cde87369b2f5160b1e5111401dacaa6
BLAKE2b-256 8921720d2eb78572ef2470ef6d2a0c2f764cc371571f5ce9aa767cec05b62b0a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.12.0-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 0a234191017418e31d66f0e7469061666616b10ab4cff4b4ec717321775ecb03
MD5 8647072f98e1088d8508795c3ea9b55f
BLAKE2b-256 13ec7672447a8793ec35e0934f149caf3f348b324d430d212e7a1acb061cd90c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.12.0-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0f915157435232c6fae04791c7dd8871c3a4be608934defacde6586c1accacf4
MD5 3b36c10992568d43427b859dfd2d5b68
BLAKE2b-256 e677fb1c44e78387af3f6ed95534d45a43017169edc74ed77aba86613719d32f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.12.0-cp312-cp312-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 aafced24493c3e6d017bf648ac7f4df13274ce1f8e123757e0eed2f01b6066a6
MD5 646aeb95cb51dca9101de5bb8710af3a
BLAKE2b-256 c95797327c113d75816c6bc36168babbadbad1f267a8b5bf6279c82915fec46e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.12.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 570acfd1aaf4d820be0841a0126222fce086aa97083c0ab1e1ae1eae44ce91bf
MD5 7565579b83bb73558b98020980837a6f
BLAKE2b-256 08712c7a9d3336976139678a31b5511556eb30f029719a2f491df8ddf08fb02b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.12.0-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 04b5af4b033c04cdc693463d25f8adc18303797a7bf7a305c06a0309d4bcd2b1
MD5 2f1f4a58605e4c76540f97a9583ad16e
BLAKE2b-256 e4c3702a85a8d1e5be5edabf69073b771c28b7f2785b4b96f06dc664d2dfc701

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.12.0-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 8873606c2ec4ec7e683a01cc1e1e9e97d758cd59a6a17c06d2eb5aef420d5344
MD5 abe2f5de87dfa540098edb14b00899b4
BLAKE2b-256 8546c8aeb0237b1571c668238150887db94b73eca27ce9d14d5641dabb20e797

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.12.0-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 eb0de297d07c47295ee893402f7ce33502c22c134e42e1d31f0253ad1dc22919
MD5 d87715e5db9123a3db397a6ad11152f2
BLAKE2b-256 6d5a7e75a50aec60bde68b1cc6f67eb3c58e2fc05a9b371742133629a0b7962c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.12.0-cp311-cp311-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 db6ce530cb2701575cbf9a3ce780349d8efeff2cfd3ca6a683792817add9816f
MD5 eaae40718887303691dd5567802b09c0
BLAKE2b-256 6c831ad8ea8a06454b7d6c4ab330bd9f244f4568f3cf647eeeb433fb140817b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.12.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8e0d13eb4f6a365b1817508f01eb4507058a7531670919e769f2be0fbf0812c4
MD5 411aca163c3a11fee432f897fff9eb1a
BLAKE2b-256 bf672f65c198f4eb8b8ceb6621a66e9493a8b8d24111b5f3ad2483d579ff70ee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.12.0-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 08429b0523c3d14c190c87cd636e048df57b74dcaa86101762cab3c863ba2293
MD5 b857e87a4ae4acffe6c4eecf7e0c1c5a
BLAKE2b-256 aec8f2dd401a08c44d5ac07912b188fa16b35b5b2e786ba6623100fe4515e2d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.12.0-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 cccf8f82e9e2b37d11064025a52aa77175394f7f4f532e35ec7b7f0250bf93bb
MD5 5889313512fa1573b03156efbd365699
BLAKE2b-256 658cc5a073be24bd3761d398aac35a32cc6e17d038317ee50394d4a8cb46fefa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.12.0-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b3e56ad3b70e9515c2de1b7a8d6e3aa0bab418e4baa6b01b7ad3366a9d6a863b
MD5 63563d49b7d43ec3dbc97f1c41c1997c
BLAKE2b-256 bc46608898214f2b8f49a0485f989985292aaecc8bf4229f45385600d96bf6b1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.12.0-cp310-cp310-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 5959a14e6efab53de109cde8a27a25ea3d1fe2b5a8001b451fbd5f55fe2286da
MD5 ef268bcaddf5b70fd05af75c37c650b7
BLAKE2b-256 8c03b16064a67bd92bf465a55a3905cf1a62bda965e976964350165e18aeee3f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.12.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8d4197d9ac2e142440eaf215fb324f203dc660db4d32e20a3c12efc94e829d25
MD5 1d54d1aefb46b8b42b0fd664e6a86a22
BLAKE2b-256 977638a6fd721a774029b6cce09b57a2237f4a7aeb33b5473d5ca47fb57646cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.12.0-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 8ad1ea72714bce381cca334a84cfc5623a6a9489fc7717720410547ffb9a32dc
MD5 b5529ed410a57b3a631967e2318b9c26
BLAKE2b-256 1101509b2b55cd6bc4c940df2d2c041c16af741876848037627c470ae62acf25

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