Session · 90 min · Available now · Modeling & Inference

SECONDS, NOT HOURS

Variational Inference in Sports

Fit Bayesian models across a whole roster with variational inference, and know when to go back to sampling.

An agent can write the model. This session is about knowing when it is wrong.

$79 Buy now Buy once, watch any time. A person answers if it is the wrong one for you.
What you learn

The skills, and the artifact you leave with.

  • Fit a model with ADVI in PyMC in seconds where sampling took minutes
  • Check where the approximation holds and where it understates the spread
  • Compare the variational posterior against a sampled one on the quantities that matter
  • Decide, with a rule you can state, when speed is worth it and when it is not
You leave with

The same roster model fit both ways, side by side, with the comparison that tells you which to trust for which question.

Why it matters now

Rosters, leagues and seasons scale faster than samplers do; the analyst who can trade speed for accuracy on purpose gets answers the same day.

Pythonagents/viz/S4.py
2026-11-09T02:12:16.945372 image/svg+xml Matplotlib v3.10.8, https://matplotlib.org/ 0 50 100 150 200 250 300 350 400 optimisation step −750 −700 −650 −600 −550 −500 ELBO (10-draw estimate) The ELBO climbs and flattens: the approximation is as close as its family allows (slope ≈ 0.78)
Mean-field variational inference on the batted-ball regression, in numpy: the ELBO climbing as the approximation fits. Drawn at build from the data on this site by the code below.
The code that drew it
# Mean-field variational inference on the batted-ball regression, in numpy: the ELBO climbing as the approximation fits.
import sys, pathlib, csv, math
sys.path.insert(0, str(pathlib.Path(__file__).parent))
from _palette import plt, emit, GOLD, TEAL, MUTE
import numpy as np

rows = list(csv.DictReader(open("site/18-practice/batted-balls/files/batted_balls_2026.csv")))
x = np.array([float(r["launch_speed"]) for r in rows]); y = np.array([float(r["hit_distance"]) for r in rows])
x = (x - x.mean()) / x.std(); y = (y - y.mean()) / y.std()
rng = np.random.default_rng(7)
# q(alpha, beta, log sigma) = independent Gaussians; the ELBO estimated with ten draws per step, optimised with Adam
mu = np.zeros(3); rho = np.full(3, -2.0); m1 = np.zeros(6); m2 = np.zeros(6); lr, b1, b2 = 0.02, 0.9, 0.999
def logp(th):
    a, b, ls = th[:, 0], th[:, 1], th[:, 2]; s = np.exp(ls)
    ll = -0.5 * (((y[None, :] - a[:, None] - b[:, None] * x[None, :]) / s[:, None]) ** 2).sum(1) - len(y) * (ls + 0.5 * math.log(2 * math.pi))
    return ll - 0.5 * (a ** 2 + b ** 2) / 10 - 0.5 * ls ** 2
elbo = []
for t in range(1, 401):
    sd = np.log1p(np.exp(rho)); eps = rng.standard_normal((10, 3)); th = mu + sd * eps
    lp = logp(th); ent = (np.log(sd) + 0.5 * (1 + math.log(2 * math.pi))).sum()
    elbo.append(lp.mean() + ent)
    # pathwise gradients
    e = 1e-3; g = np.zeros(6)
    for j in range(3):
        for k, (vec, idx) in enumerate(((mu, j), (rho, 3 + j))):
            vec[j] += e; sd2 = np.log1p(np.exp(rho)); lp2 = logp(mu + sd2 * eps).mean() + (np.log(sd2) + 0.5 * (1 + math.log(2 * math.pi))).sum()
            vec[j] -= e; g[idx] = (lp2 - elbo[-1]) / e
    m1 = b1 * m1 + (1 - b1) * g; m2 = b2 * m2 + (1 - b2) * g ** 2
    step = lr * (m1 / (1 - b1 ** t)) / (np.sqrt(m2 / (1 - b2 ** t)) + 1e-8)
    mu += step[:3]; rho += step[3:]
fig, ax = plt.subplots(figsize=(7.2, 3.8))
ax.plot(elbo, color=GOLD, lw=1.6)
ax.axhline(max(elbo), color=MUTE, lw=1, ls="--")
ax.set_xlabel("optimisation step"); ax.set_ylabel("ELBO (10-draw estimate)")
ax.set_title(f"The ELBO climbs and flattens: the approximation is as close as its family allows  (slope ≈ {mu[1]:.2f})")
emit(fig)

Data: Batted balls. Simulated for practice by AthlyticZ (agents/sim_data.py, seed 20261109). License CC BY 4.0.

The problem

Sampling a Bayesian model across a whole roster, league or season takes minutes to hours, and the answer is needed the same day.

Three decisions it teaches
  1. When variational inference is the right trade: speed for an approximation you can check
  2. How to check where the approximation holds and where it understates the spread
  3. When to go back to sampling, with a rule you can state before you fit

The artifact. The same roster model fit both ways, side by side, with the comparison that tells you which to trust for which question.

Curriculum
  1. Variational Inference in Sports

    One live session, recorded, 90 minutes. Yours to keep and watch any time.

What you get

Everything that exists for this session, and nothing that does not.

  • The recording90 minutes, yours to keep
Play with the data

Run the code, in your browser.

The chart on this page, drawn by code you can edit. R runs on webR and Python on Pyodide; nothing loads until you press Run, and nothing you type leaves the page.

Python
This chart is the code's result. Run recomputes it in your browser.
The evidence lower bound as variational inference fits distance against exit velocity: noisy draws and their running mean.

Edit me: Move the learning rate, or set steps to 1000 and run again. Where does it settle?

Data: Batted balls (batted_balls_2026.csv). Simulated for practice by AthlyticZ (agents/sim_data.py, seed 20261109). License CC BY 4.0.

The code runs in your browser on Pyodide; nothing you type leaves the page.

Curriculum

What is covered, in order.

From the academy course record, as it is taught.

  1. Variational Inference in Sports

    One live session, recorded, 90 minutes. Yours to keep and watch any time.

Chris Fonnesbeck
Your instructor Chris Fonnesbeck

PhD · Biostatistics @ Vanderbilt University Medical Center

Teaches the method the way it is used in the role above. The team page lists everything they teach here.

Also teaches MCMC in Practice, Hierarchical Modeling using PyMC in Baseball.

What is included

What is yours when you buy.

  • The full recording, 75 min
  • Yours to keep: watch it any time

Refunds. If you bought it and it is the wrong one for you, write to michael@athlyticz.com and a person will answer.

Questions

The ones that stop a purchase.

Is this too advanced, or too basic, for me?

Built for someone who already writes code in the session's language and wants the method, not the syntax. If the outcome line at the top reads like something you could use next week, it is pitched right. If it reads like a different job, start with the courses on /browse.

What exactly do I get, and for how long?

The full recording, yours to keep: buy once, watch any time. Masterclass subscribers get every live session for a year and can watch every recorded session for as long as they are a member. Everyone else can buy a recording on its own.

Do I need to install anything?

No. The recording plays in the browser.

What if it is not for me?

Twenty minutes on a call before you buy costs nothing, and the call is the fastest way to tell. If you bought it and it is the wrong one for you, email michael@athlyticz.com and a person will answer.

Want more?

Ten live hours of end-to-end project work every month.

The Masterclass is all of it, live and recorded, for $1,899 a year, or $199 a month. Paid by the month, it renews monthly at $199, cancel any time by replying to your receipt.

This session $79

Variational Inference in Sports. The recording, yours to keep.

Buy now
The Masterclass $1,899 a year, or $199 a month

Every live session this year, and every recording.

Join the Masterclass
Membership $2,999 a year

The whole catalog of courses plus the Masterclass.

Get Membership
$79Buy now