# nutpie: A fast sampler for Bayesian posteriors
## Installation
nutpie can be installed using Conda or Mamba from conda-forge with
```bash
mamba install -c conda-forge nutpie
```
Or using pip:
```bash
pip install nutpie
```
To install it from source, install a Rust compiler and maturin and then
```bash
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:
```bash
rustup override set nightly
maturin develop --release --features=simd_support
```
## Usage with PyMC
First, PyMC and Numba need to be installed, for example using
```bash
mamba install -c conda-forge pymc numba
```
We need to create a model:
```python
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:
```python
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:
```python
# 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.
```bash
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](https://mc-stan.org/docs/cmdstan-guide/cmdstan-installation.html#cpp-toolchain).
We can then compile a Stan model, and sample using nutpie:
```python
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`](https://github.com/pymc-devs/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.
Raw data
{
"_id": null,
"home_page": null,
"name": "nutpie",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "statistics, bayes",
"author": "Adrian Seyboldt <adrian.seyboldt@gmail.com>, PyMC Developers <pymc.devs@gmail.com>",
"author_email": "PyMC Developers <pymc.devs@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/b1/e0/3fa6b85e944798f1583f77f7e321c22d78cfdb60b509c50d959dfd626bc5/nutpie-0.10.0.tar.gz",
"platform": null,
"description": "# nutpie: A fast sampler for Bayesian posteriors\n\n## Installation\n\nnutpie can be installed using Conda or Mamba from conda-forge with\n\n```bash\nmamba install -c conda-forge nutpie\n```\n\nOr using pip:\n\n```bash\npip install nutpie\n```\n\nTo install it from source, install a Rust compiler and maturin and then\n\n```bash\nmaturin develop --release\n```\n\nIf you want to use the nightly SIMD implementation for some of the math functions,\nswitch to Rust nightly and then install with the `simd_support` feature in then\nnutpie directory:\n\n```bash\nrustup override set nightly\nmaturin develop --release --features=simd_support\n```\n\n## Usage with PyMC\n\nFirst, PyMC and Numba need to be installed, for example using\n\n```bash\nmamba install -c conda-forge pymc numba\n```\n\nWe need to create a model:\n\n```python\nimport pymc as pm\nimport numpy as np\nimport nutpie\nimport pandas as pd\nimport seaborn as sns\n\n# Load the radon dataset\ndata = pd.read_csv(pm.get_data(\"radon.csv\"))\ndata[\"log_radon\"] = data[\"log_radon\"].astype(np.float64)\ncounty_idx, counties = pd.factorize(data.county)\ncoords = {\"county\": counties, \"obs_id\": np.arange(len(county_idx))}\n\n# Create a simple hierarchical model for the radon dataset\nwith pm.Model(coords=coords, check_bounds=False) as pymc_model:\n intercept = pm.Normal(\"intercept\", sigma=10)\n\n # County effects\n raw = pm.ZeroSumNormal(\"county_raw\", dims=\"county\")\n sd = pm.HalfNormal(\"county_sd\")\n county_effect = pm.Deterministic(\"county_effect\", raw * sd, dims=\"county\")\n\n # Global floor effect\n floor_effect = pm.Normal(\"floor_effect\", sigma=2)\n\n # County:floor interaction\n raw = pm.ZeroSumNormal(\"county_floor_raw\", dims=\"county\")\n sd = pm.HalfNormal(\"county_floor_sd\")\n county_floor_effect = pm.Deterministic(\n \"county_floor_effect\", raw * sd, dims=\"county\"\n )\n\n mu = (\n intercept\n + county_effect[county_idx]\n + floor_effect * data.floor.values\n + county_floor_effect[county_idx] * data.floor.values\n )\n\n sigma = pm.HalfNormal(\"sigma\", sigma=1.5)\n pm.Normal(\n \"log_radon\", mu=mu, sigma=sigma, observed=data.log_radon.values, dims=\"obs_id\"\n )\n```\n\nWe then compile this model and sample form the posterior:\n\n```python\ncompiled_model = nutpie.compile_pymc_model(pymc_model)\ntrace_pymc = nutpie.sample(compiled_model)\n```\n\n`trace_pymc` now contains an ArviZ `InferenceData` object, including sampling\nstatistics and the posterior of the variables defined above.\n\nWe can also control the sampler in a non-blocking way:\n\n```python\n# The sampler will now run the the background\nsampler = nutpie.sample(compiled_model, blocking=False)\n\n# Pause and resume the sampling\nsampler.pause()\nsampler.resume()\n\n# Wait for the sampler to finish (up to timeout seconds)\n# sampler.wait(timeout=0.1)\n\n# or we can also abort the sampler (and return the incomplete trace)\nincomplete_trace = sampler.abort()\n\n# or cancel and discard all progress:\nsampler.cancel()\n```\n\n## Usage with Stan\n\nIn order to sample from Stan model, `bridgestan` needs to be installed.\nA pip package is available, but right now this can not be installed using Conda.\n\n```bash\npip install bridgestan\n```\n\nWhen we install nutpie with pip, we can also specify that we want optional\ndependencies for Stan models using\n\n```\npip install 'nutpie[stan]'\n```\n\nIn addition, a C++ compiler needs to be available. For details see\n[the Stan docs](https://mc-stan.org/docs/cmdstan-guide/cmdstan-installation.html#cpp-toolchain).\n\nWe can then compile a Stan model, and sample using nutpie:\n\n```python\nimport nutpie\n\ncode = \"\"\"\ndata {\n real mu;\n}\nparameters {\n real x;\n}\nmodel {\n x ~ normal(mu, 1);\n}\n\"\"\"\n\ncompiled = nutpie.compile_stan_model(code=code)\n# Provide data\ncompiled = compiled.with_data(mu=3.)\ntrace = nutpie.sample(compiled)\n```\n\n## Advantages\n\nnutpie uses [`nuts-rs`](https://github.com/pymc-devs/nuts-rs), a library written in Rust, that implements NUTS as in\nPyMC and Stan, but with a slightly different mass matrix tuning method as\nthose. It often produces a higher effective sample size per gradient\nevaluation, and tends to converge faster and with fewer gradient evaluation.\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Sample Stan or PyMC models",
"version": "0.10.0",
"project_urls": {
"Source Code": "https://github.com/pymc-devs/nutpie"
},
"split_keywords": [
"statistics",
" bayes"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "6ce8e1561a63d6cea8fa8bf6ce560ed2ec2d8fbab415df3deeb19965f30f178b",
"md5": "3932dab74667d3731fbc075ec98c6308",
"sha256": "3a0c24cca6c3357bb4c9b9be72d489b52f0dfc898db65d747c8634d9112ce905"
},
"downloads": -1,
"filename": "nutpie-0.10.0-cp310-cp310-macosx_10_12_x86_64.whl",
"has_sig": false,
"md5_digest": "3932dab74667d3731fbc075ec98c6308",
"packagetype": "bdist_wheel",
"python_version": "cp310",
"requires_python": ">=3.9",
"size": 682049,
"upload_time": "2024-03-20T22:19:28",
"upload_time_iso_8601": "2024-03-20T22:19:28.236554Z",
"url": "https://files.pythonhosted.org/packages/6c/e8/e1561a63d6cea8fa8bf6ce560ed2ec2d8fbab415df3deeb19965f30f178b/nutpie-0.10.0-cp310-cp310-macosx_10_12_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "37cd32a7a0ff289062b5424e9200fcf4d1f1b85c633de8d2196d295547de2f2b",
"md5": "01b4cc094d1b64e4ab635c3866dfb7e2",
"sha256": "7d13d936ada677a5754a7312db5342894fd09202b5d2ec956ebdd9c50b325596"
},
"downloads": -1,
"filename": "nutpie-0.10.0-cp310-cp310-macosx_11_0_arm64.whl",
"has_sig": false,
"md5_digest": "01b4cc094d1b64e4ab635c3866dfb7e2",
"packagetype": "bdist_wheel",
"python_version": "cp310",
"requires_python": ">=3.9",
"size": 667069,
"upload_time": "2024-03-20T22:19:31",
"upload_time_iso_8601": "2024-03-20T22:19:31.132939Z",
"url": "https://files.pythonhosted.org/packages/37/cd/32a7a0ff289062b5424e9200fcf4d1f1b85c633de8d2196d295547de2f2b/nutpie-0.10.0-cp310-cp310-macosx_11_0_arm64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "670b2f1389603dbe41bb9b51a1eb1ec3db52260814d8e3635f8cdb21afe793d8",
"md5": "010447e88fb89312371e70c6344fd465",
"sha256": "54331292ccf2bb0b7fe6cbe465fa81d2d853aed823e96c60333cd449275bb5f3"
},
"downloads": -1,
"filename": "nutpie-0.10.0-cp310-cp310-manylinux_2_28_x86_64.whl",
"has_sig": false,
"md5_digest": "010447e88fb89312371e70c6344fd465",
"packagetype": "bdist_wheel",
"python_version": "cp310",
"requires_python": ">=3.9",
"size": 3484311,
"upload_time": "2024-03-20T22:19:34",
"upload_time_iso_8601": "2024-03-20T22:19:34.468481Z",
"url": "https://files.pythonhosted.org/packages/67/0b/2f1389603dbe41bb9b51a1eb1ec3db52260814d8e3635f8cdb21afe793d8/nutpie-0.10.0-cp310-cp310-manylinux_2_28_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0d276d56816f2bc410082a35ac377f9fa6a69a3e57c1f195b6b633c96f69057e",
"md5": "4339e166d0871695557088e4211a8511",
"sha256": "c6836b0a85ca552263049c8eb7039b5ab77221a43e00ce234322617aac02a5f7"
},
"downloads": -1,
"filename": "nutpie-0.10.0-cp311-cp311-macosx_10_12_x86_64.whl",
"has_sig": false,
"md5_digest": "4339e166d0871695557088e4211a8511",
"packagetype": "bdist_wheel",
"python_version": "cp311",
"requires_python": ">=3.9",
"size": 682139,
"upload_time": "2024-03-20T22:19:37",
"upload_time_iso_8601": "2024-03-20T22:19:37.517424Z",
"url": "https://files.pythonhosted.org/packages/0d/27/6d56816f2bc410082a35ac377f9fa6a69a3e57c1f195b6b633c96f69057e/nutpie-0.10.0-cp311-cp311-macosx_10_12_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "902fc0bfea7184fadd3bae4538e20aeca1797e4613ebb10443d0689821ee0771",
"md5": "7495c29bd5468f7399bbabc3c18714c0",
"sha256": "f62c1f37d45f1bf9a2862ed182f7c9b03e2dc3f08bf88705fa33ff2e63d12728"
},
"downloads": -1,
"filename": "nutpie-0.10.0-cp311-cp311-macosx_11_0_arm64.whl",
"has_sig": false,
"md5_digest": "7495c29bd5468f7399bbabc3c18714c0",
"packagetype": "bdist_wheel",
"python_version": "cp311",
"requires_python": ">=3.9",
"size": 667070,
"upload_time": "2024-03-20T22:19:39",
"upload_time_iso_8601": "2024-03-20T22:19:39.339614Z",
"url": "https://files.pythonhosted.org/packages/90/2f/c0bfea7184fadd3bae4538e20aeca1797e4613ebb10443d0689821ee0771/nutpie-0.10.0-cp311-cp311-macosx_11_0_arm64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "91b81460ac65b4e04a3d502ecf278da4a929d6b0c4107e35b65f62deb65976b6",
"md5": "0ab7bc5d1aee7f2bd818c96afb1e8fd2",
"sha256": "987808e9aa50459383a8a36d4d1ef5f11cd510a55d1068db9b7f519b6b063704"
},
"downloads": -1,
"filename": "nutpie-0.10.0-cp311-cp311-manylinux_2_28_x86_64.whl",
"has_sig": false,
"md5_digest": "0ab7bc5d1aee7f2bd818c96afb1e8fd2",
"packagetype": "bdist_wheel",
"python_version": "cp311",
"requires_python": ">=3.9",
"size": 3484359,
"upload_time": "2024-03-20T22:19:43",
"upload_time_iso_8601": "2024-03-20T22:19:43.043557Z",
"url": "https://files.pythonhosted.org/packages/91/b8/1460ac65b4e04a3d502ecf278da4a929d6b0c4107e35b65f62deb65976b6/nutpie-0.10.0-cp311-cp311-manylinux_2_28_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "1bc52acebb71ced80371438d81f2d7d50dbd977f7de911935ff8535b51eb9d4c",
"md5": "91232c80940f5c2d7aaa6ab94ba24703",
"sha256": "005c8e31a23fd95fc7e3aa4ee6bf9f6bee15ca63f0f5bfc99fcfaf7b0d3dc65d"
},
"downloads": -1,
"filename": "nutpie-0.10.0-cp312-cp312-macosx_10_12_x86_64.whl",
"has_sig": false,
"md5_digest": "91232c80940f5c2d7aaa6ab94ba24703",
"packagetype": "bdist_wheel",
"python_version": "cp312",
"requires_python": ">=3.9",
"size": 681261,
"upload_time": "2024-03-20T22:19:45",
"upload_time_iso_8601": "2024-03-20T22:19:45.954032Z",
"url": "https://files.pythonhosted.org/packages/1b/c5/2acebb71ced80371438d81f2d7d50dbd977f7de911935ff8535b51eb9d4c/nutpie-0.10.0-cp312-cp312-macosx_10_12_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "b183808b35bd04679993111b3f4dbff93ecc1bada83fc3bf6a299b3907fe8498",
"md5": "6836e88d269f83a9a744d2ff5c23824e",
"sha256": "009b9b192d3682b8f010cfdfecad272447b72a129fa70c01b15dee2f40a096cb"
},
"downloads": -1,
"filename": "nutpie-0.10.0-cp312-cp312-macosx_11_0_arm64.whl",
"has_sig": false,
"md5_digest": "6836e88d269f83a9a744d2ff5c23824e",
"packagetype": "bdist_wheel",
"python_version": "cp312",
"requires_python": ">=3.9",
"size": 667131,
"upload_time": "2024-03-20T22:19:47",
"upload_time_iso_8601": "2024-03-20T22:19:47.859790Z",
"url": "https://files.pythonhosted.org/packages/b1/83/808b35bd04679993111b3f4dbff93ecc1bada83fc3bf6a299b3907fe8498/nutpie-0.10.0-cp312-cp312-macosx_11_0_arm64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "713f6384c39450fa5ab6af0d5d3d8b14cf1cfebd16ae7a274ed6b2f9696c1240",
"md5": "d29b9d6173a75090cba45da9a642d6a2",
"sha256": "c64c54da5fb5a89c8537e63ca99c98b4b7bc023565d8888ff4fd4dcc8f1d3f9d"
},
"downloads": -1,
"filename": "nutpie-0.10.0-cp312-cp312-manylinux_2_28_x86_64.whl",
"has_sig": false,
"md5_digest": "d29b9d6173a75090cba45da9a642d6a2",
"packagetype": "bdist_wheel",
"python_version": "cp312",
"requires_python": ">=3.9",
"size": 3492966,
"upload_time": "2024-03-20T22:19:51",
"upload_time_iso_8601": "2024-03-20T22:19:51.843132Z",
"url": "https://files.pythonhosted.org/packages/71/3f/6384c39450fa5ab6af0d5d3d8b14cf1cfebd16ae7a274ed6b2f9696c1240/nutpie-0.10.0-cp312-cp312-manylinux_2_28_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "4c20bd9dd2a6f3690ab8bd2da76699ff68ff7b0f5a337e24d8421479c8bcd15f",
"md5": "d9fb96e9f92374349cc06f2210da5229",
"sha256": "82d63f7c5afc0296bf047525d500e8380146c5d86a7226e8d6abeb1d116a554d"
},
"downloads": -1,
"filename": "nutpie-0.10.0-cp39-cp39-manylinux_2_28_x86_64.whl",
"has_sig": false,
"md5_digest": "d9fb96e9f92374349cc06f2210da5229",
"packagetype": "bdist_wheel",
"python_version": "cp39",
"requires_python": ">=3.9",
"size": 3485517,
"upload_time": "2024-03-20T22:19:25",
"upload_time_iso_8601": "2024-03-20T22:19:25.092216Z",
"url": "https://files.pythonhosted.org/packages/4c/20/bd9dd2a6f3690ab8bd2da76699ff68ff7b0f5a337e24d8421479c8bcd15f/nutpie-0.10.0-cp39-cp39-manylinux_2_28_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "246e327772aab7701c84f965e55029fd52b5005918b53e297ae718ed3f4b1d03",
"md5": "c4ec3fc6ff902d9e1f07f5fc1913ea74",
"sha256": "9044d161b4501d9ce86fd1cb968cd874cf1ee453cb71d618207b87907633810e"
},
"downloads": -1,
"filename": "nutpie-0.10.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl",
"has_sig": false,
"md5_digest": "c4ec3fc6ff902d9e1f07f5fc1913ea74",
"packagetype": "bdist_wheel",
"python_version": "pp310",
"requires_python": ">=3.9",
"size": 3477823,
"upload_time": "2024-03-20T22:19:58",
"upload_time_iso_8601": "2024-03-20T22:19:58.486831Z",
"url": "https://files.pythonhosted.org/packages/24/6e/327772aab7701c84f965e55029fd52b5005918b53e297ae718ed3f4b1d03/nutpie-0.10.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6750ce37268504fd26514e6c2639619c6362256daa3969b8a9ddbe2086484cc2",
"md5": "3c5b858908ac7b71adeb3cdef3d02ff2",
"sha256": "7e4b3666735a37d44d5c0f445c8776b811214a9f33d94e756a02990a8d2eb5b3"
},
"downloads": -1,
"filename": "nutpie-0.10.0-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl",
"has_sig": false,
"md5_digest": "3c5b858908ac7b71adeb3cdef3d02ff2",
"packagetype": "bdist_wheel",
"python_version": "pp39",
"requires_python": ">=3.9",
"size": 3477949,
"upload_time": "2024-03-20T22:19:54",
"upload_time_iso_8601": "2024-03-20T22:19:54.678773Z",
"url": "https://files.pythonhosted.org/packages/67/50/ce37268504fd26514e6c2639619c6362256daa3969b8a9ddbe2086484cc2/nutpie-0.10.0-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "b1e03fa6b85e944798f1583f77f7e321c22d78cfdb60b509c50d959dfd626bc5",
"md5": "297eba3a1ae5d80afbbcb10c8f10779a",
"sha256": "d75b6bafa5a9d9534e456d6a09d61b41ad20a492c9f47528516d08ce89420770"
},
"downloads": -1,
"filename": "nutpie-0.10.0.tar.gz",
"has_sig": false,
"md5_digest": "297eba3a1ae5d80afbbcb10c8f10779a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 171196,
"upload_time": "2024-03-20T22:20:00",
"upload_time_iso_8601": "2024-03-20T22:20:00.747384Z",
"url": "https://files.pythonhosted.org/packages/b1/e0/3fa6b85e944798f1583f77f7e321c22d78cfdb60b509c50d959dfd626bc5/nutpie-0.10.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-03-20 22:20:00",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "pymc-devs",
"github_project": "nutpie",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "nutpie"
}