# pymc

> Bayesian modeling with PyMC — build hierarchical models, run MCMC with NUTS sampler, perform variational inference, LOO/WAIC model comparison, and posterior predictive checks for probabilistic programming and full Bayesian inference.

**Use case**: Build hierarchical Bayesian models with MCMC and posterior checks

**Canonical URL**: https://agentcookbooks.com/skills/pymc/

**Topics**: claude-code, skills, science, data-science

**Trigger phrases**: "Bayesian model with PyMC", "hierarchical model", "MCMC sampling", "posterior distribution", "Bayesian inference"

**Source**: [K-Dense AI](https://github.com/K-Dense-AI/scientific-agent-skills/tree/main/scientific-skills/pymc)

**License**: MIT

---

## What it does

`pymc` is a Claude Code skill from K-Dense AI's [scientific-agent-skills repo](https://github.com/K-Dense-AI/scientific-agent-skills). It turns Claude into a PyMC expert for probabilistic programming — specifying prior distributions, building hierarchical model structures, sampling with the NUTS sampler, running variational inference (ADVI) for faster approximate posteriors, comparing models with LOO/WAIC, and validating fits with posterior predictive checks.

A session produces a complete Bayesian analysis: model specification in PyMC's model context, MCMC sampling with convergence diagnostics (R-hat, effective sample size), posterior summary statistics, and visualization of the posterior distributions and predictive checks with ArviZ.

## When to use it

Reach for it when:

- You need full posterior distributions rather than point estimates and confidence intervals
- You're building hierarchical models where partial pooling across groups is theoretically motivated
- You want to formally propagate uncertainty through a prediction pipeline using Bayesian updating

When *not* to reach for it:

- Frequentist inference with standard tests — use `statistical-analysis` or `statsmodels`
- Predictive ML pipelines where interpretability of uncertainty isn't the goal — use `scikit-learn`

## Install

Copy the `SKILL.md` from K-Dense AI's [pymc folder](https://github.com/K-Dense-AI/scientific-agent-skills/tree/main/scientific-skills/pymc) into `.claude/skills/pymc/` in your project.

Trigger phrases: "Bayesian model with PyMC", "hierarchical model", "MCMC sampling", "posterior distribution".

## What a session looks like

A typical session has three phases:

1. **Model specification.** Describe the data generating process — likelihood function, prior beliefs about parameters, and any group structure for hierarchical pooling. Claude writes the PyMC model context with named priors and likelihood.
2. **Sampling and diagnostics.** MCMC runs with `pm.sample()`. Claude checks convergence diagnostics (R-hat < 1.01, bulk-ESS > 400) and warns if chains didn't mix.
3. **Posterior analysis.** ArviZ plots visualize the posterior distributions and trace plots. LOO or WAIC compares models if multiple are specified, and posterior predictive checks verify the model generates data consistent with observations.

## Receipts

**Where it works well:**
- Hierarchical models for grouped data (patients nested in hospitals, students nested in schools) where partial pooling outperforms both complete pooling and no pooling
- Small sample sizes where prior information meaningfully improves estimates — PyMC makes the prior specification explicit and auditable

**Where it backfires:**
- High-dimensional parameter spaces slow NUTS sampling dramatically; variational inference (ADVI) is faster but approximate
- Convergence failures require understanding of model geometry and reparameterization, which is difficult to diagnose without experience in Bayesian workflow

**Pattern that works:** start with a simple, non-hierarchical model first, verify it samples well and recovers known parameters on simulated data, then add hierarchical structure incrementally.

## Source and attribution

Originally authored by [K-Dense Inc.](https://github.com/K-Dense-AI). The canonical SKILL.md lives in the [`pymc` folder](https://github.com/K-Dense-AI/scientific-agent-skills/tree/main/scientific-skills/pymc) of their public scientific-agent-skills repository.

License: MIT. Install, adapt, and redistribute with attribution preserved.

This page documents the skill from a practitioner's perspective. For the formal spec and any updates, defer to the source repo.