Practice · Baseball · Simulated for practice

Pitch speeds by type

Six thousand invented pitches, 1,500 of each type, with release speed. A clean dataset for the full model-checking loop: fit, simulate, compare, fix.

The data

6,000 rows, 2 columns.

pitch_typestring
FF four-seam, SL slider, CH changeup, CU curveball
release_speednumber · mph
Speed out of the hand

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.

Python
This chart is the code's result. Run recomputes it in your browser.
Posterior for each pitch type's average speed after twenty pitches, shaded to the 90% interval; the diamond is the average over all 1,500.

Edit me: Move the prior sd, or change first_n to 200 and run: the data overwhelms the prior.

Data: Pitch speeds by type (pitches_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.

Starter code

import pandas as pd

pitches = pd.read_csv("data/pitches_2026.csv")
pitches.groupby("pitch_type").release_speed.describe().round(1)
Three questions

Easy, medium, hard.

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

  1. Easy

    Report the mean and spread of release speed for each pitch type, and the gap between the fastest and slowest type.

    Hint

    One groupby with agg for mean and std; the gap is the difference of the extremes of the means.

    Worked solution
    import pandas as pd
    
    p = pd.read_csv("data/pitches_2026.csv")
    s = p.groupby("pitch_type").release_speed.agg(["mean", "std"]).round(2).sort_values("mean")
    print(s.to_string())
    print("gap between fastest and slowest type:", round(s["mean"].max() - s["mean"].min(), 1), "mph")

    Output, from running this code on the dataset when the page was built

    mean   std
    pitch_type             
    CU          79.43  2.79
    CH          85.02  2.47
    SL          85.58  2.40
    FF          93.97  1.93
    gap between fastest and slowest type: 14.5 mph
  2. Medium

    Using only the first 20 pitches of each type, compute the posterior for each type's mean speed under a normal prior centered at 88 mph with sd 2. How far is each posterior from the mean of all 1,500?

    Hint

    Normal prior, normal data with the sample sd plugged in: the posterior precision is the sum of the precisions, and the posterior mean is the precision-weighted average.

    Worked solution
    import numpy as np
    import pandas as pd
    
    p = pd.read_csv("data/pitches_2026.csv")
    truth = p.groupby("pitch_type").release_speed.mean()
    first = p.groupby("pitch_type").head(20).groupby("pitch_type").release_speed.agg(["mean", "std", "size"])
    prec = 1 / 2.0**2 + first["size"] / first["std"] ** 2
    post = (88 / 2.0**2 + first["size"] * first["mean"] / first["std"] ** 2) / prec
    print(pd.DataFrame({"posterior": post, "sd": 1 / np.sqrt(prec), "all 1,500": truth,
                        "miss": post - truth}).round(2).to_string())

    Output, from running this code on the dataset when the page was built

    posterior    sd  all 1,500  miss
    pitch_type                                  
    CH              85.87  0.52      85.02  0.85
    CU              80.45  0.80      79.43  1.03
    FF              94.17  0.35      93.97  0.20
    SL              86.57  0.33      85.58  1.00
  3. Hard

    Fit a single normal to all 6,000 speeds, ignoring pitch type, and run a posterior predictive check: simulate 200 replicated datasets and compare the share of pitches between 82 and 88 mph with the real share. What does the check say, and what fixes it?

    Hint

    The single normal puts most of its mass in the middle; the real data has a valley there between breaking balls and fastballs. A mixture by pitch type is the fix; check it the same way.

    Worked solution
    import numpy as np
    import pandas as pd
    
    p = pd.read_csv("data/pitches_2026.csv")
    v = p.release_speed.to_numpy()
    stat = lambda a: np.mean((a > 82) & (a < 88))
    rng = np.random.default_rng(5)
    single = [stat(rng.normal(v.mean(), v.std(), v.size)) for _ in range(200)]
    g = p.groupby("pitch_type").release_speed.agg(["mean", "std", "size"])
    mixture = [stat(np.concatenate([rng.normal(r["mean"], r["std"], int(r["size"])) for _, r in g.iterrows()])) for _ in range(200)]
    print(f"real share between 82 and 88 mph   {stat(v):.3f}")
    print(f"single normal, 90% of replicates   {np.percentile(single, 5):.3f} to {np.percentile(single, 95):.3f}")
    print(f"mixture by type, 90% of replicates {np.percentile(mixture, 5):.3f} to {np.percentile(mixture, 95):.3f}")

    Output, from running this code on the dataset when the page was built

    real share between 82 and 88 mph   0.431
    single normal, 90% of replicates   0.382 to 0.405
    mixture by type, 90% of replicates 0.426 to 0.441
Extension

Bring in an AI agent.

Give an agent the hard question's output and ask it to choose between the two models and justify the choice in two sentences. Then ask it to propose one more test statistic that would catch a different failure of the single normal, and run it yourself before accepting the answer.

Where the method is taught

MCMC in Practice. 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.