← Explainer Library

Interactive Explainer

Probabilistic Programming, in Practice

PyMC, NumPyro, Stan — the productive recipe for Bayesian modelling. Declare the model, pick an inference engine, inspect diagnostics. Two live demos in the browser plus the checklist of what to look at after every run.

Step 1

What a PPL is — and isn't

A probabilistic programming language lets you write a generative model as code. You declare priors, sampling distributions, observations; the runtime takes care of posterior inference. The same model can be run with HMC, NUTS, ADVI, SMC, or Laplace approximation depending on a one-line switch.

ToolBackendStrengthsQuirks
StanCustom + C++Best NUTS sampler; rock-solid for hierarchical modelsDifferent language; slower compile
PyMCPyTensor / JAXPythonic; large user base; rich tools (ArviZ)Compile time on first run
NumPyroJAXFast NUTS via JAX; GPU-friendly; cleanest deep-learning interopSmaller community than PyMC
Turing.jlJuliaComposes with Julia's ML stack; flexibleJulia-only
PyroPyTorchStochastic VI on big models; good for BNNs / latent-variableHMC less mature than NumPyro's
Edward2 / TFPTensorFlowScales; ecosystem hooksAPI churn
The slogan. You write the math once. The PPL handles the inference. Switching between HMC and VI is a flag change, not a rewrite.
Step 2

Pseudo-code template

with model:
    theta = Beta("theta", alpha=1, beta=1)        # prior
    y = Binomial("y", n=20, p=theta, observed=14) # likelihood + obs

    trace = sample(2000, chains=4)                 # NUTS
    az.plot_trace(trace)
    az.summary(trace)                              # r-hat, ESS

Three things every PPL gives you:

  1. Declarative model. Variables are named, distributions have priors, observations attach to data.
  2. Inference engine. NUTS / HMC by default; VI / Laplace on demand.
  3. Diagnostics. r-hat (between-chain convergence), ESS (effective sample size), divergent transitions, posterior predictive checks.
Step 3

Live: coin-bias model — HMC vs analytic

The simplest Bayesian model: $\theta \sim \mathrm{Beta}(1, 1)$, $y \sim \mathrm{Bernoulli}(\theta)$. Beta-Binomial conjugacy gives the analytic posterior; an HMC chain should match it. Add data with the buttons and run HMC live.

k / n 0/0 r-hat ESS
Posterior: HMC samples vs analytic Beta
HMC trace plot (chain 1)
Step 4

Live: 2-D Bayesian regression — HMC vs VI

A Bayesian linear regression with a small dataset. HMC gives honest posterior; mean-field VI fits a factorised Gaussian. Watch the credible bands differ: VI is usually too narrow, especially in directions where the parameters correlate.

n
HMC posterior over (slope, intercept)
VI Gaussian fit (mean-field)
The VI trap. Mean-field VI factorises across parameters, so it can't represent correlated posteriors. The HMC contours often slant; the VI contours always align with the axes. For credible-interval-quality work, prefer HMC or full-rank VI.
Step 5

The diagnostic checklist

Step 6

What PPLs are for (and not for)