NOTE: Separate codes by "cell" # ============================================================================================= # Cell 0: Flag position-player pitching outings using MLB's listed position, not speed alone. # # 1. Candidates: pitcher-seasons with raw BF-weighted season FBv < SLOW_MPH, or <= FEW_APPS # appearances (only these need a lookup). # 2. FanGraphs id -> MLBAM id via the Chadwick register (pybaseball.playerid_reverse_lookup). # 3. MLBAM id -> primaryPosition from the MLB Stats API. # 4. A pitcher-season is a position player if: listed position is not P / TWP (two-way) # AND (season FBv < SLOW_MPH or appearances <= FEW_APPS). # Players the register can't match fall back to the old rule (season FBv < 80). # # Output: position_flags.csv (playerid, season, apps, season_fbv, mlbam, position, rule, drop) # prepare() reads it if present (see the patch at the bottom of this message). # # Needs internet: pip install pybaseball requests # Note: primaryPosition is the player's position now, not by season. A position player who later # converted to pitching is listed P and kept; there are only a handful of such cases. # ============================================================================================= import os import numpy as np import pandas as pd import requests DATA_DIR = r"[your drive partition]" INPUT = os.path.join(DATA_DIR, "pitcher_game_logs_2002_2026.csv") OUT = os.path.join(DATA_DIR, "position_flags.csv") FIRST, LAST = 2008, 2026 SLOW_MPH, FEW_APPS, OLD_RULE_MPH = 85.0, 10, 80.0 PITCHER_CODES = {"P", "TWP"} # Manual overrides by FanGraphs playerid, for players whose listed position changed later. # Brett Phillips (14735) pitched as a position player in 2021-23, then converted to pitching. FORCE_DROP = {14735} def candidates(path): d = pd.read_csv(path, usecols=["playerid", "PlayerName", "season", "TBF", "FBv"], low_memory=False) d = d[(d.season >= FIRST) & (d.season <= LAST) & (d.TBF > 0) & d.FBv.notna()] ps = (d.groupby(["playerid", "season"]) .apply(lambda g: pd.Series({"name": g.PlayerName.iloc[0], "apps": len(g), "BF": g.TBF.sum(), "season_fbv": np.average(g.FBv, weights=g.TBF)}), include_groups=False) .reset_index()) return ps[(ps.season_fbv < SLOW_MPH) | (ps.apps <= FEW_APPS)].copy() def fangraphs_to_mlbam(fg_ids): from pybaseball import playerid_reverse_lookup look = playerid_reverse_lookup([int(i) for i in fg_ids], key_type="fangraphs") return (look[["key_fangraphs", "key_mlbam"]].dropna() .astype({"key_fangraphs": int, "key_mlbam": int}) .drop_duplicates("key_fangraphs") .set_index("key_fangraphs").key_mlbam) def mlb_positions(mlbam_ids, batch=100): out = {} ids = sorted(set(int(i) for i in mlbam_ids)) for k in range(0, len(ids), batch): chunk = ids[k:k + batch] r = requests.get("https://statsapi.mlb.com/api/v1/people", params={"personIds": ",".join(map(str, chunk))}, timeout=60) r.raise_for_status() for p in r.json().get("people", []): out[p["id"]] = p.get("primaryPosition", {}).get("abbreviation") return pd.Series(out, name="position") def classify(ps, mlbam, pos): ps = ps.copy() ps["mlbam"] = ps.playerid.map(mlbam) ps["position"] = ps.mlbam.map(pos) known = ps.position.notna() slow_or_few = (ps.season_fbv < SLOW_MPH) | (ps.apps <= FEW_APPS) ps["rule"] = np.where(known, "listed position", "fallback: speed < 80") ps["drop"] = np.where(known, ~ps.position.isin(PITCHER_CODES) & slow_or_few, ps.season_fbv < OLD_RULE_MPH) force = ps.playerid.isin(FORCE_DROP) ps.loc[force, "drop"] = True ps.loc[force, "rule"] = "manual override" return ps if __name__ == "__main__" or True: # also runs as a notebook cell ps = candidates(INPUT) print(f"Candidate pitcher-seasons: {len(ps):,} ({ps.playerid.nunique():,} players)") mlbam = fangraphs_to_mlbam(ps.playerid.unique()) print(f"Matched to MLBAM ids: {ps.playerid.isin(mlbam.index).mean():.1%} of candidate rows") pos = mlb_positions(mlbam.values) flags = classify(ps, mlbam, pos) flags.to_csv(OUT, index=False) old = flags.season_fbv < OLD_RULE_MPH print(f"\nFlagged as position players: {int(flags['drop'].sum())} pitcher-seasons, " f"{int(flags.loc[flags['drop'], 'apps'].sum())} outings") print("\nKept now, dropped by the old < 80 mph rule (real pitchers):") print(flags[old & ~flags['drop']][["name", "season", "apps", "season_fbv", "position"]] .round(1).to_string(index=False)) print("\nDropped now, kept by the old rule (position players throwing 80+):") print(flags[~old & flags['drop']][["name", "season", "apps", "season_fbv", "position"]] .round(1).to_string(index=False)) print("\nBy season (outings dropped):") print(flags[flags['drop']].groupby("season").apps.sum().to_string()) print(f"\nSaved {OUT}") # In[15]: # ===================== Cell 1 binomial logit model ===================== """ Fastball velocity and strikeouts: plain binomial logit (no pitcher random intercept), fitted by maximum likelihood (Newton-Raphson), 2008-2026. Equivalent Stata model: logit SO ib2008.season##(c.FBv_c c.FBv_season_c) [i.GS], binomial(TBF) vce(cluster playerid) (i.e., glm SO ..., family(binomial TBF) link(logit) vce(cluster playerid)) Data preparation: identical to the random-intercept model cell (prepare() below), including position players dropped by listed position when position_flags.csv is present. Standard errors: pitcher-clustered sandwich, G/(G-1) small-sample factor, as in Stata's vce(cluster). Outputs go to fbv_jax_results with the tag ..._plain (estimates, coefficients, Wald tests, fixed-velocity predictions, plot). """ import argparse import json import os import time import numpy as np import pandas as pd from scipy import stats # ---------------------------------------------------------------------------- # Data preparation # ---------------------------------------------------------------------------- def wmean_by(values, weights, keys): """TBF-weighted mean of values within groups given by integer keys.""" num = np.bincount(keys, weights=values * weights) den = np.bincount(keys, weights=weights) return (num / den)[keys] def park_season_offsets(fbv, w, ps_key, pk_key, season_of_pk, tol=1e-8, max_iter=5000): """Two-way FE by alternating projections: fbv = a[ps] + b[park-season] + e. Returns b (one per park-season), centred to weighted mean 0 in each season.""" b = np.zeros(pk_key.max() + 1) diff = np.inf for it in range(max_iter): a_row = wmean_by(fbv - b[pk_key], w, ps_key) b_new = np.bincount(pk_key, weights=(fbv - a_row) * w) / np.bincount(pk_key, weights=w) diff = np.max(np.abs(b_new - b)) b = b_new if diff < tol: break # centre within season (the season level is not identified separately # from the pitcher-season effects, so fix it at zero) pk_w = np.bincount(pk_key, weights=w) seasons = np.unique(season_of_pk) for s in seasons: m = season_of_pk == s b[m] -= np.average(b[m], weights=pk_w[m]) return b, it + 1, diff def prepare(path, first, last, pre2017_adjust, min_season_fbv, log): d = pd.read_csv(path, low_memory=False) need = ["playerid", "season", "gamedate", "dh", "teamid", "Team", "Opp", "HomeAway", "GS", "SO", "TBF", "FBv"] d = d[need].copy() d = d[(d.season >= first) & (d.season <= last)] n0 = len(d) # spurious row: Diekman was with KCR until late July 2019 bad = (d.playerid == 5003) & (d.gamedate == "2019-05-19") & (d.teamid == 10) log(f"Dropped spurious Diekman OAK row: {int(bad.sum())}") d = d[~bad] dups = d.duplicated(["playerid", "gamedate", "dh"], keep=False).sum() log(f"Remaining duplicate pitcher-date-game rows: {dups}") assert ((d.SO >= 0) & (d.SO == np.floor(d.SO))).all() or d.SO.isna().any() assert (d.SO.dropna() <= d.TBF[d.SO.notna()]).all() miss = d[["SO", "TBF", "FBv"]].isna().any(axis=1) log(f"Rows missing a model variable: {int(miss.sum()):,}") d = d[~miss] log(f"Rows with zero BF: {int((d.TBF == 0).sum()):,}") d = d[d.TBF > 0].copy() # position players: listed position from position_flags.csv if present (see # position_flags.py); otherwise raw TBF-weighted pitcher-season FBv < min_season_fbv ps_key = d.groupby(["playerid", "season"]).ngroup().to_numpy() flags_path = os.path.join(os.path.dirname(path), "position_flags.csv") if os.path.exists(flags_path): fl = pd.read_csv(flags_path) fl = fl[fl["drop"].astype(str).str.lower() == "true"] keys = pd.MultiIndex.from_frame(d[["playerid", "season"]]) pos = np.asarray(keys.isin(pd.MultiIndex.from_frame(fl[["playerid", "season"]]))) how = "listed position (position_flags.csv)" else: raw_mean = wmean_by(d.FBv.to_numpy(), d.TBF.to_numpy(), ps_key) pos = raw_mean < min_season_fbv how = f"season FBv < {min_season_fbv} mph" log(f"Position-player pitcher-seasons dropped ({how}): " f"{len(np.unique(ps_key[pos])):,} ({int(pos.sum()):,} outings)") d = d[~pos].copy() # PITCHf/x -> Statcast level shift d["FBv_raw"] = d.FBv d.loc[d.season < 2017, "FBv"] += pre2017_adjust # park x season calibration d["park"] = np.where(d.HomeAway == "H", d.Team, d.Opp.str.lstrip("@")) ps_key = d.groupby(["playerid", "season"]).ngroup().to_numpy() pk_codes = d.groupby(["park", "season"]).ngroup().to_numpy() season_of_pk = d.groupby(["park", "season"]).season.first().to_numpy() b, iters, pdiff = park_season_offsets(d.FBv.to_numpy(), d.TBF.to_numpy(), ps_key, pk_codes, season_of_pk) d["park_offset"] = b[pk_codes] d["FBv"] = d.FBv - d.park_offset pk = pd.DataFrame({"season": season_of_pk, "offset": b}) sd = pk.groupby("season").offset.std() log(f"Park-season offsets ({iters} iterations, last change {pdiff:.1e} mph); SD by season: " + ", ".join(f"{s}:{v:.2f}" for s, v in sd.items())) # TBF-weighted season mean of corrected game FBv d["FBv_season"] = wmean_by(d.FBv.to_numpy(), d.TBF.to_numpy(), ps_key) chk = np.bincount(ps_key, weights=(d.FBv - d.FBv_season) * d.TBF) assert np.max(np.abs(chk)) < 1e-6 for s in range(first, last + 1): assert (d.season == s).any(), f"no outings in {s}" d = d.sort_values(["playerid", "season", "gamedate", "dh"]).reset_index(drop=True) log(f"Included outings: {len(d):,}; excluded within years: {n0 - len(d):,}; " f"pitchers: {d.playerid.nunique():,}") return d, pk # ---------------------------------------------------------------------------- # Design matrix # ---------------------------------------------------------------------------- def build_design(d, first, last, ref_game, ref_season, interact_season_mean, role): seasons = list(range(first, last + 1)) fc = d.FBv.to_numpy() - ref_game sc = d.FBv_season.to_numpy() - ref_season cols, names = [np.ones(len(d))], ["_cons"] S = {s: (d.season.to_numpy() == s).astype(float) for s in seasons} for s in seasons[1:]: cols.append(S[s]); names.append(f"{s}.season") cols.append(fc); names.append("FBv_c") for s in seasons[1:]: cols.append(S[s] * fc); names.append(f"{s}.season#c.FBv_c") cols.append(sc); names.append("FBv_season_c") if interact_season_mean: for s in seasons[1:]: cols.append(S[s] * sc); names.append(f"{s}.season#c.FBv_season_c") if role == "gs": cols.append(d.GS.to_numpy().astype(float)); names.append("1.GS") return np.column_stack(cols), names def design_row(names, season, first, fbv_c=0.0, fbv_season_c=0.0, gs=0.0): x = np.zeros(len(names)) for j, n in enumerate(names): if n == "_cons": x[j] = 1 elif n == f"{season}.season": x[j] = 1 elif n == "FBv_c": x[j] = fbv_c elif n == f"{season}.season#c.FBv_c": x[j] = fbv_c elif n == "FBv_season_c": x[j] = fbv_season_c elif n == f"{season}.season#c.FBv_season_c": x[j] = fbv_season_c elif n == "1.GS": x[j] = gs return x # ---------------------------------------------------------------------------- # Plain binomial logit: Newton-Raphson + pitcher-clustered sandwich # ---------------------------------------------------------------------------- def fit_plain(X, y, n, cid, G, log, max_iter=50, tol=1e-10): beta = np.zeros(X.shape[1]); converged = False for it in range(1, max_iter + 1): p = 1 / (1 + np.exp(-(X @ beta))) H = (X * (n * p * (1 - p))[:, None]).T @ X step = np.linalg.solve(H, X.T @ (y - n * p)) beta += step log(f" iter {it:3d} max |step| {np.max(np.abs(step)):.2e}") if np.max(np.abs(step)) < tol: converged = True break p = 1 / (1 + np.exp(-(X @ beta))) H = (X * (n * p * (1 - p))[:, None]).T @ X ll = float(np.sum(y * np.log(p) + (n - y) * np.log1p(-p))) S = np.zeros((G, X.shape[1])); np.add.at(S, cid, X * (y - n * p)[:, None]) Hinv = np.linalg.inv(H) V = (G / (G - 1)) * Hinv @ (S.T @ S) @ Hinv return beta, ll, Hinv, V, converged, it # ---------------------------------------------------------------------------- # Main # ---------------------------------------------------------------------------- def main(): ap = argparse.ArgumentParser() # ---- Data folder (edit if the files live elsewhere on this machine) ---- DATA_DIR = r"[your drive partition]" ap.add_argument("--input", default=os.path.join(DATA_DIR, "pitcher_game_logs_2002_2026.csv")) ap.add_argument("--out", default=os.path.join(DATA_DIR, "fbv_jax_results")) ap.add_argument("--first", type=int, default=2008) ap.add_argument("--last", type=int, default=2026) ap.add_argument("--ref-game", type=float, default=92.0) ap.add_argument("--ref-season", type=float, default=92.0) ap.add_argument("--interact-season-mean", type=int, default=1, choices=[0, 1]) ap.add_argument("--role", default="none", choices=["none", "gs"]) ap.add_argument("--pre2017-adjust", type=float, default=0.5) ap.add_argument("--min-season-fbv", type=float, default=80.0) a, _ = ap.parse_known_args() # ignores the extra arguments Jupyter passes os.makedirs(a.out, exist_ok=True) tag = f"{a.first}_{a.last}_role-{a.role}_ism{a.interact_season_mean}_plain" logf = open(os.path.join(a.out, f"log_{tag}.txt"), "w") def log(msg): print(msg, flush=True); logf.write(msg + "\n"); logf.flush() log(f"Input: {a.input}\nSettings: {vars(a)}") d, pk = prepare(a.input, a.first, a.last, a.pre2017_adjust, a.min_season_fbv, log) pk.to_csv(os.path.join(a.out, "park_season_offsets.csv"), index=False) X, names = build_design(d, a.first, a.last, a.ref_game, a.ref_season, a.interact_season_mean, a.role) y = d.SO.to_numpy(float); n = d.TBF.to_numpy(float) cid = d.groupby("playerid").ngroup().to_numpy() G = int(cid.max() + 1) log(f"N = {len(d):,}; clusters G = {G:,}; parameters = {len(names)}; no random intercept") t0 = time.time() est, ll, V_oim, V, conv, iters = fit_plain(X, y, n, cid, G, log) from scipy.special import gammaln ll_full = ll + float(np.sum(gammaln(n + 1) - gammaln(y + 1) - gammaln(n - y + 1))) log(f"Converged: {conv} after {iters} iterations, {time.time() - t0:.1f} s; " f"log likelihood = {ll_full:,.4f}") P = len(names) np.savez(os.path.join(a.out, f"estimates_{tag}.npz"), beta=est, V=V, V_oim=V_oim, names=np.array(names), ref_game=a.ref_game, ref_season=a.ref_season, first=a.first, last=a.last) np.savez(os.path.join(a.out, "plain_logit_estimates.npz"), beta=est, V=V, names=np.array(names)) rows = [] for j, nm in enumerate(names): se = np.sqrt(V[j, j]); se_o = np.sqrt(V_oim[j, j]) rows.append(dict(term=nm, coef=est[j], se_cluster=se, se_oim=se_o, z=est[j] / se, p=2 * stats.norm.sf(abs(est[j] / se)), lo=est[j] - 1.959964 * se, hi=est[j] + 1.959964 * se)) coef = pd.DataFrame(rows) coef.to_csv(os.path.join(a.out, f"coefficients_{tag}.csv"), index=False) with pd.option_context("display.float_format", "{:.5f}".format, "display.width", 140): log("\n" + coef.to_string(index=False)) # Wald tests (clustered covariance), as testparm def wald(label, idx): b = est[idx]; Vi = V[np.ix_(idx, idx)] w = float(b @ np.linalg.solve(Vi, b)) log(f"{label}: chi2({len(idx)}) = {w:.2f}, p = {stats.chi2.sf(w, len(idx)):.4g}") return dict(test=label, chi2=w, df=len(idx), p=stats.chi2.sf(w, len(idx))) ix = lambda pred: [j for j, nm in enumerate(names) if pred(nm)] tests = [ wald("Season differences at reference velocities (i.season)", ix(lambda s: s.endswith(".season"))), wald("Game-velocity slope varies by season (i.season#c.FBv_c)", ix(lambda s: s.endswith("#c.FBv_c"))), ] time_terms = ix(lambda s: s.endswith(".season") or s.endswith("#c.FBv_c")) if a.interact_season_mean: tests.append(wald("Season-velocity slope varies by season", ix(lambda s: s.endswith("#c.FBv_season_c")))) time_terms = ix(lambda s: ".season" in s) tests.append(wald("Joint: all season terms and interactions", time_terms)) pd.DataFrame(tests).to_csv(os.path.join(a.out, f"wald_tests_{tag}.csv"), index=False) # Fixed-velocity predictions, delta-method CIs beta = est[:P]; Vb = V[:P, :P] gs_vals = [0.0, 1.0] if a.role == "gs" else [0.0] preds = [] for gs in gs_vals: x_ref = design_row(names, a.first, a.first, gs=gs) for s in range(a.first, a.last + 1): x = design_row(names, s, a.first, gs=gs) xb = x @ beta; p = 1 / (1 + np.exp(-xb)) gr = 100 * p * (1 - p) * x se = np.sqrt(gr @ Vb @ gr) p0 = 1 / (1 + np.exp(-(x_ref @ beta))) gd = gr - 100 * p0 * (1 - p0) * x_ref sed = np.sqrt(gd @ Vb @ gd) preds.append(dict(season=s, GS=int(gs), k_per_100=100 * p, se=se, lo=100 * p - 1.959964 * se, hi=100 * p + 1.959964 * se, diff_vs_first=100 * (p - p0), diff_se=sed)) preds = pd.DataFrame(preds) preds.to_csv(os.path.join(a.out, f"fixed_velocity_predictions_{tag}.csv"), index=False) log("\n" + preds.round(3).to_string(index=False)) try: import matplotlib.pyplot as plt # no Agg switch, so later cells still show plots fig, ax = plt.subplots(figsize=(9, 5.2)) colors = {0: "#1f4e79", 1: "#b5532a"} labels = {0: "Relief outing (GS = 0)", 1: "Start (GS = 1)"} for gs, sub in preds.groupby("GS"): ax.plot(sub.season, sub.k_per_100, color=colors[gs], lw=2, label=labels[gs] if a.role == "gs" else None) ax.fill_between(sub.season, sub.lo, sub.hi, color=colors[gs], alpha=0.15, lw=0) ax.set_xlabel("Season"); ax.set_ylabel("Predicted strikeouts per 100 BF") ax.set_title("Strikeout rate at fixed fastball velocity", loc="left", fontsize=13, pad=22) ax.text(0, 1.01, f"Game {a.ref_game:g} mph; season mean {a.ref_season:g} mph " f"(Statcast scale, park-corrected); plain logit, no pitcher random intercept", transform=ax.transAxes, fontsize=9, color="#555") ax.set_xticks(range(a.first, a.last + 1, 2)) ax.spines[["top", "right"]].set_visible(False) ax.grid(axis="y", color="#e5e5e5") if a.role == "gs": ax.legend(frameon=False) fig.text(0.01, 0.01, "95% CIs from pitcher-clustered SEs.", fontsize=8, color="#555") fig.tight_layout(rect=(0, 0.03, 1, 1)) fig.savefig(os.path.join(a.out, f"fixed_velocity_trend_{tag}.png"), dpi=200) plt.close(fig) except ImportError: log("matplotlib not installed; plot skipped.") json.dump(dict(settings=vars(a), loglik=ll_full, converged=conv, iterations=iters, N=len(d), G=G), open(os.path.join(a.out, f"summary_{tag}.json"), "w"), indent=2) log(f"Saved results to {a.out}") if __name__ == "__main__": main() # In[20]: # ============================================================================================= # Cell 2: Area chart with the velocity / non-velocity split for 2018-2020 pooled (and the last season). # Run after the main script: uses d, g, FIRST, LAST, RESULTS and # the colours defined there. Pooled values = BF-weighted averages of the three seasons. # ============================================================================================= import os import numpy as np import pandas as pd import matplotlib.pyplot as plt for _v in ("d", "g", "FIRST", "LAST", "RESULTS"): assert _v in globals(), "Run the main script (k_velocity_split_2008.py) first" POOL = [2018, 2019, 2020] BLUE, GREY, INK, INK2, GRID = "#2a78d6", "#8a8984", "#1f1f1e", "#5f5e5a", "#e6e5e1" bf = d.groupby("season").TBF.sum() base = g.fitted.iloc[0] # 2008 league K% pp = g[g.season.isin(POOL)].copy() w = bf.loc[pp.season].to_numpy() pool = dict(fitted=np.average(pp.fitted, weights=w), at_2008_velocity=np.average(pp.at_2008_velocity, weights=w)) pool["velocity_part"] = pool["fitted"] - pool["at_2008_velocity"] pool["same_speed_part"] = pool["at_2008_velocity"] - base pool["rise_since_2008"] = pool["fitted"] - base pool["velocity_share"] = 100 * pool["velocity_part"] / pool["rise_since_2008"] lab = f"{POOL[0]}–{POOL[-1]}" pd.Series(pool).to_csv(os.path.join(RESULTS, "k_split_2018_20_pooled.csv"), header=["value"]) print(f"{lab} pooled: rise {pool['rise_since_2008']:.2f}; velocity-associated " f"{pool['velocity_part']:.2f} ({pool['velocity_share']:.0f}%); non-velocity " f"{pool['same_speed_part']:.2f} ({100 - pool['velocity_share']:.0f}%)") fig, ax = plt.subplots(figsize=(9, 5.4)) ax.fill_between(g.season, g.at_2008_velocity, g.fitted, color=BLUE, alpha=0.15, lw=0) ax.fill_between(g.season, base, g.at_2008_velocity, color=BLUE, alpha=0.42, lw=0) ax.hlines(base, FIRST, LAST, color=INK2, lw=0.8) ax.plot(g.season, g.actual, color=BLUE, lw=2.4) ax.scatter(g.season, g.actual, color=BLUE, s=22, zorder=3, edgecolor="white", linewidth=1) ax.plot(g.season, g.at_2008_velocity, color=GREY, lw=2.2, ls=(0, (5, 3))) # pooled years: horizontal bracket above the K% line top = g[g.season.isin(POOL)].actual.max() + 0.25 ax.annotate("League K%", (LAST, g.actual.iloc[-1]), xytext=(8, 0), textcoords="offset points", va="center", fontsize=10, color=BLUE, fontweight="bold") ax.annotate("K% if velocity\nhad stayed at 2008", (LAST, g.at_2008_velocity.iloc[-1]), xytext=(8, 0), textcoords="offset points", va="center", fontsize=10, color=GREY, fontweight="bold") ax.annotate("2008 level", (LAST, base), xytext=(8, 0), textcoords="offset points", va="center", fontsize=10, color=INK2) ax.text(2023.2, 21.45, "velocity-associated\nincrease", ha="center", va="center", fontsize=9.5, color=INK, fontweight="bold") ax.text(2014.8, 18.25, "velocity-unassociated\nincrease", ha="center", va="center", fontsize=9.5, color=INK, fontweight="bold") # numbers: pooled 2018-20 (centred on 2019) and the last season r_last = g[g.season == LAST].iloc[0] items = [(lab, np.mean(POOL), "center", pool["fitted"], pool["at_2008_velocity"], pool["velocity_part"], pool["velocity_share"], pool["rise_since_2008"]), (str(LAST), LAST - 0.25, "right", r_last.fitted, r_last.at_2008_velocity, r_last.velocity_part, r_last.velocity_share, r_last.rise_since_2008)] for name, x, ha, fit, cf, vp, vsh, rise in items: same = cf - base ax.text(x, (fit + cf) / 2, f"{name}\n+{vp:.1f} ({vsh:.0f}%)", ha=ha, va="center", fontsize=9.5, color=INK, fontweight="bold") ax.text(x, (cf + base) / 2, f"{name}\n+{same:.1f} ({100 * same / rise:.0f}%)", ha=ha, va="center", fontsize=9.5, color=INK, fontweight="bold") ax.set_ylim(base - 0.35, top + 0.6) ax.set_xlim(FIRST - 0.5, LAST + 4); ax.set_xticks(range(FIRST, LAST + 1, 2)) ax.set_ylabel("Strikeouts per 100 BF", color=INK2) ax.set_title("Rise in strikeouts since 2008: velocity-associated (light) vs. non-velocity (dark)", loc="left", fontsize=13, color=INK, pad=24) ax.text(0, 1.015, f"Plain logit (no pitcher intercept); dashed line = velocity at the 2008 league average; " f"{lab} = BF-weighted pool", transform=ax.transAxes, fontsize=9, color=INK2) ax.spines[["top", "right"]].set_visible(False); ax.spines[["left", "bottom"]].set_color("#bdbcb6") ax.tick_params(colors=INK2, labelsize=11) ax.grid(axis="y", color=GRID, lw=0.8); ax.grid(axis="x", visible=False); ax.set_axisbelow(True) fig.tight_layout(); fig.savefig(os.path.join(RESULTS, "k_split_2018_20_pooled.png"), dpi=200) plt.show() # In[18]: # =====================Cell 3: Strikeout rate at a fixed velocity ===================== # Run AFTER the model cell or the plain-logit script (it uses prepare, build_design, design_row). # Uses the plain-logit estimates saved by k_velocity_split_2008.py if present; otherwise fits # the plain logit here (a few seconds). Standard errors are clustered by pitcher. import os import numpy as np import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') DATA_DIR = r"[your drive partition]" INPUT = os.path.join(DATA_DIR, "pitcher_game_logs_2002_2026.csv") RESULTS = os.path.join(DATA_DIR, "fbv_jax_results") FIRST, LAST = 2008, 2026 REF_GAME = REF_SEASON = 92.0 FIXED_V = 95.0 ISM = 1 # 1 = season-mean velocity x season interaction included REFIT = False # True = ignore saved estimates and fit again os.makedirs(RESULTS, exist_ok=True) BLUE, INK, INK2, GRID = "#2a78d6", "#1f1f1e", "#5f5e5a", "#e6e5e1" # ---- estimates: load the plain-logit estimates if saved, otherwise fit ---- est_path = os.path.join(RESULTS, "plain_logit_estimates.npz") if os.path.exists(est_path) and not REFIT: E = np.load(est_path) names = list(E["names"]); beta, V = E["beta"], E["V"] print("Loaded", est_path) else: print("Fitting the plain logit...") d, _ = prepare(INPUT, FIRST, LAST, 0.5, 80.0, print) X, names = build_design(d, FIRST, LAST, REF_GAME, REF_SEASON, ISM, "none") y = d.SO.to_numpy(float); n = d.TBF.to_numpy(float) beta = np.zeros(X.shape[1]) for it in range(50): # Newton-Raphson p = 1 / (1 + np.exp(-(X @ beta))) H = (X * (n * p * (1 - p))[:, None]).T @ X step = np.linalg.solve(H, X.T @ (y - n * p)) beta += step if np.max(np.abs(step)) < 1e-10: break p = 1 / (1 + np.exp(-(X @ beta))) # pitcher-clustered sandwich codes, ci = np.unique(d.playerid.to_numpy(), return_inverse=True) S = np.zeros((len(codes), X.shape[1])); np.add.at(S, ci, X * (y - n * p)[:, None]) G = len(codes); Hinv = np.linalg.inv(H) V = G / (G - 1) * Hinv @ (S.T @ S) @ Hinv np.savez(est_path, beta=beta, V=V, names=np.array(names)) print(f"Converged in {it + 1} iterations - saved", est_path) def predict(season, fbv): """K per 100 BF and SE for game FBv = season-average FBv = fbv.""" x = design_row(names, season, FIRST, fbv_c=fbv - REF_GAME, fbv_season_c=fbv - REF_SEASON) p = 1 / (1 + np.exp(-(x @ beta))) gr = 100 * p * (1 - p) * x return 100 * p, np.sqrt(gr @ V @ gr) def style(ax, title, subtitle): ax.set_title(title, loc="left", fontsize=13, color=INK, pad=24) ax.text(0, 1.015, subtitle, transform=ax.transAxes, fontsize=9, color=INK2) ax.spines[["top", "right"]].set_visible(False) ax.spines[["left", "bottom"]].set_color("#bdbcb6") ax.tick_params(colors=INK2, labelsize=11) ax.grid(axis="y", color=GRID, lw=0.8); ax.grid(axis="x", visible=False); ax.set_axisbelow(True) seasons = np.arange(FIRST, LAST + 1) # ---------------- predicted K% at a fixed velocity ---------------- pr = np.array([predict(yr, FIXED_V) for yr in seasons]) lo, hi = pr[:, 0] - 1.96 * pr[:, 1], pr[:, 0] + 1.96 * pr[:, 1] np.savetxt(os.path.join(RESULTS, f"plain_logit_k_at_{FIXED_V:g}mph.csv"), np.column_stack([seasons, pr[:, 0], lo, hi]), delimiter=",", header="season,k_per_100,lo,hi", comments="", fmt="%.4f") fig, ax = plt.subplots(figsize=(9, 5.2)) ax.fill_between(seasons, lo, hi, color=BLUE, alpha=0.15, lw=0) ax.plot(seasons, pr[:, 0], color=BLUE, lw=2) ax.scatter(seasons, pr[:, 0], color=BLUE, s=22, zorder=3, edgecolor="white", linewidth=1) for i in (0, int(np.argmax(pr[:, 0])), len(seasons) - 1): ax.annotate(f"{pr[i, 0]:.1f}", (seasons[i], pr[i, 0]), xytext=(0, 10), textcoords="offset points", ha="center", fontsize=9, fontweight="bold", color=BLUE) ax.set_xticks(range(FIRST, LAST + 1, 2)) ax.set_ylabel("Predicted strikeouts per 100 BF", color=INK2) style(ax, f"Strikeout rate for a {FIXED_V:g} mph pitcher, {FIRST}-{LAST}", f"Plain logit (no pitcher random intercept); game FBv = season-average FBv = {FIXED_V:g} mph; " "band = 95% CI (pitcher-clustered)") fig.tight_layout(); fig.savefig(os.path.join(RESULTS, f"plain_logit_k_at_{FIXED_V:g}mph.png"), dpi=200) plt.show() # In[19]: # ============================================================================================= # Cell 4: K% across velocity: single seasons plus a pooled 2018-2020 curve in place of 2019. # Run after the main script (k_velocity_split_2008.py): uses d, beta, V, names, design_row, # FIRST, REF, RESULTS. The pooled curve is the BF-weighted average of the three seasons' # predicted K% at each velocity; its 95% CI uses the delta method (pitcher-clustered V). # ============================================================================================= import os import numpy as np import pandas as pd import matplotlib.pyplot as plt for _v in ("d", "beta", "V", "names", "design_row", "RESULTS"): assert _v in globals(), "Run the main script (k_velocity_split_2008.py) first" CURVES = [[2008], [2015], [2018, 2020], [2026]] # each entry: one season or a pool BLUE, ORANGE, AQUA, GOLD = "#2a78d6", "#eb6834", "#1baf7a", "#eda100" INK, INK2, GRID = "#1f1f1e", "#5f5e5a", "#e6e5e1" bf = d.groupby("season").TBF.sum() def predict_pool(seasons, fbv): """BF-weighted mean K per 100 BF over seasons (game = season FBv = fbv), with delta-method SE.""" w = bf.loc[seasons].to_numpy(dtype=float, copy=True); w = w / w.sum() k, grad = 0.0, np.zeros(len(beta)) for s, ws in zip(seasons, w): x = design_row(names, s, FIRST, fbv_c=fbv - REF, fbv_season_c=fbv - REF) p = 1 / (1 + np.exp(-(x @ beta))) k += ws * 100 * p grad += ws * 100 * p * (1 - p) * x return k, np.sqrt(grad @ V @ grad) def label(seasons): return str(seasons[0]) if len(seasons) == 1 else f"{seasons[0]}–{str(seasons[-1])[-2:]}" vgrid = np.linspace(80, 100, 81) fig, ax = plt.subplots(figsize=(9, 5.6)) rows, ends = [], [] for seasons, color in zip(CURVES, [BLUE, ORANGE, AQUA, GOLD]): pr = np.array([predict_pool(seasons, v) for v in vgrid]) ax.fill_between(vgrid, pr[:, 0] - 1.96 * pr[:, 1], pr[:, 0] + 1.96 * pr[:, 1], color=color, alpha=0.13, lw=0) ax.plot(vgrid, pr[:, 0], color=color, lw=2) ends.append((pr[-1, 0], label(seasons))) o = d[d.season.isin(seasons) & (d.FBv >= 80) & (d.FBv < 100)].copy() # observed, pooled o["bin"] = np.floor(o.FBv) + 0.5 b = o.groupby("bin").agg(SO=("SO", "sum"), TBF=("TBF", "sum")) b = b[b.TBF >= 500] rows += [(label(seasons), v, k, k - 1.96 * se, k + 1.96 * se) for v, (k, se) in zip(vgrid, pr)] ends.sort(); ys = [e[0] for e in ends] # end labels, nudged apart for i in range(1, len(ys)): ys[i] = max(ys[i], ys[i - 1] + 1.1) for (yv, lab), yl in zip(ends, ys): ax.annotate(lab, (vgrid[-1], yv), xytext=(100.4, yl), textcoords="data", va="center", fontsize=9, color=INK) pd.DataFrame(rows, columns=["curve", "fbv", "k_per_100", "lo", "hi"]).to_csv( os.path.join(RESULTS, "plain_logit_k_by_velocity_2018_20.csv"), index=False) ax.set_xlim(80, 101.8); ax.set_xticks(range(80, 101, 2)) ax.set_xlabel("Game fastball velocity (mph, Statcast scale)", color=INK2) ax.set_ylabel("Strikeouts per 100 BF", color=INK2) ax.set_title("Strikeout rate by fastball velocity", loc="left", fontsize=13, color=INK, pad=24) ax.text(0, 1.015, "Lines: plain logit prediction, 95% CI; 2018–20 = BF-weighted pool. " "Dots: observed K% in 1-mph bins (≥ 500 BF).", transform=ax.transAxes, fontsize=9, color=INK2) ax.spines[["top", "right"]].set_visible(False); ax.spines[["left", "bottom"]].set_color("#bdbcb6") ax.tick_params(colors=INK2, labelsize=11) ax.grid(axis="y", color=GRID, lw=0.8); ax.grid(axis="x", visible=False); ax.set_axisbelow(True) fig.tight_layout(); fig.savefig(os.path.join(RESULTS, "plain_logit_k_by_velocity_2018_20.png"), dpi=200) plt.show() # In[24]: # =====================Cell 5: Rescaled league K% and game FBv (no model needed) ===================== # Run AFTER the model cell (it uses prepare). import os import numpy as np import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') DATA_DIR = r"[your drive partition]" INPUT = os.path.join(DATA_DIR, "pitcher_game_logs_2002_2026.csv") RESULTS = os.path.join(DATA_DIR, "fbv_jax_results") FIRST, LAST = 2008, 2026 os.makedirs(RESULTS, exist_ok=True) BLUE, ORANGE, AQUA = "#2a78d6", "#eb6834", "#1baf7a" INK, INK2, GRID = "#1f1f1e", "#5f5e5a", "#e6e5e1" # ---- data (same preparation as the model) ---- d, _ = prepare(INPUT, FIRST, LAST, 0.5, 80.0, print) def style(ax, title, subtitle): ax.set_title(title, loc="left", fontsize=13, color=INK, pad=24) ax.text(0, 1.015, subtitle, transform=ax.transAxes, fontsize=9, color=INK2) ax.spines[["top", "right"]].set_visible(False) ax.spines[["left", "bottom"]].set_color("#bdbcb6") ax.tick_params(colors=INK2, labelsize=11) ax.grid(axis="y", color=GRID, lw=0.8); ax.set_axisbelow(True) # ---------------- Figure 1: rescaled league K% and game FBv ---------------- s = d[(d.FBv >= 80) & (d.FBv <= 100)] lg = s.groupby("season").agg(SO=("SO", "sum"), TBF=("TBF", "sum"), FBv=("FBv", "mean")) lg["K"] = 100 * lg.SO / lg.TBF norm = lambda v: 100 * (v - v.min()) / (v.max() - v.min()) lg["K_n"], lg["FBv_n"] = norm(lg.K), norm(lg.FBv) lg.to_csv(os.path.join(RESULTS, "fig1_league_k_fbv_by_season.csv")) fig, ax = plt.subplots(figsize=(9, 5.2)) for col, color, lab, raw, fmt in [("FBv_n", BLUE, "Avg game fastball velocity", "FBv", "{:.1f} mph"), ("K_n", ORANGE, "Strikeouts per BF", "K", "{:.1f}%")]: ax.plot(lg.index, lg[col], color=color, lw=2, label=lab) ax.scatter(lg.index, lg[col], color=color, s=22, zorder=3, edgecolor="white", linewidth=1) ax.annotate(lab, (lg.index[-1], lg[col].iloc[-1]), xytext=(6, 0), textcoords="offset points", va="center", fontsize=9, color=INK) for yr in (lg.index[0], lg[col].idxmax(), lg.index[-1]): first_yr = (yr == lg.index[0]) if first_yr and col == "FBv_n": # start labels stacked below the first point: off, ha = (10, -17), "center" # 91.5 mph on top, nudged right ... elif first_yr: off, ha = (0, -33), "center" # ... 17.5% under it else: off, ha = (0, 10), "center" ax.annotate(fmt.format(lg.loc[yr, raw]), (yr, lg.loc[yr, col]), xytext=off, textcoords="offset points", ha=ha, fontsize=10, fontweight="bold", color=color) ax.set_ylim(-20, 110); ax.set_yticks(range(0, 101, 20)); ax.set_xlim(FIRST - 1.0, LAST + 5); ax.set_xticks(range(FIRST, LAST + 1, 2)) ax.set_ylabel("Rescaled value (season min = 0, max = 100)", color=INK2) style(ax, "Velocity rose steadily; strikeouts peaked and fell back", f"League totals, outings with game FBv 80-100 mph. Each series rescaled to its own " f"{FIRST}-{LAST} range; labels show actual values.") fig.tight_layout(); fig.savefig(os.path.join(RESULTS, "fig1_normalized_k_and_fbv.png"), dpi=200) plt.show() # In[ ]: