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

Uploaded Source

Built Distributions

nutpie-0.13.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl (9.5 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

nutpie-0.13.0-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl (9.7 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ ARM64

nutpie-0.13.0-cp312-none-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.12 Windows x86-64

nutpie-0.13.0-cp312-cp312-manylinux_2_28_x86_64.whl (9.5 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.28+ x86-64

nutpie-0.13.0-cp312-cp312-manylinux_2_28_aarch64.whl (9.7 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.28+ ARM64

nutpie-0.13.0-cp312-cp312-macosx_11_0_arm64.whl (1.4 MB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

nutpie-0.13.0-cp312-cp312-macosx_10_12_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

nutpie-0.13.0-cp311-none-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.11 Windows x86-64

nutpie-0.13.0-cp311-cp311-manylinux_2_28_x86_64.whl (9.5 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

nutpie-0.13.0-cp311-cp311-manylinux_2_28_aarch64.whl (9.7 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ ARM64

nutpie-0.13.0-cp311-cp311-macosx_11_0_arm64.whl (1.4 MB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

nutpie-0.13.0-cp311-cp311-macosx_10_12_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

nutpie-0.13.0-cp310-none-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.10 Windows x86-64

nutpie-0.13.0-cp310-cp310-manylinux_2_28_x86_64.whl (9.5 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

nutpie-0.13.0-cp310-cp310-manylinux_2_28_aarch64.whl (9.7 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ ARM64

nutpie-0.13.0-cp310-cp310-macosx_11_0_arm64.whl (1.4 MB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

nutpie-0.13.0-cp310-cp310-macosx_10_12_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.10 macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for nutpie-0.13.0.tar.gz
Algorithm Hash digest
SHA256 26ac629127936b81a46e77491761a43b2f227b94b1641bc76d62a5da6df23765
MD5 56fc00c085f80325faa9cf7961cdeef4
BLAKE2b-256 d8e6c0275bc7c02bf67328632008f29d4d2e78515c2ae7b091278580d8e717fd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.13.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e1fd4d0848b5d81d9c14e8f1e4883e3f6f69de9bb6468e6460fcf78b7757201a
MD5 54c114ac41074651598346a3b53e7264
BLAKE2b-256 d30bee70e3bc70e3f423d6d11b788a978110d5158a48d35a18fd2b6645d2913e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.13.0-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 0f11100fa545ef79d8b620ea7c4480c27bfcb4d9d9829c5c91d55a7d4b6312ec
MD5 ea984014448284e3bf20a720a8327823
BLAKE2b-256 b00963b1f9ecc6fe6ba8b512e1b13fe8d358875ebf976358d51776aedd448986

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.13.0-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 a27dccc5e3a532a81f74c03f7c6fa0b971e1aae502d558dd75f54fa38e43af3b
MD5 946b281e84c1b797a3c0841527bfe409
BLAKE2b-256 7d3fb0a91e2ec452a7bfa3d5fd9cf2cc40bddeef8c1a092a4550d56ee81a5702

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.13.0-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 cedfc469c52d738f1879568ba73b7b0325348421ff04eb314d49837e8dd5c7a5
MD5 8be2dc7f5150fb031342da2ac3033290
BLAKE2b-256 d0a1e0fa5f47c5fdf3299090d09f0e7bf22c0d78e7814fbed8123cfcb645c96e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.13.0-cp312-cp312-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 531243c06ff3ab5665976a24a76b59ff7bd759065225600f925054ebb2f39ef4
MD5 e9487dfb6682ea94dde9856dea0a4385
BLAKE2b-256 a3feacdf692ee84ef3b144bbb2b2c065dc5101c8de5fe65c37f7c22fc92329a5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.13.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 397d6b59973d179bf674337aa32aa89d05195f9ea6a35991952d0199f057cc02
MD5 fbba6028a4f3b0bf9933c3f5ddb07c18
BLAKE2b-256 8edf1f922c0a8186b4e4b79cf7bb1b7c51449685b08b525d6de7515f932a57fb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.13.0-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 44ea7ccda94e7127a5e701070d6b332f2d7f6c72d8ae0379adf9387f98fff1c5
MD5 0ad7414c03d15a2898c191647e684ef6
BLAKE2b-256 963639f734ebbf8c76cf4bde7221d10c4ca4c476906cb7bc6cd0f830ca632233

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.13.0-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 949006625b1bcc618fc6746f36939bbc90c9a203e79aa020b77358b8c8ff0901
MD5 47f1640963b4c10a489eda9402e64df5
BLAKE2b-256 dc77236d12bf0a8263c765511476562105a2af76fd62ea4d7c1600302db13979

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.13.0-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ce9cd2890c804f21cce35061449c6ec48d35c7f169e0ced61ab1e451f80c5f2e
MD5 f405bdb4ae6ea3b44bbce8855a74b9b4
BLAKE2b-256 6e68f37cc791be1b1184271c8dd9602cd97a8402f4f79689cf9f987547f9e5c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.13.0-cp311-cp311-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 f1a173f18f70fcda2a379bd5afa193aaf1e736ad728829ffcb29bdeec642040d
MD5 efc54bbafd6544a55fcc3b94161534cc
BLAKE2b-256 f36633674a8a7868067068f920000b7c732906a05226f59bd906a64069b9454c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.13.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 58cef7df8dca7f5ce2edce63316396cf1259658c168708f51860310d85d33747
MD5 81c841dcf9771b8c58fa96388c33c12b
BLAKE2b-256 bc257e992c283b8beb241daf73fbdb2d6407e984bacf30178a672a4799705e2a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.13.0-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 804b4866a01c4df411418231f9112f0bd922342ab41f5700bfec0872a3abc324
MD5 db6e1c67d13d890d2d721f9701c37998
BLAKE2b-256 81150dcdee2f3ceb67ea87fe8aff12e424599af7c3aa47ea5bfab0cbae185f53

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.13.0-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 5c4283a024b1a41cc6fb6d01ccc5de2f1a577fe873f6b8928ad929bab5ee6e5f
MD5 686369018915b6b1501b83c6e0229d47
BLAKE2b-256 fcfef0c383467ec5b53de8893df47bbb78d0e2ace4ea6d39e2e3c8786a3ea8b0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.13.0-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e96e71db23dc7023637fc6c666e054d576af878e793c997d399c09ee956c5e8b
MD5 7d443c96fa5d8394e4041339361f378e
BLAKE2b-256 fa4faaaed63dce60a80b510b8fb4e21300dfc5eb0b4e19e671520ae4a1360343

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.13.0-cp310-cp310-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 3336907b723e293d8811ad938a4996ca83414d628ed37376d31880a01f7a572e
MD5 0218173f805b9d95d7a8f213ac683b58
BLAKE2b-256 da3eebc903d0df7d8764fe6e4b2c7f4c60a1daaf3ebacf419af84b452492768d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.13.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e258f993bbf01fe23e3b98e376b49afca62e321e78726cae7397b1b20f1425e0
MD5 a4ceac03760bafafe84486d3eab99d66
BLAKE2b-256 b4b610c2783d4940040b745d04f1e531122714b5849b6fd48a8e0d388e049640

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.13.0-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 993c890fb0bd3e822b6114b63fd14aeecd2f585eafb3fd609bc3d1637a08d25e
MD5 579eb28845e4408409f0f4103392d551
BLAKE2b-256 cd204ea54bfce0643f08b4f3c766d50870cb3cc3e9785c2b51a36ca5eb362b26

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