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

Uploaded Source

Built Distributions

nutpie-0.7.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl (3.6 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

nutpie-0.7.0-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl (3.6 MB view details)

Uploaded PyPy manylinux: glibc 2.28+ x86-64

nutpie-0.7.0-cp312-cp312-manylinux_2_28_x86_64.whl (3.6 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.28+ x86-64

nutpie-0.7.0-cp311-none-win_amd64.whl (489.1 kB view details)

Uploaded CPython 3.11 Windows x86-64

nutpie-0.7.0-cp311-cp311-manylinux_2_28_x86_64.whl (3.6 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

nutpie-0.7.0-cp311-cp311-macosx_11_0_arm64.whl (658.0 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

nutpie-0.7.0-cp311-cp311-macosx_10_7_x86_64.whl (683.0 kB view details)

Uploaded CPython 3.11 macOS 10.7+ x86-64

nutpie-0.7.0-cp310-none-win_amd64.whl (489.0 kB view details)

Uploaded CPython 3.10 Windows x86-64

nutpie-0.7.0-cp310-cp310-manylinux_2_28_x86_64.whl (3.6 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

nutpie-0.7.0-cp310-cp310-macosx_11_0_arm64.whl (658.0 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

nutpie-0.7.0-cp310-cp310-macosx_10_7_x86_64.whl (683.0 kB view details)

Uploaded CPython 3.10 macOS 10.7+ x86-64

nutpie-0.7.0-cp39-none-win_amd64.whl (489.2 kB view details)

Uploaded CPython 3.9 Windows x86-64

nutpie-0.7.0-cp39-cp39-manylinux_2_28_x86_64.whl (3.6 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.28+ x86-64

File details

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

File metadata

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

File hashes

Hashes for nutpie-0.7.0.tar.gz
Algorithm Hash digest
SHA256 e162fba724317a5541cec058e67c3a5b7f7bdb3b990273afef789fae451c171d
MD5 b347e04eb3f129e9a88db5a33288fb34
BLAKE2b-256 74b87f6c658a1816cd66e51f892f00807d406708da190c916c4922f3982d0de9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.7.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 903eb3912cb5ea20fb16fec819b66744f989ae50ba39a1843b8fef5b58f1d2ee
MD5 d4bdcbba203b0e580a65d3eeb062ff32
BLAKE2b-256 1cc1d5cd1397352ccf5af9010dd1408469852071beb1d4a5081e6936823c27b7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.7.0-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a2298ff6a5cfd51cd881c85f39f34af232faebde3813bee9cb5e0b9ed9fb3518
MD5 5a0ed846176da3953991301c6dd66804
BLAKE2b-256 a6a72c4d3310d8cc9d43228cc792f06820b063d0226a82150898ab529e6e5fd0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.7.0-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e0c6d7f3a65ba774ef3c2f49217305c27f2f500e49fb3f5b318c831edd5e795c
MD5 54770b36bc3cb010d68e41223b871618
BLAKE2b-256 8ef02b23e4722905505346e0d67afa3f41318e546c13be94abcf8ebecef74b25

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for nutpie-0.7.0-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 8d18b5f84c5b21bdb6cd77e0993351bcd6e39d2985e478a9cc161509a001f26e
MD5 1fd990dcda73c0489eec35853ae35b5b
BLAKE2b-256 fa40628f727cdf3d06fa05070ed58c48608433304a8447adfafa117faa534dac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.7.0-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 48da703a7beccaa57d896a0ce8b7e5d3bd1aa8866bc013ea2def9c0c5c2a4ea6
MD5 9509fbc68975334594d6dd3f88de7364
BLAKE2b-256 dd0fa0a909445268fa9358fe89d3993e373872e5abdef33fbebccb6ad1fecc03

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.7.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 574f9aacad149f5e43be6675a82a4ce91d762f376a8529f1d58e4f6afedf4057
MD5 5a0020313f57a67227869fc79a22e9ef
BLAKE2b-256 2478de0775362c502cce45a3df86de5830512ea0fe73695434683db922587a14

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.7.0-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 39b70873455c829ff628fa2903198f2551ffc17c9cdb4393cf4190ab3096293f
MD5 3e21dd21d0bbcad55be675fb03268ecc
BLAKE2b-256 2525db8c441e16ebfa4d16253eb48e31106cff2499361cb7fee42829218a1906

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for nutpie-0.7.0-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 1e0ea52c90d5b1d7833ff81aa46cbee5595060d8f44034488f541beddc7a1b02
MD5 82bfb32c6558537056555b9a3fd9f855
BLAKE2b-256 36c98195d5cb62bf1681dc1f4ade9f51ccaa638dc39a3d5c8df5ab4f3489c0b8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.7.0-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 974087877f89be070de8462716f22f6ff4b96c12cb014277af7e44f49335fd89
MD5 3abb3ba763f9c3bce748b07302d50833
BLAKE2b-256 f5077407293235096edac746aadb440f669028b303c19c2b02675767e801eac0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.7.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6f590197b5d3c3721a978d3c04050266ffd3ee0743dff5467222ca4980bc9ae9
MD5 daaf12c65940604e9408599476a7b3fb
BLAKE2b-256 7dec42a0da787bef824f699501160e553359ae733a944b617795178d4bbc080c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.7.0-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 a9e9032fd65871fdcc00b6d551dc03fa5e42ef68d7fe09f22137772d2f861690
MD5 8876dc9c359fd20c1975d1580b54c59d
BLAKE2b-256 9128617da1f45b5c63cc7f8fb7e6c02dcf43c73726a8356bec4acdb8ebab16cd

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for nutpie-0.7.0-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 d3ed4be6ba951a54df02a9c623098580b299be89b80c6694857f7175f846b2db
MD5 22678652022e2315a7472bb6e962961c
BLAKE2b-256 bda1b514776ba0d902b548a7e486e7090debbc35fa382cf2adfdd5294c9e4a91

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nutpie-0.7.0-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f41e84a940ce33b0994cae8bfc92040cf8392c350863d4a1fad6e15b5c5df86f
MD5 aca7aa3562bdcfc7370cd5a5f54e4c81
BLAKE2b-256 b96059aefc059c5a20d5b2d81b776e39d68f47dff437a5ad619e065e5152e0e5

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