# ============================================================================================= # Derive the pre-2017 FBv offset (PITCHf/x -> Statcast reporting change) from same-pitcher # comparisons in the FanGraphs game logs. # # Idea: a pitcher's fastball speed changes little from one season to the next (on average it # drifts slightly down with age). If the 2016 -> 2017 change for the same pitchers is noticeably # more positive than the change in neighbouring years, the extra is the measurement step. # # Three estimates: # A. Year-pair method. For every consecutive pair of seasons, take pitchers with at least # MIN_BF batters faced in both seasons and compute the change in their season FBv. # Step = 2016->17 change minus the normal change, where "normal" is either the average of # the two neighbouring pairs (2015->16, 2017->18) or the average of all other pairs # 2008->09 ... 2018->19. # B. Pitch Info benchmark. pivFA (Pitch Info) was recalibrated and has no 2017 step. For # pitchers with MIN_BF_PI batters faced in both 2016 and 2017, step = change in FBv minus # change in pivFA, same pitchers, same games. # Both methods are reported as means and medians across pitchers. # # Season FBv = BF-weighted mean of game FBv (raw: no offset, no park adjustment). # Position players are dropped with position_flags.csv if it is in the same folder. # ============================================================================================= import os import numpy as np import pandas as pd 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, 2019 # year pairs used for the "normal" change (all pre-Hawk-Eye) STEP_YEAR = 2017 # first season reported at release MIN_BF = 400 # method A threshold (both seasons) MIN_BF_PI = 100 # method B threshold (both seasons) WEIGHT_BF = False # False: each pitcher counts once (memo figures); True: weight by # the smaller of his two seasons' BF os.makedirs(RESULTS, exist_ok=True) def wmed(x, w): """Weighted median.""" o = np.argsort(x); x, w = np.asarray(x)[o], np.asarray(w)[o] c = np.cumsum(w) return float(x[np.searchsorted(c, 0.5 * c[-1])]) # ---- load ----------------------------------------------------------------------------------- d = pd.read_csv(INPUT, low_memory=False, usecols=["playerid", "season", "TBF", "FBv", "pivFA"]) d = d[d.season.between(FIRST, LAST) & (d.TBF > 0)].copy() flags = os.path.join(DATA_DIR, "position_flags.csv") if os.path.exists(flags): fl = pd.read_csv(flags) fl = fl[fl["drop"].astype(str).str.lower() == "true"][["playerid", "season"]] d = d.merge(fl.assign(_pos=1), on=["playerid", "season"], how="left") print(f"Position-player outings dropped: {int(d._pos.notna().sum()):,}") d = d[d._pos.isna()].drop(columns="_pos") def season_means(df, col): """BF-weighted season mean of `col` over outings where it is present, plus BF.""" x = df[df[col].notna()] g = x.assign(wv=x[col] * x.TBF).groupby(["playerid", "season"]) return pd.DataFrame({col: g.wv.sum() / g.TBF.sum(), "BF": g.TBF.sum()}).reset_index() # ---- A. year-pair method ------------------------------------------------------------------- ps = season_means(d, "FBv") rows = [] for y in range(FIRST, LAST): a = ps[(ps.season == y) & (ps.BF >= MIN_BF)] b = ps[(ps.season == y + 1) & (ps.BF >= MIN_BF)] m = a.merge(b, on="playerid", suffixes=("_0", "_1")) diff = m.FBv_1 - m.FBv_0 w = np.minimum(m.BF_0, m.BF_1) if WEIGHT_BF else np.ones(len(m)) rows.append(dict(pair=f"{y}->{y + 1}", pitchers=len(m), mean_change=float(np.average(diff, weights=w)), median_change=wmed(diff, w))) pairs = pd.DataFrame(rows) print(f"\nA. Same-pitcher change in season FBv, pitchers with {MIN_BF}+ BF in both seasons") print(pairs.to_string(index=False, float_format=lambda v: f"{v:+.2f}")) step_pair = f"{STEP_YEAR - 1}->{STEP_YEAR}" nbr = [f"{STEP_YEAR - 2}->{STEP_YEAR - 1}", f"{STEP_YEAR}->{STEP_YEAR + 1}"] out = [] for stat in ("mean_change", "median_change"): s = pairs.set_index("pair")[stat] jump = s[step_pair] normal_nbr = s[nbr].mean() normal_all = s.drop(step_pair).mean() out += [dict(method=f"A. year pairs ({stat.split('_')[0]})", baseline="neighbouring pairs", jump=jump, normal=normal_nbr, step=jump - normal_nbr), dict(method=f"A. year pairs ({stat.split('_')[0]})", baseline=f"all other pairs {FIRST}-{LAST}", jump=jump, normal=normal_all, step=jump - normal_all)] # ---- B. Pitch Info benchmark --------------------------------------------------------------- both = d[d.FBv.notna() & d.pivFA.notna() & d.season.isin([STEP_YEAR - 1, STEP_YEAR])] pf = season_means(both, "FBv").merge(season_means(both, "pivFA")[["playerid", "season", "pivFA"]], on=["playerid", "season"]) a = pf[(pf.season == STEP_YEAR - 1) & (pf.BF >= MIN_BF_PI)] b = pf[(pf.season == STEP_YEAR) & (pf.BF >= MIN_BF_PI)] m = a.merge(b, on="playerid", suffixes=("_0", "_1")) w = np.minimum(m.BF_0, m.BF_1) if WEIGHT_BF else np.ones(len(m)) d_fbv, d_pi = m.FBv_1 - m.FBv_0, m.pivFA_1 - m.pivFA_0 print(f"\nB. {step_pair}, {len(m)} pitchers with {MIN_BF_PI}+ BF in both seasons " f"(outings with both FBv and pivFA)") for stat, f in (("mean", lambda x: float(np.average(x, weights=w))), ("median", lambda x: wmed(x, w))): print(f" {stat:6s}: FBv {f(d_fbv):+.2f}, pivFA {f(d_pi):+.2f}, difference {f(d_fbv - d_pi):+.2f}") out.append(dict(method=f"B. FBv minus pivFA ({stat})", baseline="pivFA change, same pitchers", jump=f(d_fbv), normal=f(d_pi), step=f(d_fbv - d_pi))) # ---- summary -------------------------------------------------------------------------------- res = pd.DataFrame(out) print("\nEstimated pre-2017 offset (mph to add to 2008-2016 FBv)") print(res.to_string(index=False, float_format=lambda v: f"{v:+.2f}")) print(f"\nRange: {res.step.min():.2f} to {res.step.max():.2f} mph; median {res.step.median():.2f}") pairs.to_csv(os.path.join(RESULTS, "offset_year_pairs.csv"), index=False) res.to_csv(os.path.join(RESULTS, "offset_estimates.csv"), index=False)