Practice · Baseball · Simulated for practice

Hot-start hitters

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?

The data

19 rows, 6 columns.

namestring
Invented hitter name
at_batsinteger · AB
At-bats in the hot start (always 45)
hitsinteger · H
Hits in those 45 at-bats
remaining_at_batsinteger · AB
At-bats over the rest of the season
remaining_hitsinteger · H
Hits over the rest of the season
teamstring
Invented team name

Simulated 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.

Reference chart

The chart, and the code that draws it.

Edit the code and press Run to recompute it in your browser.

This chart is the code's result. Run recomputes it in your browser.
Each hitter's posterior batting average after 45 at-bats, shaded to the 90% credible interval; the diamond is what they hit the rest of the season.

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.

Starter code

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()
Three questions

Easy, medium, hard.

Try each before opening the hint; open the worked solution only after your own attempt.

  1. Easy

    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?

    Hint

    Two ratios with assign, then sort by the first-45 average and compare the two columns for the top of the list.

    Worked solution
    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.
  2. Medium

    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?

    Hint

    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.

    Worked solution
    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
  3. Hard

    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.

    Hint

    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.

    Worked solution
    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
Extension

Bring in an AI agent.

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.

Where the method is taught

Hierarchical Modeling using PyMC in Baseball. Every dataset in the practice library names its session the same way.

Keep up

One email when something new arrives.

A short welcome note, a few session picks over two weeks, then only the updates you signed up for. Reply stop any time.