Practice · Baseball · Simulated for practice
Nineteen invented hitters after their first 45 at-bats of a season, and what they hit over the rest of it. The question every front office asks in April: which hot starts are real?
namestringat_batsinteger · ABhitsinteger · Hremaining_at_batsinteger · ABremaining_hitsinteger · HteamstringSimulated for practice. Simulated for practice by AthlyticZ (agents/sim_data.py, seed 20261109). License CC BY 4.0. Generated from a documented model with realistic distributions; every name is invented.
Edit the code and press Run to recompute it in your browser.
Edit me: Move the prior strength, or edit prior_strength and run: watch the hot starts pull toward the group.
Data: Hot-start hitters (batting_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 webR or Pyodide; nothing you type leaves the page.
import pandas as pd
hitters = pd.read_csv("data/batting_2026.csv")
hitters.assign(avg=hitters["hits"] / hitters["at_bats"]).sort_values("avg", ascending=False).head()
Try each before opening the hint; open the worked solution only after your own attempt.
Compute each hitter's average over the first 45 at-bats and over the rest of the season. Which three hot starts held up best?
Two ratios with assign, then sort by the first-45 average and compare the two columns for the top of the list.
import pandas as pd
h = (pd.read_csv("data/batting_2026.csv")
.assign(start=lambda d: d.hits / d.at_bats,
rest=lambda d: d.remaining_hits / d.remaining_at_bats,
held=lambda d: d.rest - d.start))
top = h.nlargest(5, "start")[["name", "start", "rest", "held"]].round(3)
print(top.to_string(index=False))
print("\nThe hottest starts fell by", round(-top.held.mean(), 3), "on average.")
Output, from running this code on the dataset when the page was built
name start rest held
Reid Ivanescu 0.378 0.296 -0.082
Owen Brannagh 0.356 0.268 -0.088
Wes Quillon 0.356 0.241 -0.115
Mateo Dunmarrow 0.356 0.288 -0.067
Dante Quintavalle 0.311 0.326 0.015
The hottest starts fell by 0.067 on average.Shrink each start toward the group with an empirical-Bayes Beta prior. Does the shrunk estimate predict the rest of the season better than the raw start?
The spread of raw averages is talent plus binomial noise. Subtract the noise, mean of p(1-p)/45, to get the talent variance, then solve for the Beta parameters.
import numpy as np
import pandas as pd
h = pd.read_csv("data/batting_2026.csv").assign(
start=lambda d: d.hits / d.at_bats, rest=lambda d: d.remaining_hits / d.remaining_at_bats)
m = h.start.mean()
noise = (h.start * (1 - h.start) / h.at_bats).mean()
talent_var = max(h.start.var() - noise, 1e-5)
k = m * (1 - m) / talent_var - 1 # prior strength, in at-bats
h["shrunk"] = (h.hits + m * k) / (h.at_bats + k)
mse = lambda col: ((h[col] - h.rest) ** 2).mean()
print(f"prior mean {m:.3f}, prior worth {k:.0f} at-bats")
print(f"error of the raw start {mse('start'):.5f}")
print(f"error of the shrunk start {mse('shrunk'):.5f}")
Output, from running this code on the dataset when the page was built
prior mean 0.249, prior worth 84 at-bats error of the raw start 0.00583 error of the shrunk start 0.00152
Fit the hierarchical Beta-binomial properly: put a grid over the league mean and the prior strength, compute the marginal likelihood of all nineteen starts, and report the posterior for the prior strength.
The marginal likelihood of one hitter is a ratio of Beta functions; scipy.special.betaln keeps it on the log scale. Sum over hitters, add a flat prior on the grid, normalize.
import numpy as np
import pandas as pd
from scipy.special import betaln
h = pd.read_csv("data/batting_2026.csv")
mu = np.linspace(0.20, 0.31, 111)[:, None]
kappa = np.geomspace(5, 2000, 160)[None, :]
a, b = mu * kappa, (1 - mu) * kappa
loglik = sum(betaln(a + y, b + n - y) - betaln(a, b) for y, n in zip(h.hits, h.at_bats))
post = np.exp(loglik - loglik.max()); post /= post.sum()
k_post = post.sum(axis=0)
cdf = np.cumsum(k_post)
lo, hi = kappa[0, np.searchsorted(cdf, 0.05)], kappa[0, np.searchsorted(cdf, 0.95)]
print(f"posterior mean league average {float((post.sum(axis=1) * mu[:, 0]).sum()):.3f}")
print(f"prior strength: median {kappa[0, np.searchsorted(cdf, 0.5)]:.0f} at-bats, 90% interval {lo:.0f} to {hi:.0f}")
Output, from running this code on the dataset when the page was built
posterior mean league average 0.250 prior strength: median 160 at-bats, 90% interval 32 to 1479
Ask a coding agent to write the same model in PyMC with a logit-normal population, then check its work against yours: the posterior league average should land within .005 of the grid answer, the sampler should report no divergences and r-hat under 1.01, and the agent should explain in one sentence why nineteen hitters leave the prior strength so uncertain.
Hierarchical Modeling using PyMC in Baseball. Every dataset in the practice library names its session the same way.
A short welcome note, a few session picks over two weeks, then only the updates you signed up for. Reply stop any time.
Loading the calendar.
The calendar did not load here. Open it on Calendly.
Confirmed
Calendly has sent the invitation to your inbox, with the link for the call. Michael will see you then.
We've teamed up with AthlyticZ to supercharge how we use data science and statistics to impact our sports. More clarity in the numbers means bigger results for our athletes.
AthlyticZ has completely transformed the learning approach to data science through the use of sports-based problems. The instructors are the best of the best, and the practical projects have immediate impact to students.
The sports industry moves fast. Managing the pace of evolving technology, data, and AI isn't optional - it's imperative. Our Analytics team was looking for a partner to meet us where we are, to help us in the continued investment of our people and capabilities without pulling us away from the work that matters. AthlyticZ delivers exactly that: a structured, practitioner-led platform that fits how modern sports analytics groups actually operate.