A course in applied Bayesian statistics, built from real data

Learn Bayesian data analysis the way it is actually done: one real problem at a time.

88 worked examples and 15 challenges, each a complete analysis in PyMC, almost all of public datasets. Every page shows the model, the code, the checks that passed and the ones that did not, and what the posterior means for the question that was asked.

88worked examples, executed and narrated
15challenges with tiered hints and reference solutions
Realpublic datasets, from supernovae to election polls; the few simulated systems are labelled
Everyfailure kept in: divergent chains, miscalibrated intervals, wrong answers

Why model this way

Uncertainty is the result

A Bayesian analysis returns a distribution over everything unknown, $p(\theta \mid y) \propto p(y \mid \theta)\,p(\theta)$. Any quantity you compute from the parameters — a forecast, a ranking, a probability that a threshold is crossed — comes with its uncertainty attached, without a separate derivation.

A model is a story about the data

Instead of choosing a test from a menu, you write down how the data could have been generated: censoring, missingness, measurement error, groups, space, time, physics. The same language covers a regression and a gravitational-wave ringdown, so you learn one way of thinking, not a list of procedures.

Checking is part of the method

Priors are simulated before they are trusted, samplers are diagnosed, and fitted models are asked to reproduce the data. When a model fails, these pages say so and show the repair. Seeing the failures is how you learn to spot them in your own work.

The workflow every page follows

The examples are not a tour of an API. Each one is a complete iteration of the Bayesian workflow of Gelman et al. (2020): a question, a generative model, checks before and after fitting, and a conclusion stated on the scale the question was asked in.

QuestionGenerativemodelPriorpredictiveInferenceDiagnosticsPosteriorpredictiveDecisiona check fails → revise the model, and say so
  1. Question. What decision or claim will the model inform? See E01.
  2. Generative model. A story of how the data came to be, written as distributions. See E01.
  3. Prior predictive. Simulate before fitting: do the priors claim anything absurd? See D03.
  4. Inference. Sample the posterior, or approximate it with care. See E12.
  5. Diagnostics. Did the computation work? Divergences, R-hat, effective sample size. See E02.
  6. Posterior predictive. Can the fitted model reproduce the features of the data you care about? See E03.
  7. Decision. Carry the whole posterior through to the quantity someone acts on. See E33.

Three ways in

New to Bayesian modelling

Read the foundations in order. They assume some Python and some regression, nothing more.

  1. E01 · The Bayesian workflow
  2. E02 · Hierarchical models
  3. E03 · GLMs and model comparison
  4. D03 · An analysis end to end

A practitioner with a problem

Start from your data, not from a method. The guide maps outcome types, dependence structures and questions to the example that treats them.

Choosing a model →

Ready to test yourself

Eleven open-ended problems on real data. The brief says what to deliver, never how; the first model you try will break, and finding out why is the exercise.

The challenges →

Recently added

Chaos and how to fit, forecast and control it; regime detection with hidden Markov models; jump-diffusion SDEs for market crashes; reaction-diffusion in cell biology; optimal placement of heaters and sensors for the heat equation; and a flexible response curve for media models, written in JAX.

E85Market structure from purchase incidence: combining panel, activity and demographic data84.51 Complete Journey (CC0): household x 14 cereals 0/1 for Jan-Jun 2017 (2,391 households), shopping trips, demographics for a third, Jul-Dec as a test waveco-purchase lift confounded by volume, Bernoulli-logit factor model ladder (activity, category propensity, centred-loading choice map), a naive map whose first axis is volume, …E84Market structure analysis: who competes with whom?Allenby & Rossi (1991) margarine scanner panel (bayesm): 516 households, 4,470 purchases of 10 products with all shelf pricesswitching matrix and switching lift, IIA (logit diversion proportional to share), nested-logit tests of form vs brand vs tier hierarchies scored on held-out last purchases, factor-structured hierarchical logit as a choice map, …E83Bayesian quantile regression, and when it lies20,000 US singleton births 2023 (NCHS natality public-use file) + a known-truth simulationcheck loss and a Frisch-Newton LP solver, the quantile process, the asymmetric Laplace working likelihood (mode right, spread wrong, units trap), coverage simulation, Yang-Wang-He sandwich, Gibbs posterior with a bootstrap-calibrated learning rate, …E82Bayesian optimisation: choosing the next experimentShields et al. 2021 direct-arylation screen: all 1,728 reactions (12 ligands x 4 bases x 4 solvents x 3 conc x 3 temp), plus 50 chemists' campaigns and EDBO runs (MIT)GP over categorical + continuous factors (per-factor correlation kernels, main effects + interactions), MAP refits of a compiled PyMC model vs NUTS, EI/PI/UCB/Thompson (Kronecker + Matheron draws), paired benchmark vs random/OFAT/chemists, encoding failure, …E81Circular data: directions, times of day and seasonsSanta Barbara airport hourly wind 2021-23 (IEM ASOS); Barro Colorado camera traps (Rowcliffe et al. 2014, CC BY); Oklahoma quakes as a nullmean resultant vector, von Mises / wrapped / projected normal, tan-half vs canonical (natural-parameter) link, wrapped-prior and step-mode failures with zero divergences, harmonic circular predictors, a regime mixture, circular PPC (rose bands, PIT), …E80A gravitational lens, pixel by pixelHST/ACS image of the Einstein ring SDSS J1627-0053 (MAST), SLACS published modelspixel-level forward model (SIE + shear, Sérsic lens light, FFT PSF from field stars), drizzle-correlated noise whitened in Fourier space, linear amplitudes integrated out, staged vs random starts, wrap_jax + nutpie low-rank, …E79CMB field-level inference: the sky behind the LMCPlanck 2018 SMICA map patches (IRSA mirror of the Planck Legacy Archive), half-mission noise, common mask, best-fit TT spectrumGaussian random fields on a flat-sky patch, naive masked-power bias checked on simulations, joint sky + bandpower posterior in PyMC (Hartley basis), centred / non-centred / partial non-centring at low and high signal-to-noise, why a mask hole defeats HMC, …

All 88 examples →