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

Uploaded Source

Built Distributions

nutpie-0.9.1-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl (3.7 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

nutpie-0.9.1-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl (3.7 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

nutpie-0.9.1-cp312-cp312-manylinux_2_28_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.28+ x86-64

nutpie-0.9.1-cp311-none-win_amd64.whl (504.6 kB view details)

Uploaded CPython 3.11 Windows x86-64

nutpie-0.9.1-cp311-cp311-manylinux_2_28_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

nutpie-0.9.1-cp311-cp311-macosx_11_0_arm64.whl (676.3 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

nutpie-0.9.1-cp311-cp311-macosx_10_7_x86_64.whl (706.0 kB view details)

Uploaded CPython 3.11 macOS 10.7+ x86-64

nutpie-0.9.1-cp310-none-win_amd64.whl (504.6 kB view details)

Uploaded CPython 3.10 Windows x86-64

nutpie-0.9.1-cp310-cp310-manylinux_2_28_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

nutpie-0.9.1-cp310-cp310-macosx_11_0_arm64.whl (676.3 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

nutpie-0.9.1-cp310-cp310-macosx_10_7_x86_64.whl (706.0 kB view details)

Uploaded CPython 3.10 macOS 10.7+ x86-64

nutpie-0.9.1-cp39-none-win_amd64.whl (504.9 kB view details)

Uploaded CPython 3.9 Windows x86-64

nutpie-0.9.1-cp39-cp39-manylinux_2_28_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.28+ x86-64

File details

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

File metadata

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

File hashes

Hashes for nutpie-0.9.1.tar.gz
Algorithm Hash digest
SHA256 dc290fc67fe921b8581ac5b990e5da840ee1ccb0a05d0f022b9e8ddbc4e4387a
MD5 a5bdd125a79721bc96a7663a16bf9c61
BLAKE2b-256 ee9c15fef2da392cc2834ab0ff377d9c0dff31a3e3a6a46c140e123f4bfafc9f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.1-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d6b73a4a9312042183379396cd56feaf593ff0c381487028205ecf6bfa47634f
MD5 11c5b5f68580901492adb001ce2511c7
BLAKE2b-256 6488df4beadf9ca48dc637f0e99db9b59706afe7aaf64d3ebe25d81b4bb4a5d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.1-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ea65be79e8f85b9842e8c631d4b14a53e02aaee822b84581e1acb7bee4f274d6
MD5 874c7b8ea86857b0267ccc031dc4111b
BLAKE2b-256 60f68076b77426b89350c4943b1bfe0168a861c176eea320814b71b5ef80e0b8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.1-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ba63fd73c9236c867430907b5d9791a09e60802ac392d1089467c38ec01a927d
MD5 b10d81bcc12d526d59f2b4ebe07ab927
BLAKE2b-256 b2154c49a6919673e924a2ebfcd76eedb7e8a5f09a445414c8e6dda85484295b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nutpie-0.9.1-cp311-none-win_amd64.whl
  • Upload date:
  • Size: 504.6 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.2.3

File hashes

Hashes for nutpie-0.9.1-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 fc1ba443277a7d687a80dd31f0cbc3560a67d377c0e18ba66dbbea4c1087912e
MD5 2635ac8ee4320f18a919e1dcdf18c2b7
BLAKE2b-256 dc76f38d48376c9e947c55408eb9ba57c8cb31cd05bd28289ffc792dd4a3777d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.1-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f0cdd428d4adef5fd8e3c8e2560187b67ca2b94beb8afdbd7a81214ed5575619
MD5 b5307b1bae81c7ef27180749ae9c6b6a
BLAKE2b-256 4985c2941527c7671d112b8ae42dcd7a9a0f4c0725b3efb3e36a04959913ce4f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 824cf258901597cf337be080b8d2bdce25779c25790354f688c31b7e7c83adfe
MD5 1bfdf46f20bee44603461bfadda8ab9d
BLAKE2b-256 0fbacf3e7aa02ce6f24f7a4cca2c788e8d789e38384e2e975988cb0cfdbebc0d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.1-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 5881f722e8fceba7f52142b20d1a241375f3f2f35e2bcb1200862db63fc92216
MD5 abba642abf4cc5a340a43be53aa29a49
BLAKE2b-256 7c548d858e6152417c65fb248b22d5492c35188bcdd05b8cc06fe68822cbfadd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: nutpie-0.9.1-cp310-none-win_amd64.whl
  • Upload date:
  • Size: 504.6 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.2.3

File hashes

Hashes for nutpie-0.9.1-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 00f29c073ebb18ceff57c5045ae62cd6fc0e114825fdc228d12df56bfdad7275
MD5 695f03489ce34568f9a23926215a5591
BLAKE2b-256 b70bef662cbbeb831b83effead516d8ffe0838b0617b2846c70479369ed01aff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.1-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 070e08258feae40c08a6e1d382ed00e924b773e14c7f322360cfc4019875047b
MD5 d4623c7beadbc4e9489b7b0347379762
BLAKE2b-256 caab9adac6baa788eea70bbe14e0e31d5d406226e6bea91c60f61eebb6eb1bc9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6243f193b7aa303e17f2e92bda1bfbb26d80520ee2eefb91d346b3ef373cd994
MD5 90e83d5e0c6c2632a378cb436564817d
BLAKE2b-256 ff209ab2db66511667c282f307f1a03d36ba4c77d0f833a8106ba4f926c4fcfc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.1-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 0b2f7b40077376af37d3ebb7fe991e6e275f68be6f8e5433cbebcb828a8d955e
MD5 ea597a6086a41c800a7602e3c74647f9
BLAKE2b-256 e7ab3e3055813078c1487f6873a5b13fbfc6cae336c12a0459bf33b08f67a85f

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for nutpie-0.9.1-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 71e362aebe87bdaf81f9b4588f42259504154e20d445a7e65f28043f3add0987
MD5 a2315c047d09c2cfecc88a23b6424817
BLAKE2b-256 c80f930cd9519c3ee0ef8b8bb05f328cc16df38e1155d8ef981c8f682051c162

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.9.1-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0b6eb80cc606d294be8b919ca562d5b1cec66506703e9ff833eb7c583cf65788
MD5 1979d7cdb520c676078f4b65f992499b
BLAKE2b-256 c2bc6a8f77462b99e9c628b8873f83c9c03405465c780332eb704edaeaf067bc

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