PyMC Bayesian Modeling
Overview
PyMC is a Python library for Bayesian modeling and probabilistic programming. Build, fit, validate, and compare Bayesian models using the current API (PyMC ≥ 6 with ArviZ ≥ 1.1), including hierarchical models, MCMC sampling (NUTS), variational inference, and PSIS-LOO model comparison.
# no install step (PyMC 6 and ArviZ 1.x need Python >= 3.12): nutpie sampler, plotting backend, NetCDF I/O
uv run --python ">=3.12" --with "pymc[nutpie]>=6" --with "arviz[matplotlib,h5netcdf]>=1.1" python model.py
PyMC 6 changed several defaults and names that most tutorials still use — read Version notes at the end before reusing PyMC 5 / ArviZ 0.x code.
When to Use This Skill
This skill should be used when:
- Building Bayesian models (linear/logistic regression, hierarchical models, time series, etc.)
- Performing MCMC sampling or variational inference
- Conducting prior/posterior predictive checks
- Diagnosing sampling issues (divergences, convergence, ESS)
- Comparing multiple models with PSIS-LOO cross-validation
- Implementing uncertainty quantification through Bayesian methods
- Working with hierarchical/multilevel data structures
- Handling missing data or measurement error in a principled way
Does NOT Trigger
| Scenario |
Use Instead |
| Frequentist regression with p-values, standard errors, and residual diagnostics |
alterlab-statsmodels |
| Reporting-focused mixed-effects model for nested data (lme4/glmmTMB/brms, statsmodels MixedLM, or bambi formulas) with ICC and variance-component reporting |
alterlab-multilevel-models |
| Pooling effect sizes across studies (fixed/random effects, heterogeneity, funnel plots) |
alterlab-meta-analysis |
| Point-prediction machine learning (random forests, gradient boosting, CV tuning) |
alterlab-scikit-learn |
Hand-off rule. When the request matches a row above, open your answer by naming that skill by its exact id (for example alterlab-statsmodels) and saying in one line why it fits better, then stop applying this skill's procedure. Name it even when you also need to ask for missing input. If only part of the request is out of scope, name the skill for that part and do only the in-scope part. A brief general pointer is fine; the named skill does the work.
Standard Bayesian Workflow
Follow this 8-step workflow for building and validating Bayesian models:
- Data preparation — standardize predictors, handle missing data, set up
coords
- Model building — weakly informative priors, named
dims, pm.Data() for predictables
- Prior predictive check —
pm.sample_prior_predictive; validate priors before fitting
- Fit —
pm.sample(draws=2000, tune=1000, chains=4, target_accept=0.9), then pm.compute_log_likelihood(idata) if you will compare models
- Diagnostics — R-hat < 1.01, ESS > 400, no divergences, good trace mixing
- Posterior predictive check —
pm.sample_posterior_predictive; check fit vs. observed data
- Analyze —
az.summary, az.plot_dist (was plot_posterior), az.plot_forest
- Predict —
pm.set_data then pm.sample_posterior_predictive; extract HDI intervals
Full step-by-step code: references/workflow_examples.md.
Common Model Patterns
PyMC supports linear/logistic/Poisson regression, hierarchical (multilevel) models, and
time-series (AR). Ready-to-adapt code for each lives in references/model_patterns.md.
Use the non-centered parameterization for hierarchical models when groups have few observations: the centered form creates funnel geometry that NUTS explores poorly, which shows up as divergences.
Templates: assets/linear_regression_template.py, assets/hierarchical_model_template.py.
Distribution Selection
Choosing priors and likelihoods is the highest-leverage modeling decision. A quick chooser
(scale params, unbounded, positive, probabilities, correlation matrices; continuous/count/binary/
categorical likelihoods) is in references/distribution_selection.md. The comprehensive catalog
is in references/distributions.md.
Model Comparison and Sampling
- Compare models with PSIS-LOO (
scripts/model_comparison.py); interpret elpd_diff against dse and check Pareto-k. ArviZ 1.x has no WAIC.
- Sample with NUTS by default (nutpie when installed); raise
target_accept for divergences; ADVI for fast approximation.
- Diagnose with
scripts/model_diagnostics.py (check_diagnostics, create_diagnostic_report).
- Troubleshoot divergences, low ESS, high R-hat, slow sampling.
Code and decision rules: references/model_comparison.md. Detailed sampling-algorithm guide:
references/sampling_inference.md.
Best Practices
Model Building
- Always standardize predictors for better sampling
- Use weakly informative priors (not flat)
- Use named dimensions (
dims) for clarity
- Non-centered parameterization for hierarchical models
- Check prior predictive before fitting
Sampling
- Run multiple chains (at least 4) for convergence
- Use
target_accept=0.9 as baseline (higher if needed)
- Call
pm.compute_log_likelihood(idata) before model comparison
- Set random seed for reproducibility
Validation
- Check diagnostics before interpretation (R-hat, ESS, divergences)
- Posterior predictive check for model validation
- Compare multiple models when appropriate
- Report uncertainty (HDI intervals, not just point estimates)
Start simple and add complexity gradually, iterating on the model based on each
predictive check (see the 8-step workflow above).
Resources
This skill includes:
References (references/)
workflow_examples.md: Full step-by-step code for the 8-step Bayesian workflow (data prep → predictions).
model_patterns.md: Ready-to-adapt code for linear/logistic/Poisson regression, hierarchical, and AR time-series models.
distribution_selection.md: Quick chooser for priors and likelihoods by parameter/outcome type.
model_comparison.md: PSIS-LOO comparison, diagnostic scripts, and troubleshooting (divergences, ESS, R-hat, slow sampling).
distributions.md: Comprehensive catalog of PyMC distributions organized by category (continuous, discrete, multivariate, mixture, time series). Use when selecting priors or likelihoods.
sampling_inference.md: Detailed guide to sampling algorithms (NUTS, Metropolis, SMC), variational inference (ADVI, SVGD), and handling sampling issues. Use when encountering convergence problems or choosing inference methods.
workflows.md: A single end-to-end runnable script (data prep → save) plus extra cookbook recipes not in workflow_examples.md — missing-data imputation, QR reparameterization, mixture models, and a model-averaging helper.
Scripts (scripts/)
model_diagnostics.py: Automated diagnostic checking and report generation (handles both PyMC-NUTS and nutpie sample stats). Functions: check_diagnostics() for quick checks, create_diagnostic_report() for comprehensive analysis with plots.
model_comparison.py: PSIS-LOO model comparison utilities. Functions: compare_models(), check_loo_reliability(), model_averaging().
Templates (assets/)
linear_regression_template.py: Complete template for Bayesian linear regression with full workflow (data prep, prior checks, fitting, diagnostics, predictions).
hierarchical_model_template.py: Complete template for hierarchical/multilevel models with non-centered parameterization and group-level analysis.
Quick Reference
Model Building
with pm.Model(coords={'var': names}) as model:
# Priors
param = pm.Normal('param', mu=0, sigma=1, dims='var')
# Likelihood
y = pm.Normal('y', mu=..., sigma=..., observed=data)
Sampling
idata = pm.sample(draws=2000, tune=1000, chains=4, target_accept=0.9) # returns xarray.DataTree
pm.compute_log_likelihood(idata) # needed for LOO / az.compare
Diagnostics
from scripts.model_diagnostics import check_diagnostics
check_diagnostics(idata)
Model Comparison
from scripts.model_comparison import compare_models
compare_models({'m1': idata1, 'm2': idata2}) # PSIS-LOO; higher elpd is better
Predictions
# X must have been wrapped at build time: pm.Data('X', X, dims=('obs', 'predictors'))
with model:
pm.set_data({'X': X_new}, coords={'obs': range(len(X_new))})
pm.sample_posterior_predictive(idata, predictions=True, extend_inferencedata=True)
# predictions land in idata.predictions
Additional Notes
- PyMC integrates with ArviZ for visualization and diagnostics
- Use
pm.model_to_graphviz(model) to visualize model structure (needs the graphviz package)
- Save results with
idata.to_netcdf('results.nc') (requires h5netcdf or netCDF4, e.g. arviz[h5netcdf]); load with az.from_netcdf('results.nc')
- For very large models, consider minibatch ADVI or data subsampling
Version notes (PyMC 6 / ArviZ 1.x)
PyMC 6.0 (May 2026) moved to ArviZ 1.x and PyTensor 3. Code written for PyMC 5 / ArviZ 0.x fails in the ways below.
- Results are an
xarray.DataTree, not az.InferenceData. Group access (idata.posterior, idata["posterior"]) still works; idata.extend(other) became idata.update(other); list groups with list(idata.children).
- Log-likelihood: call
pm.compute_log_likelihood(idata) after sampling. pm.sample(idata_kwargs={"log_likelihood": True}) is deprecated.
- Sampler: nutpie is the default NUTS sampler when installed (400 tuning steps by default; PyMC's own NUTS keeps 1000). Select explicitly with
nuts_sampler="pymc" | "nutpie" | "numpyro" | "blackjax". Sample-stat names differ (tree_depth vs depth), so diagnostics code should check which exists. Starting values go in initvals= (start= fails).
- Backend: PyTensor 3 compiles with Numba by default, so the first call includes JIT-compilation time.
- Summaries:
az.summary reports 89% equal-tailed intervals (eti89_lb, eti89_ub) instead of 94% HDI (hdi_3%, hdi_97%). Use az.summary(idata, ci_kind="hdi", ci_prob=0.94) or set az.rcParams["stats.ci_kind"] / ["stats.ci_prob"]. az.hdi(..., prob=0.95) replaces hdi_prob=.
- Plots return a
PlotCollection (save with pc.savefig("file.png"), show with pc.show()); ax= / axes= arguments are gone. Renames: plot_posterior → plot_dist, plot_ppc → plot_ppc_dist (prior checks: group="prior_predictive", num_samples= instead of num_pp_samples=), plot_trace → plot_trace_dist (trace plus density), plot_dist_comparison → plot_prior_posterior. Install a backend with arviz[matplotlib].
- Model comparison:
az.waic is gone and az.compare(dict) takes no ic=/scale=; it ranks by PSIS-LOO elpd (higher is better) with stacking weights and reports elpd_diff, dse, p_worse, and diag_* columns.
- To predict on new data, the predictors must be wrapped in
pm.Data('X', X, dims=...) at build time — only then can pm.set_data({'X': X_new}, coords={...}) swap them. A plain NumPy array baked into the graph cannot be replaced.
pm.sample_prior_predictive takes draws= (the old samples= keyword was removed).
- For out-of-sample predictions call
pm.sample_posterior_predictive(idata, predictions=True, extend_inferencedata=True, ...); results then live in idata.predictions, not idata.posterior_predictive.
1---2name: alterlab-pymc3description: Bayesian modeling and probabilistic programming with PyMC 6 and ArviZ 1.x — hierarchical models, MCMC (NUTS via PyMC, nutpie, NumPyro, or BlackJAX), variational inference, PSIS-LOO model comparison, and prior/posterior predictive checks. Use when fitting Bayesian or hierarchical models, estimating posteriors and credible intervals, diagnosing divergences, R-hat, or ESS, running probabilistic inference, or comparing models with LOO (WAIC was removed from ArviZ 1.x). Part of the AlterLab Academic Skills suite.4license: Apache-2.05allowed-tools: Read Write Edit Bash(python:*) Bash(uv:*)6compatibility: No API key required. Runs locally via `uv run --python ">=3.12" --with "pymc[nutpie]>=6" --with "arviz[matplotlib]>=1.1" python script.py` (no install step; the interpreter pin matters because an older default Python resolves older releases); requires pymc >= 6 (current 6.3 as of 2026-09) with ArviZ >= 1.1 and PyTensor 3; nutpie optional but used by default when installed.7metadata:8 source: https://skillmd.com/skills/alterlab-ieu/alterlab-pymc9 skill-author: AlterLab10 version: "1.1.0"11 last_updated: "2026-09-23"12---1314# PyMC Bayesian Modeling1516## Overview1718PyMC is a Python library for Bayesian modeling and probabilistic programming. Build, fit, validate, and compare Bayesian models using the current API (PyMC ≥ 6 with ArviZ ≥ 1.1), including hierarchical models, MCMC sampling (NUTS), variational inference, and PSIS-LOO model comparison.1920```bash21# no install step (PyMC 6 and ArviZ 1.x need Python >= 3.12): nutpie sampler, plotting backend, NetCDF I/O22uv run --python ">=3.12" --with "pymc[nutpie]>=6" --with "arviz[matplotlib,h5netcdf]>=1.1" python model.py23```2425PyMC 6 changed several defaults and names that most tutorials still use — read **Version notes** at the end before reusing PyMC 5 / ArviZ 0.x code.2627## When to Use This Skill2829This skill should be used when:30- Building Bayesian models (linear/logistic regression, hierarchical models, time series, etc.)31- Performing MCMC sampling or variational inference32- Conducting prior/posterior predictive checks33- Diagnosing sampling issues (divergences, convergence, ESS)34- Comparing multiple models with PSIS-LOO cross-validation35- Implementing uncertainty quantification through Bayesian methods36- Working with hierarchical/multilevel data structures37- Handling missing data or measurement error in a principled way3839### Does NOT Trigger4041| Scenario | Use Instead |42|----------|-------------|43| Frequentist regression with p-values, standard errors, and residual diagnostics | `alterlab-statsmodels` |44| Reporting-focused mixed-effects model for nested data (lme4/glmmTMB/brms, statsmodels MixedLM, or bambi formulas) with ICC and variance-component reporting | `alterlab-multilevel-models` |45| Pooling effect sizes across studies (fixed/random effects, heterogeneity, funnel plots) | `alterlab-meta-analysis` |46| Point-prediction machine learning (random forests, gradient boosting, CV tuning) | `alterlab-scikit-learn` |4748**Hand-off rule.** When the request matches a row above, open your answer by naming that skill by its exact id (for example `alterlab-statsmodels`) and saying in one line why it fits better, then stop applying this skill's procedure. Name it even when you also need to ask for missing input. If only part of the request is out of scope, name the skill for that part and do only the in-scope part. A brief general pointer is fine; the named skill does the work.4950## Standard Bayesian Workflow5152Follow this 8-step workflow for building and validating Bayesian models:53541. **Data preparation** — standardize predictors, handle missing data, set up `coords`552. **Model building** — weakly informative priors, named `dims`, `pm.Data()` for predictables563. **Prior predictive check** — `pm.sample_prior_predictive`; validate priors *before* fitting574. **Fit** — `pm.sample(draws=2000, tune=1000, chains=4, target_accept=0.9)`, then `pm.compute_log_likelihood(idata)` if you will compare models585. **Diagnostics** — R-hat < 1.01, ESS > 400, no divergences, good trace mixing596. **Posterior predictive check** — `pm.sample_posterior_predictive`; check fit vs. observed data607. **Analyze** — `az.summary`, `az.plot_dist` (was `plot_posterior`), `az.plot_forest`618. **Predict** — `pm.set_data` then `pm.sample_posterior_predictive`; extract HDI intervals6263Full step-by-step code: `references/workflow_examples.md`.6465## Common Model Patterns6667PyMC supports linear/logistic/Poisson regression, hierarchical (multilevel) models, and68time-series (AR). Ready-to-adapt code for each lives in `references/model_patterns.md`.6970Use the non-centered parameterization for hierarchical models when groups have few observations: the centered form creates funnel geometry that NUTS explores poorly, which shows up as divergences.71Templates: `assets/linear_regression_template.py`, `assets/hierarchical_model_template.py`.7273## Distribution Selection7475Choosing priors and likelihoods is the highest-leverage modeling decision. A quick chooser76(scale params, unbounded, positive, probabilities, correlation matrices; continuous/count/binary/77categorical likelihoods) is in `references/distribution_selection.md`. The comprehensive catalog78is in `references/distributions.md`.7980## Model Comparison and Sampling8182- **Compare models** with PSIS-LOO (`scripts/model_comparison.py`); interpret `elpd_diff` against `dse` and check Pareto-k. ArviZ 1.x has no WAIC.83- **Sample** with NUTS by default (nutpie when installed); raise `target_accept` for divergences; ADVI for fast approximation.84- **Diagnose** with `scripts/model_diagnostics.py` (`check_diagnostics`, `create_diagnostic_report`).85- **Troubleshoot** divergences, low ESS, high R-hat, slow sampling.8687Code and decision rules: `references/model_comparison.md`. Detailed sampling-algorithm guide:88`references/sampling_inference.md`.8990## Best Practices9192### Model Building93941. **Always standardize predictors** for better sampling952. **Use weakly informative priors** (not flat)963. **Use named dimensions** (`dims`) for clarity974. **Non-centered parameterization** for hierarchical models985. **Check prior predictive** before fitting99100### Sampling1011021. **Run multiple chains** (at least 4) for convergence1032. **Use `target_accept=0.9`** as baseline (higher if needed)1043. **Call `pm.compute_log_likelihood(idata)`** before model comparison1054. **Set random seed** for reproducibility106107### Validation1081091. **Check diagnostics** before interpretation (R-hat, ESS, divergences)1102. **Posterior predictive check** for model validation1113. **Compare multiple models** when appropriate1124. **Report uncertainty** (HDI intervals, not just point estimates)113114Start simple and add complexity gradually, iterating on the model based on each115predictive check (see the 8-step workflow above).116117## Resources118119This skill includes:120121### References (`references/`)122123- **`workflow_examples.md`**: Full step-by-step code for the 8-step Bayesian workflow (data prep → predictions).124- **`model_patterns.md`**: Ready-to-adapt code for linear/logistic/Poisson regression, hierarchical, and AR time-series models.125- **`distribution_selection.md`**: Quick chooser for priors and likelihoods by parameter/outcome type.126- **`model_comparison.md`**: PSIS-LOO comparison, diagnostic scripts, and troubleshooting (divergences, ESS, R-hat, slow sampling).127- **`distributions.md`**: Comprehensive catalog of PyMC distributions organized by category (continuous, discrete, multivariate, mixture, time series). Use when selecting priors or likelihoods.128- **`sampling_inference.md`**: Detailed guide to sampling algorithms (NUTS, Metropolis, SMC), variational inference (ADVI, SVGD), and handling sampling issues. Use when encountering convergence problems or choosing inference methods.129- **`workflows.md`**: A single end-to-end runnable script (data prep → save) plus extra cookbook recipes not in `workflow_examples.md` — missing-data imputation, QR reparameterization, mixture models, and a model-averaging helper.130131### Scripts (`scripts/`)132133- **`model_diagnostics.py`**: Automated diagnostic checking and report generation (handles both PyMC-NUTS and nutpie sample stats). Functions: `check_diagnostics()` for quick checks, `create_diagnostic_report()` for comprehensive analysis with plots.134135- **`model_comparison.py`**: PSIS-LOO model comparison utilities. Functions: `compare_models()`, `check_loo_reliability()`, `model_averaging()`.136137### Templates (`assets/`)138139- **`linear_regression_template.py`**: Complete template for Bayesian linear regression with full workflow (data prep, prior checks, fitting, diagnostics, predictions).140141- **`hierarchical_model_template.py`**: Complete template for hierarchical/multilevel models with non-centered parameterization and group-level analysis.142143## Quick Reference144145### Model Building146```python147with pm.Model(coords={'var': names}) as model:148 # Priors149 param = pm.Normal('param', mu=0, sigma=1, dims='var')150 # Likelihood151 y = pm.Normal('y', mu=..., sigma=..., observed=data)152```153154### Sampling155```python156idata = pm.sample(draws=2000, tune=1000, chains=4, target_accept=0.9) # returns xarray.DataTree157pm.compute_log_likelihood(idata) # needed for LOO / az.compare158```159160### Diagnostics161```python162from scripts.model_diagnostics import check_diagnostics163check_diagnostics(idata)164```165166### Model Comparison167```python168from scripts.model_comparison import compare_models169compare_models({'m1': idata1, 'm2': idata2}) # PSIS-LOO; higher elpd is better170```171172### Predictions173```python174# X must have been wrapped at build time: pm.Data('X', X, dims=('obs', 'predictors'))175with model:176 pm.set_data({'X': X_new}, coords={'obs': range(len(X_new))})177 pm.sample_posterior_predictive(idata, predictions=True, extend_inferencedata=True)178# predictions land in idata.predictions179```180181## Additional Notes182183- PyMC integrates with ArviZ for visualization and diagnostics184- Use `pm.model_to_graphviz(model)` to visualize model structure (needs the `graphviz` package)185- Save results with `idata.to_netcdf('results.nc')` (requires `h5netcdf` or `netCDF4`, e.g. `arviz[h5netcdf]`); load with `az.from_netcdf('results.nc')`186- For very large models, consider minibatch ADVI or data subsampling187188## Version notes (PyMC 6 / ArviZ 1.x)189190PyMC 6.0 (May 2026) moved to ArviZ 1.x and PyTensor 3. Code written for PyMC 5 / ArviZ 0.x fails in the ways below.191192- **Results are an `xarray.DataTree`**, not `az.InferenceData`. Group access (`idata.posterior`, `idata["posterior"]`) still works; `idata.extend(other)` became `idata.update(other)`; list groups with `list(idata.children)`.193- **Log-likelihood**: call `pm.compute_log_likelihood(idata)` after sampling. `pm.sample(idata_kwargs={"log_likelihood": True})` is deprecated.194- **Sampler**: nutpie is the default NUTS sampler when installed (400 tuning steps by default; PyMC's own NUTS keeps 1000). Select explicitly with `nuts_sampler="pymc" | "nutpie" | "numpyro" | "blackjax"`. Sample-stat names differ (`tree_depth` vs `depth`), so diagnostics code should check which exists. Starting values go in `initvals=` (`start=` fails).195- **Backend**: PyTensor 3 compiles with Numba by default, so the first call includes JIT-compilation time.196- **Summaries**: `az.summary` reports 89% equal-tailed intervals (`eti89_lb`, `eti89_ub`) instead of 94% HDI (`hdi_3%`, `hdi_97%`). Use `az.summary(idata, ci_kind="hdi", ci_prob=0.94)` or set `az.rcParams["stats.ci_kind"]` / `["stats.ci_prob"]`. `az.hdi(..., prob=0.95)` replaces `hdi_prob=`.197- **Plots** return a `PlotCollection` (save with `pc.savefig("file.png")`, show with `pc.show()`); `ax=` / `axes=` arguments are gone. Renames: `plot_posterior` → `plot_dist`, `plot_ppc` → `plot_ppc_dist` (prior checks: `group="prior_predictive"`, `num_samples=` instead of `num_pp_samples=`), `plot_trace` → `plot_trace_dist` (trace plus density), `plot_dist_comparison` → `plot_prior_posterior`. Install a backend with `arviz[matplotlib]`.198- **Model comparison**: `az.waic` is gone and `az.compare(dict)` takes no `ic=`/`scale=`; it ranks by PSIS-LOO `elpd` (higher is better) with stacking weights and reports `elpd_diff`, `dse`, `p_worse`, and `diag_*` columns.199- To predict on new data, the predictors must be wrapped in `pm.Data('X', X, dims=...)` at build time — only then can `pm.set_data({'X': X_new}, coords={...})` swap them. A plain NumPy array baked into the graph cannot be replaced.200- `pm.sample_prior_predictive` takes `draws=` (the old `samples=` keyword was removed).201- For out-of-sample predictions call `pm.sample_posterior_predictive(idata, predictions=True, extend_inferencedata=True, ...)`; results then live in `idata.predictions`, not `idata.posterior_predictive`.202