Alterlab pymc

From NVlabs/Skill2Env · View interactive version

Alterlab pymc is an AI agent skill from the GitHub repository NVlabs/Skill2Env. Bayesian modeling and probabilistic programming with PyMC — hierarchical models, MCMC (NUTS) sampling, variational inference, LOO/WAIC model comparison, and posterior predictive checks. Use when fitting Bayesian or hierarchical models, estimating posteriors and credible intervals, running probabilis

How to use this skill

Skill file

PyMC Bayesian Modeling

Overview

PyMC is a Python library for Bayesian modeling and probabilistic programming. Build, fit, validate, and compare Bayesian models using PyMC's modern API (version 5.x+), including hierarchical models, MCMC sampling (NUTS), variational inference, and model comparison (LOO, WAIC).

When to Use This Skill

This skill should be used when:

Standard Bayesian Workflow

Follow this 8-step workflow for building and validating Bayesian models:

  1. Data preparation — standardize predictors, handle missing data, set up coords
  2. Model building — weakly informative priors, named dims, pm.Data() for predictables
  3. Prior predictive check — pm.sample_prior_predictive; validate priors before fitting
  4. Fit — pm.sample(draws=2000, tune=1000, chains=4, target_accept=0.9); include log_likelihood=True for comparison
  5. Diagnostics — R-hat < 1.01, ESS > 400, no divergences, good trace mixing
  6. Posterior predictive check — pm.sample_posterior_predictive; check fit vs. observed data
  7. Analyze — az.summary, az.plot_posterior, az.plot_forest
  8. 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.

Critical: Always use non-centered parameterization for hierarchical models to avoid 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

Code and decision rules: references/model_comparison.md. Detailed sampling-algorithm guide:

references/sampling_inference.md.

Best Practices

Model Building

  1. Always standardize predictors for better sampling
  2. Use weakly informative priors (not flat)
  3. Use named dimensions (dims) for clarity
  4. Non-centered parameterization for hierarchical models
  5. Check prior predictive before fitting

Sampling

  1. Run multiple chains (at least 4) for convergence
  2. Use target_accept=0.9 as baseline (higher if needed)
  3. Include log_likelihood=True for model comparison
  4. Set random seed for reproducibility

Validation

  1. Check diagnostics before interpretation (R-hat, ESS, divergences)
  2. Posterior predictive check for model validation
  3. Compare multiple models when appropriate
  4. 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/`)

Scripts (`scripts/`)

Templates (`assets/`)

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)

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}, ic='loo')

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

Common gotchas (PyMC 5.x / ArviZ):

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.