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

Uploaded Source

Built Distributions

nutpie-0.11.1-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl (4.6 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

nutpie-0.11.1-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl (4.6 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ ARM64

nutpie-0.11.1-cp312-none-win_amd64.whl (653.0 kB view details)

Uploaded CPython 3.12 Windows x86-64

nutpie-0.11.1-cp312-cp312-manylinux_2_28_x86_64.whl (4.6 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.28+ x86-64

nutpie-0.11.1-cp312-cp312-manylinux_2_28_aarch64.whl (4.6 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.28+ ARM64

nutpie-0.11.1-cp312-cp312-macosx_11_0_arm64.whl (747.7 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

nutpie-0.11.1-cp312-cp312-macosx_10_12_x86_64.whl (775.4 kB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

nutpie-0.11.1-cp311-none-win_amd64.whl (651.5 kB view details)

Uploaded CPython 3.11 Windows x86-64

nutpie-0.11.1-cp311-cp311-manylinux_2_28_x86_64.whl (4.6 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

nutpie-0.11.1-cp311-cp311-manylinux_2_28_aarch64.whl (4.6 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ ARM64

nutpie-0.11.1-cp311-cp311-macosx_11_0_arm64.whl (746.6 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

nutpie-0.11.1-cp311-cp311-macosx_10_12_x86_64.whl (775.4 kB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

nutpie-0.11.1-cp310-none-win_amd64.whl (651.6 kB view details)

Uploaded CPython 3.10 Windows x86-64

nutpie-0.11.1-cp310-cp310-manylinux_2_28_x86_64.whl (4.6 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

nutpie-0.11.1-cp310-cp310-manylinux_2_28_aarch64.whl (4.6 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ ARM64

nutpie-0.11.1-cp310-cp310-macosx_11_0_arm64.whl (746.7 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

nutpie-0.11.1-cp310-cp310-macosx_10_12_x86_64.whl (775.5 kB view details)

Uploaded CPython 3.10 macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for nutpie-0.11.1.tar.gz
Algorithm Hash digest
SHA256 9a937898a91415ac703b0de41d2d328045a8a39a0319fafc34ca25382b8d6c04
MD5 d2edb73319b2d43e39360e0c6adad6c7
BLAKE2b-256 e07c810e157e839895d9eac8886773f3110bc5acdda0287343fb642349d755eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.1-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e7469e1331a94d343996f7b8c313484c0a2163183f9a65cea155a4fa146005a4
MD5 321c2be82d1f038e7a4389212f09419d
BLAKE2b-256 1434cae3cb80431e0317a51c98aea5c37a291d7dc28cffa0b2d41c8da0c3a085

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.1-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 38ead17714a78d9ebbbd27d1c712c5f5f46ca7c44fb79fa8da96a6fc2d3f18f9
MD5 1f9fd9e3cf7ba75cbf551445ecd2b3f1
BLAKE2b-256 3f60b6479cfc4849ee64ba8ea6aff6789dae0dd9f40b6f02b6dbc385baf52815

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.1-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 470a08326b077a0ccd92adb1a9ae6d4d964814cce55c3dffaf703808cfc0c4cb
MD5 332ae51e978d9a6ca9715b6ddb1d58cf
BLAKE2b-256 a6450ee9d45263176ba6f154a140bf0f368fcfeb510d21b1005b57ed80b5b2c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.1-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9e36657a0f9baf4cf7a0256d68e3b92252cc886943504a4098c97539ed9be408
MD5 15bf94fe626f2a66eb0f6f7666d91d21
BLAKE2b-256 987156b6c8c12ffd56e4e500b0b90252a4db48b3723b5d9db011bbd563b9ed7f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.1-cp312-cp312-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 a504c4a41fb2680aae283da56d2b88b3f3eb6fc3d6de3e3cdb378e5339bfda80
MD5 6c13ca203eabe023d4c8102ec81cf582
BLAKE2b-256 41c8fbd0e7ec78a661e761d58c437c9abcb01fd719d917978f332cc57bff1fe4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d229a43d91c9071be388d88543713521a8984c56aad58032e6603ff630688e98
MD5 78a62987dfb648f8a2b05f84c262a1e1
BLAKE2b-256 c20b95b9281ad90244e2b2f997f6ece1c114ec0d73e122dc308b4e6086c74eba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.1-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e2063fa030faf7b5685116e4e1cebbf5d0721f8bbd2f782ff778f9af06ee052b
MD5 1659f96b418f496653f1d28c61d9ec76
BLAKE2b-256 e71de1218f160d425737a24b541934606a9552b15e491d5ffa4d864edc70a3c7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.1-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 010e2219e83895118bc1381e9b838684ee211b128f25cd51dd4ec178907fcc61
MD5 a933afca37a1688599a065613c998558
BLAKE2b-256 8cd37af61a6174ea3531704fd0f796339374c3a98612ce64353d945be90ba877

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.1-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0418c1b0940beadc689d130e3d71ef28e0830b3ee58963ef4c2245e3e9ce29a9
MD5 188d606070876e754f12330155880e2e
BLAKE2b-256 34ee514d7e6993da3be2ad7059bdd33cfa8201ca35367189a01064b06543250f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.1-cp311-cp311-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 4f2a53ea4e6d07905647dc772433935a0fbb45f752a5f32be67a3c94f854d6aa
MD5 0944568c6318ddc1040c33f8e7054a75
BLAKE2b-256 6fb5347d18532aa5c2de844bc3771b407425088977e9bfcbcb2a6be4461d0db4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8adbcb42fe1a3630593b486243d2c722b251ebeb0fe2f6e31b636783bfea63da
MD5 dd200c71bce67e21b413f3fafe05d24d
BLAKE2b-256 f02a6ab7e78415908b374544a82358ce1f65af938602b1de1d846db492883631

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.1-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 679e9c0de1ee5c64beb99266ceadcdd72c39e3f6b6378207c9b3d1cc9828b988
MD5 859d28ae023a1e73ef2147c19a7c983d
BLAKE2b-256 342cc6d0de52fae1ababe9a032ea64bcd39d71edb3492a18bc1c0e9e2564fc33

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.1-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 bee1e6420269631ced529665698f1ee2b5a9143b469c6689b42fd411d16729e2
MD5 62f3f426111a3314a9ce6607d203407d
BLAKE2b-256 f1a205a36a9bae8b1d4f95bb2aacb1dac7d07ddcdd5d20c8bb95d881edb482c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.1-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f269652e4f3cd1085a32b3fe3bc40dbdf8533a6aa03da2c9b2b76a8c5acf3d95
MD5 458bfbc0f6dfc18cdba8b5ff8646ce00
BLAKE2b-256 cc6eb3e3b88c944a0d2973a254adcc1aa4fcc80287a1edb57f4dac04d07b4688

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.1-cp310-cp310-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 2d4630f49a40d0b84f0f812e2181c75b9c6142cfed3ea36261a70a6c2756e865
MD5 7037bea41d0239ad70fb7320eb51b468
BLAKE2b-256 23c75202f209dee5262385f77a0528732d9d8f70e73fc22b14d0f58477de0e5c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a76d092f7c5acbb7ecb7312b7aab564a40eca5ad608e5cf5107615f4b20815f4
MD5 01d3ad7d4dfc7595988eab554329a1da
BLAKE2b-256 fe1a1e72e8be9324203a841ecd9a8ce0a7cc14dc751cd560a1a76ebb67b50494

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.11.1-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c07c3da2d6bc27a7610e6dd228b6cdd11b43ce64d2aa6f1f138651ecef174895
MD5 8f96e045b3c1376ec4ae6ca0d8395ad0
BLAKE2b-256 7d3dbcbadf8dcfacd6c42ef9772c335489ba9d608e577e13ac5455e2d56b261a

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