Skip to main content

Python interface to JAGS for Bayesian analysis via MCMC

Project description

PyJAGS: The Python Interface to JAGS

PyJAGS provides a Python interface to JAGS, a program for analysis of Bayesian hierarchical models using Markov Chain Monte Carlo (MCMC) simulation.

PyJAGS adds the following features on top of JAGS:

  • Multicore support for parallel simulation of multiple Markov chains
  • Built-in ArviZ integration via pyjags.from_pyjags() for diagnostics and visualization
  • Incremental sampling with automatic convergence detection (ESS and R-hat criteria)
  • Saving and restoring MCMC sample chains to/from HDF5 files
  • Merging samples along iterations or across chains for resumed sampling

License: GPLv2

Compatibility

Component Supported Versions
Python 3.12+
NumPy 1.x and 2.x
ArviZ 1.0+
macOS Intel and Apple Silicon (M1/M2/M3/M4)
Linux Debian/Ubuntu (tested), other distributions (untested)

Note: Python 3.10 and 3.11 were supported in earlier releases but are no longer supported because ArviZ 1.0 — a core dependency — requires Python 3.12+.

Installation

Prerequisites

A working JAGS installation, a C++ compiler, and CMake are required. PyJAGS uses CMake with pkg-config to locate the JAGS headers and libraries at build time.

macOS

1. Install JAGS via Homebrew

Apple Silicon (M1/M2/M3/M4):

Homebrew installs to /opt/homebrew on Apple Silicon Macs:

# Install Homebrew if needed (https://brew.sh)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Follow Homebrew's instructions to add it to your PATH, then:
brew install jags

Make sure pkg-config can find the JAGS installation by adding this to your shell profile (~/.zprofile or ~/.zshrc):

export PKG_CONFIG_PATH="/opt/homebrew/lib/pkgconfig:$PKG_CONFIG_PATH"

Intel Mac:

Homebrew installs to /usr/local on Intel Macs and pkg-config typically finds JAGS automatically:

brew install jags

2. Set up Python and install PyJAGS

We recommend using uv to manage Python installations and virtual environments:

# Install uv if needed
curl -LsSf https://astral.sh/uv/install.sh | sh

# Install Python 3.12 and create a virtual environment
uv python install 3.12
uv venv --python 3.12 .venv
source .venv/bin/activate

# Install PyJAGS
uv pip install pyjags

To install from source (for development):

source .venv/bin/activate

git clone https://github.com/michaelnowotny/pyjags.git
cd pyjags

uv pip install -e .

Linux

Install JAGS using your distribution's package manager:

# Debian/Ubuntu
sudo apt-get install jags pkg-config

# Fedora/RHEL (untested — package names may differ)
sudo dnf install jags jags-devel pkgconf

Then install PyJAGS:

pip install pyjags

Or from source:

git clone https://github.com/michaelnowotny/pyjags.git
cd pyjags

pip install -e .

Windows

PyJAGS is not natively supported on Windows. Windows users should use WSL2 (Windows Subsystem for Linux) with an Ubuntu distribution, then follow the Linux installation instructions above.

Notebooks

The notebooks/ directory contains Jupyter notebooks demonstrating PyJAGS features:

Notebook Description
Trading Cost Estimation Bayesian estimation of bid-ask spreads using Hasbrouck's model (with and without a market factor)
Logistic Regression Bayesian logistic regression with MCMC diagnostics
Eight Schools Classic hierarchical model with prior/posterior analysis, warmup splitting, and LOO
Advanced Functionality Parallel chains, HDF5 persistence, chain merging, and incremental sampling until convergence

If you have a native installation (macOS or Linux with JAGS and .venv set up as described above), you can run Jupyter Lab directly without Docker:

./scripts/jagslab lab

On first run, this installs notebook dependencies (JupyterLab, seaborn, scikit-learn) into your .venv and registers a "Python 3.12 (pyjags)" Jupyter kernel that points to the correct Python environment. The notebooks are pre-configured to use this kernel.

ArviZ Integration

PyJAGS includes a built-in converter for ArviZ 1.0+. Use pyjags.from_pyjags() to convert sample dictionaries returned by Model.sample() into ArviZ DataTree objects for diagnostics and visualization:

import pyjags
import arviz as az

model = pyjags.Model(code=model_code, data=data, chains=4)
model.sample(1000, vars=[])                     # burn-in
samples = model.sample(5000, vars=['mu', 'sigma'])

idata = pyjags.from_pyjags(samples)             # -> xarray.DataTree
az.summary(idata)
az.plot_trace(idata)

The converter also supports prior samples, log-likelihood extraction, and warmup splitting. See the Eight Schools notebook for a complete example.

Development Environment (jagslab)

PyJAGS also includes a Docker-based development environment managed by the jagslab CLI script. This provides a reproducible setup with JAGS, Python 3.12, and Jupyter Lab without requiring a local JAGS installation.

Quick Start (Docker)

# 1. Copy the environment configuration
cp .env.example .env          # Edit .env to customize (e.g., Jupyter port)

# 2. Build the Docker image
./scripts/jagslab build

# 3. Run the test suite
./scripts/jagslab test

# 4. Start Jupyter Lab
./scripts/jagslab start       # Opens at http://localhost:8888

jagslab Commands

Command Description
start Start Jupyter Lab via Docker (auto-starts container and installs pyjags)
lab Start Jupyter Lab natively from .venv (no Docker required)
stop Stop the Docker container
test [args] Run the test suite
install Install/reinstall pyjags (recompiles C++ extension)
shell Open a bash shell in the container
python [args] Start Python REPL or run a script in the container
build Build the Docker image
rebuild Rebuild image from scratch (no Docker cache)
status Show container status
logs Tail container logs
clean Remove build artifacts
version Show pyjags, JAGS, Python, and numpy versions

Running Tests

./scripts/jagslab test                                           # All tests
./scripts/jagslab test test.test_model                           # One module
./scripts/jagslab test test.test_model.TestModel.test_samples_shape  # Single test

Working with the C++ Extension

PyJAGS includes a C++ extension (src/pyjags/console.cc) that wraps the JAGS library using pybind11. When editing Python files under src/pyjags/, changes take effect immediately thanks to the editable install. However, after editing src/pyjags/console.cc, you must recompile:

./scripts/jagslab install

Configuration

The .env file (created from .env.example) controls environment settings:

  • JAGSLAB_PORT — Host port for Jupyter Lab (default: 8888)

If .env does not exist when you run a jagslab command, it is automatically created from .env.example.

Troubleshooting

macOS: symbol not found '_JAGS_NA' or missing JAGS symbols at runtime

This usually means the compiled extension was not linked against libjags. Verify that pkg-config finds your JAGS installation:

pkg-config --libs --cflags jags

If this fails or points to the wrong location, set PKG_CONFIG_PATH as described in the installation instructions above, then reinstall PyJAGS.

macOS: found architecture 'x86_64', required architecture 'arm64'

This occurs on Apple Silicon Macs when JAGS was installed via an Intel (Rosetta) Homebrew at /usr/local but Python is running natively as ARM64. The fix is to install JAGS using the native ARM Homebrew at /opt/homebrew:

# Check which Homebrew you are using
which brew
# /opt/homebrew/bin/brew  -> ARM (correct for Apple Silicon)
# /usr/local/bin/brew     -> Intel/Rosetta (will cause architecture mismatch)

# If needed, install ARM Homebrew and then:
/opt/homebrew/bin/brew install jags

Make sure PKG_CONFIG_PATH points to the ARM Homebrew pkgconfig directory (/opt/homebrew/lib/pkgconfig) so that the build picks up the correct library.

CMake errors during build

PyJAGS uses CMake (via scikit-build-core) to find and link against JAGS. If the build fails with CMake errors, ensure CMake is installed:

# macOS
brew install cmake

# Debian/Ubuntu
sudo apt-get install cmake

# pip
pip install cmake

Useful Links

Acknowledgements

  • JAGS was created by Martyn Plummer
  • PyJAGS was originally created by Tomasz Miasko
  • As of May 2020, PyJAGS is developed by Michael Nowotny
  • Max Schulist (PR #1) proposed migrating to scikit-build-core, pyproject.toml, and pytest — ideas that inspired the packaging modernization in version 2.1

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

pyjags-2.2.0.tar.gz (43.1 kB view details)

Uploaded Source

Built Distributions

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

pyjags-2.2.0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (5.8 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

pyjags-2.2.0-cp314-cp314-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (5.7 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

pyjags-2.2.0-cp314-cp314-macosx_15_0_x86_64.whl (156.5 kB view details)

Uploaded CPython 3.14macOS 15.0+ x86-64

pyjags-2.2.0-cp314-cp314-macosx_15_0_arm64.whl (331.8 kB view details)

Uploaded CPython 3.14macOS 15.0+ ARM64

pyjags-2.2.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (5.8 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

pyjags-2.2.0-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (5.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

pyjags-2.2.0-cp313-cp313-macosx_15_0_x86_64.whl (156.3 kB view details)

Uploaded CPython 3.13macOS 15.0+ x86-64

pyjags-2.2.0-cp313-cp313-macosx_15_0_arm64.whl (331.4 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

pyjags-2.2.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (5.8 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

pyjags-2.2.0-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (5.7 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

pyjags-2.2.0-cp312-cp312-macosx_15_0_x86_64.whl (156.3 kB view details)

Uploaded CPython 3.12macOS 15.0+ x86-64

pyjags-2.2.0-cp312-cp312-macosx_15_0_arm64.whl (331.3 kB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

File details

Details for the file pyjags-2.2.0.tar.gz.

File metadata

  • Download URL: pyjags-2.2.0.tar.gz
  • Upload date:
  • Size: 43.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyjags-2.2.0.tar.gz
Algorithm Hash digest
SHA256 1fc611ebd7b7c67c35e5ac6ec848a248796fec9056ef27d750f5839d33fda6bf
MD5 430e0e39cd3df3e3f369effa31bee354
BLAKE2b-256 1d4bcc82cfbf5a17afd955293907f1b84fdf685711a7d04232f04ef5beda24b6

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyjags-2.2.0.tar.gz:

Publisher: release.yml on michaelnowotny/pyjags

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyjags-2.2.0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pyjags-2.2.0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 39c15a43bd1dfef86e8857c754d45d5eb1a7afe87a219d74324af0bd266b4e8b
MD5 f14187fbecc74945d2a0de0ccf0f03ad
BLAKE2b-256 8d1bf4a6304c58d83a480ee87c46fb83ea6a0dae7cb0f44109dfb4c1f637558f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyjags-2.2.0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on michaelnowotny/pyjags

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyjags-2.2.0-cp314-cp314-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pyjags-2.2.0-cp314-cp314-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 4b9469c48dae0c2c8bef24ea774dff81b3cf110ef75d0402e17c83b758d63231
MD5 28c5526725e2a80347ed402f097edc16
BLAKE2b-256 e00b9b638370733ec372045df8a0064400b3378039e23dfcb6ac7c287a4e473f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyjags-2.2.0-cp314-cp314-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl:

Publisher: release.yml on michaelnowotny/pyjags

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyjags-2.2.0-cp314-cp314-macosx_15_0_x86_64.whl.

File metadata

File hashes

Hashes for pyjags-2.2.0-cp314-cp314-macosx_15_0_x86_64.whl
Algorithm Hash digest
SHA256 84a307e64d19fd4327e6ab0f983b65f8642a53bb1db0c8a2e2424242afceb216
MD5 e5a27a301b86c7020c3dec69b65d8afb
BLAKE2b-256 0fe8e61aee75d5f191639962e6d0e461f417d6691512c740f9ba5b7ffac7eef4

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyjags-2.2.0-cp314-cp314-macosx_15_0_x86_64.whl:

Publisher: release.yml on michaelnowotny/pyjags

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyjags-2.2.0-cp314-cp314-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pyjags-2.2.0-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 484a1dfe92c704995d43c66342c9bd160b042036f684387edf322cad13d55fcf
MD5 247185893dd8fc38b363f6801ffa1fe0
BLAKE2b-256 759d56eda579784d7222c803622f3280b0c037f4c4c9473b808cc72d30a6a388

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyjags-2.2.0-cp314-cp314-macosx_15_0_arm64.whl:

Publisher: release.yml on michaelnowotny/pyjags

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyjags-2.2.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pyjags-2.2.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f42031943251f87e8928296c13d5c2e76c25f9307db885d19db4761e79f5190b
MD5 f38ea5ea5b01dcc5ac7327b12e73d7bb
BLAKE2b-256 60af9cfc1a40c18f36059bcbda48660d1849880eeab0a9cc231ad43fb145d517

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyjags-2.2.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on michaelnowotny/pyjags

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyjags-2.2.0-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pyjags-2.2.0-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 34a16381aff6df411a54b7205ef7292d821fc629c414080e39d013f5d901b5a9
MD5 404b3763fb4d90596b14e0741dbd617d
BLAKE2b-256 b15e87fe526cf96fbf1d4f3557542f04a789601ed72512535a091b64a9900e49

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyjags-2.2.0-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl:

Publisher: release.yml on michaelnowotny/pyjags

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyjags-2.2.0-cp313-cp313-macosx_15_0_x86_64.whl.

File metadata

File hashes

Hashes for pyjags-2.2.0-cp313-cp313-macosx_15_0_x86_64.whl
Algorithm Hash digest
SHA256 29b24962afcf23bff823223ed06a954e7837d7dfe04a5b6db7d6965f5714400b
MD5 93110e8892c0eaada55af6c5f3fae22d
BLAKE2b-256 ce19ebed6ae224a121f1386e315137e89cd6f35f261f5b5e1b8d72aeebb443bf

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyjags-2.2.0-cp313-cp313-macosx_15_0_x86_64.whl:

Publisher: release.yml on michaelnowotny/pyjags

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyjags-2.2.0-cp313-cp313-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pyjags-2.2.0-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 6d533ab46030b92ed8556cc52d7f53438779e12e1819860bd6f7c52f2eb26ef8
MD5 3ff68016962ec84d64fbee7c4990b2a1
BLAKE2b-256 407f8b265810d0111eb29bca0d7667b226af35adab72bba14bb55027dc30c799

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyjags-2.2.0-cp313-cp313-macosx_15_0_arm64.whl:

Publisher: release.yml on michaelnowotny/pyjags

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyjags-2.2.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pyjags-2.2.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4210e4eeadf812d429e35f9f9b4caed257f30c43700a8f4469f03f368868032c
MD5 635e658fead7eba0fbe99fdb1240161a
BLAKE2b-256 51eb7af3eecab53bb41a62a9a68dd2e0251122bd67e76490bbaff9846ab421d8

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyjags-2.2.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on michaelnowotny/pyjags

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyjags-2.2.0-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pyjags-2.2.0-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 6475ed7ca11e9842c026298077ed51aa55c5a9b9eae471f65e616113e8d9abbc
MD5 7b2af183b53a118427d02a82c52ffea5
BLAKE2b-256 9b5532d186c19aef48b833fcbea3443bd9b6a301ada925e5809988cfcc1f24f8

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyjags-2.2.0-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl:

Publisher: release.yml on michaelnowotny/pyjags

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyjags-2.2.0-cp312-cp312-macosx_15_0_x86_64.whl.

File metadata

File hashes

Hashes for pyjags-2.2.0-cp312-cp312-macosx_15_0_x86_64.whl
Algorithm Hash digest
SHA256 e0415b6dedb8eba6fa4ea9a8bdd07368789967b4af60b0f057a75fa97e3e585f
MD5 eb830810c0787eec403d9394fa85c04a
BLAKE2b-256 b8739bbbe7f3ea92c90c989c77023ca9247941a9d0ad6a0730d6b9e5158dca90

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyjags-2.2.0-cp312-cp312-macosx_15_0_x86_64.whl:

Publisher: release.yml on michaelnowotny/pyjags

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyjags-2.2.0-cp312-cp312-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pyjags-2.2.0-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 cc1034a3d8ba31c0c43efc69652b18b073afd30ac19bffb50e2b33588597af90
MD5 c61ce91c558b2af4869fb9de0cfcce91
BLAKE2b-256 58b219301458ffe027be0127f0afaf99464f3efa688767fe47d7b96407cac718

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyjags-2.2.0-cp312-cp312-macosx_15_0_arm64.whl:

Publisher: release.yml on michaelnowotny/pyjags

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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