Skip to main content

Sample Stan or PyMC models

Project description

nutpie: A fast sampler for Bayesian posteriors

The nutpie package provides a fast NUTS sampler for PyMC and Stan models.

See the documentation for more details.

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)
# Note that not passing any timeout to `wait` will
# wait until the sampler finishes, then return the InferenceData object:
idata = sampler.wait()

# 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.16.8.tar.gz (711.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

nutpie-0.16.8-cp314-cp314-win_amd64.whl (9.1 MB view details)

Uploaded CPython 3.14Windows x86-64

nutpie-0.16.8-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.7 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

nutpie-0.16.8-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (8.0 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ ARM64

nutpie-0.16.8-cp314-cp314-macosx_11_0_arm64.whl (8.4 MB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

nutpie-0.16.8-cp314-cp314-macosx_10_12_x86_64.whl (9.2 MB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

nutpie-0.16.8-cp313-cp313-win_amd64.whl (9.1 MB view details)

Uploaded CPython 3.13Windows x86-64

nutpie-0.16.8-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

nutpie-0.16.8-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (8.0 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

nutpie-0.16.8-cp313-cp313-macosx_11_0_arm64.whl (8.4 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

nutpie-0.16.8-cp313-cp313-macosx_10_12_x86_64.whl (9.2 MB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

nutpie-0.16.8-cp312-cp312-win_amd64.whl (9.1 MB view details)

Uploaded CPython 3.12Windows x86-64

nutpie-0.16.8-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.7 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

nutpie-0.16.8-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (8.0 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

nutpie-0.16.8-cp312-cp312-macosx_11_0_arm64.whl (8.4 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

nutpie-0.16.8-cp312-cp312-macosx_10_12_x86_64.whl (9.2 MB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

nutpie-0.16.8-cp311-cp311-win_amd64.whl (9.1 MB view details)

Uploaded CPython 3.11Windows x86-64

nutpie-0.16.8-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.7 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

nutpie-0.16.8-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (8.0 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64

nutpie-0.16.8-cp311-cp311-macosx_11_0_arm64.whl (8.4 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

nutpie-0.16.8-cp311-cp311-macosx_10_12_x86_64.whl (9.2 MB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

nutpie-0.16.8-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.7 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

nutpie-0.16.8-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (8.0 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ ARM64

File details

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

File metadata

  • Download URL: nutpie-0.16.8.tar.gz
  • Upload date:
  • Size: 711.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.12.6

File hashes

Hashes for nutpie-0.16.8.tar.gz
Algorithm Hash digest
SHA256 093c16755093d47a341649c508c196d0665ee20d930642a6269186098170a638
MD5 a4b1434b9c456c71e263e12c68f7ed34
BLAKE2b-256 9d5a8f07112293d50682c74bbf86f4af5e76a69dcf96708efb0e8a6bafbe32ef

See more details on using hashes here.

File details

Details for the file nutpie-0.16.8-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: nutpie-0.16.8-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 9.1 MB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.12.6

File hashes

Hashes for nutpie-0.16.8-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 80c067162053510d41ab4635a14bf38328b6d531d0d68e896ca95e9024b31823
MD5 9929cd9f24ad4a73f6dc1e0d3262cf43
BLAKE2b-256 f9ec90aad618ba8554a39e60d3f6e63aa5b9eaf7096b1302840129d8de3373e0

See more details on using hashes here.

File details

Details for the file nutpie-0.16.8-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.8-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bdfe312470cc541bab3d5fba74b91a2507c47992429294c81177ac5083b4ff7f
MD5 1a15152be124c63a71de0c94066446a7
BLAKE2b-256 5049a19d27bddc9885028d1bfbbf40d5558ea6ad5b706c9a1845a3501627b3c8

See more details on using hashes here.

File details

Details for the file nutpie-0.16.8-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.8-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 eea5fb63140865d71dd219a179cc0d9b34dfb4f87d6c527b7da2663cc02e28ff
MD5 27ae5e70a610c9638fe8ff78f4a53d17
BLAKE2b-256 1cb4ced080c79d7b1fabe75107d14c9de0eec610a59343ea1d12baa2406eed71

See more details on using hashes here.

File details

Details for the file nutpie-0.16.8-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.8-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 22d20d1130c946a642aeb792104066c3003c8193c3a83ebc363017a3902a58a9
MD5 11f0651d366e896b20423f691c751690
BLAKE2b-256 436a4bd0c7f34e9ce0644521261c9829191e0a85ead5bca4409c16b24a65ba67

See more details on using hashes here.

File details

Details for the file nutpie-0.16.8-cp314-cp314-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.8-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 00bdb13166f3b876f09f67276e272cb720cd355d31cba5a57bc00fa9baaaada8
MD5 91fea378c42973b5892ca8d6195fcf8a
BLAKE2b-256 30094cb4972c3c0602bf9b0663c70541db7439746d86f480b2675db89dfe13d1

See more details on using hashes here.

File details

Details for the file nutpie-0.16.8-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: nutpie-0.16.8-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 9.1 MB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.12.6

File hashes

Hashes for nutpie-0.16.8-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 fa6a33d1746da1d4a59897dff47dd43d52a3aa241fdee32413dd516f00e36a64
MD5 56a909f4b5c5d4050f4ebb7baa0c0de8
BLAKE2b-256 467f5018b05ea8fe5afde9b457fe2080b2f3260989f72be5afbc85ccc5b64bd4

See more details on using hashes here.

File details

Details for the file nutpie-0.16.8-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.8-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ec6663e375ecc3ad0eb3c629118224e625711e098e01459fdb9f6bf33e0ec582
MD5 31dbf5cf4b0bebb5f1ed574aeb422b51
BLAKE2b-256 fc75f8c18ad78dcbeddf94389d751c553cc48883c3604ebda6e7845b2ae279cb

See more details on using hashes here.

File details

Details for the file nutpie-0.16.8-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.8-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 6d0ad9744b6d9e3c3130ec5310d33006bfd4c90045747c8112dbe6b776842ecf
MD5 6127323b42499f89a6660a91e26a8520
BLAKE2b-256 8973823c2d83fce232974e257e52b65c294634430793f20c75def9670f4b7551

See more details on using hashes here.

File details

Details for the file nutpie-0.16.8-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.8-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5092a94dbc271bf479b3ed55e01e21c8ef5cbc11a3743b312d2b74d3babe28f1
MD5 ec0a982a88fe5685e3a396c1279502f9
BLAKE2b-256 b42b84a6f86bc060b74e9266f0a004b2aaa02c7471e5d30929a6ff61be2ea59a

See more details on using hashes here.

File details

Details for the file nutpie-0.16.8-cp313-cp313-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.8-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 039c7484831e618d97f0db607ce5b4c8e6b6660da28bbf611814dc4077bfe83c
MD5 6e3b412e13ce2290539e68bb5b2e8f2a
BLAKE2b-256 df9f4742f3999bb776cf3b41fcf807b70e4b5cc70ca6c1b0330efad3c3c9f9b9

See more details on using hashes here.

File details

Details for the file nutpie-0.16.8-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: nutpie-0.16.8-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 9.1 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.12.6

File hashes

Hashes for nutpie-0.16.8-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 be00cbc27917506c612eabd5ced45a540d975b7b0d7d616ae402cfa48f334694
MD5 678692325ec052f65275134b8b3a35f5
BLAKE2b-256 25eedfb0234b4bf5732c293c89ec1c73deca115fc99dac341fbe2d8f36edb051

See more details on using hashes here.

File details

Details for the file nutpie-0.16.8-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.8-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 60c5ded6bb40822652cf2683820c6ab8c98f339ed65d7353611ac58094eba831
MD5 e8e143677e7710d8959d52cadbeca7f2
BLAKE2b-256 2055a9e2e2775d6f2cc3b335e1d16a0cf5a8d52dbf13d37ec5c6446d5f9960d9

See more details on using hashes here.

File details

Details for the file nutpie-0.16.8-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.8-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 6fe7e9274793808e8f52806a63311cdaf32681f31f73cbfe653640e42c6ab234
MD5 ae7f63e6bbf744c00409915c302b7e40
BLAKE2b-256 ac3c3ca8af6833bdd3311cd89e071ff37af0c2de110c82fa54cc9b05ee676e2d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.8-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d6c295a78ca7c0e9851ea7da10f60322db86694d20e2fa22da0469a11a078070
MD5 1011f5db59db18f65f2d3f347e5d7638
BLAKE2b-256 52949c2f00562b3ad870a5edd1d36d495332d7611188e6b8022d6fd9c0ff36a9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.8-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 17da4331f89bd87f51621329ba0f7665b2c7082b4dcd3e38911fc0c6650882f2
MD5 52a68776a044e80c0abd303fcffee86e
BLAKE2b-256 e8f58a45c1ab76f18b04cfc0ea660944e74dddfe0d64c0dfb9f4b8722686896d

See more details on using hashes here.

File details

Details for the file nutpie-0.16.8-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: nutpie-0.16.8-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 9.1 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.12.6

File hashes

Hashes for nutpie-0.16.8-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 974426fb321a570d0df50a94fd09e0f395bc726e3aa54732684f897cfe0b0d9c
MD5 c341ff6476dc535d028f6854d5412af1
BLAKE2b-256 8ac15310dacefeddee79856cb412a90d4302615f438793f8dcd21f1baa0bb6d5

See more details on using hashes here.

File details

Details for the file nutpie-0.16.8-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.8-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 47907f9c32f178c2a3f8489cc69cef448a0a6b529a0b4efb8440ad941f72fa32
MD5 0da75a2d1d593837d45b66a56a79c882
BLAKE2b-256 6c8e00bde65855b5e048af56e413148fffd9e43b39df86fbea15440c08c9f268

See more details on using hashes here.

File details

Details for the file nutpie-0.16.8-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.8-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 28bf67e8dd906a506dad6488eb18259fc3df173b5a1d5d6da5c75a148cd14618
MD5 d2bafe15998a91484d1876c2d4a92b11
BLAKE2b-256 8a7e8e0d634ac547693e5866c5337b6216ad132d2abe053bca39bec369ee378f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.8-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b9d91561b5c28e9328ef44a7d45888f94bd8f05ff802dd14ffc73cdd37ec5d2b
MD5 705525dd0c267080b20a78bef6d828e7
BLAKE2b-256 f0acbd8b1e534a66bdaf888b6f9b13d40de085f5ce9f5f4be945454609e4281a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.16.8-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 71bcc6d64879231fd8feb7da14dc9c080030e69654f080e9041a99ceddc2869b
MD5 2885e747f56a13903e1c204803e0daae
BLAKE2b-256 4f94930f7d2867fc783915e69d22cc4439673e03733963d27347f55bde5b4ec7

See more details on using hashes here.

File details

Details for the file nutpie-0.16.8-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.8-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 89e8cb33319d442d5eed6d5ae7d27036fd209bdae45582cd17dc38eb7190a28e
MD5 02fa62123cc50fbab80cb0f836587ee5
BLAKE2b-256 427d1f1650ed44764e80c76491511b395ea21cf2b0143c9c072294c9dc6f561a

See more details on using hashes here.

File details

Details for the file nutpie-0.16.8-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for nutpie-0.16.8-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 65c49687b34c0d81266c7a0e258aae614eb34cbf8a5ec7a61d5208dceea772d2
MD5 451ee40830badde8b8d50b56a6e40756
BLAKE2b-256 ec1847eb110b518db1359eb60bf1da9a1809dccca00b1bfcd139007c6b64f391

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page