Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
import numpy as np
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
-
from scipy.stats import poisson, nbinom, gamma, lognorm, kstest
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
# =========================
|
|
@@ -36,10 +37,6 @@ STAT_LABELS = {
|
|
| 36 |
"fg_made": "Field Goals Made",
|
| 37 |
}
|
| 38 |
|
| 39 |
-
# Categorize stats for distribution choice
|
| 40 |
-
COUNT_STATS = {"pass_att","pass_td","rush_att","rush_td","targets","receptions","rec_td","fg_made"}
|
| 41 |
-
YARDAGE_STATS = {"pass_yds","rec_yds","rush_yds"}
|
| 42 |
-
|
| 43 |
STAT_OPTIONS = [
|
| 44 |
"pass_att", "pass_yds", "pass_td",
|
| 45 |
"rush_att", "rush_yds", "rush_td",
|
|
@@ -49,7 +46,7 @@ STAT_OPTIONS = [
|
|
| 49 |
|
| 50 |
TIMEFRAMES = {"Game": "", "1H": "_1h", "1Q": "_1q"}
|
| 51 |
|
| 52 |
-
# Timeframe-specific milestones
|
| 53 |
NFL_MILESTONES = {
|
| 54 |
"Game": {
|
| 55 |
"pass_att": [15, 20, 25, 30, 35, 40, 45, 50],
|
|
@@ -109,25 +106,6 @@ def to_american_odds(prob: float) -> str:
|
|
| 109 |
def apply_vig(prob: float, vig_pct: float) -> float:
|
| 110 |
return max(min(float(prob) + float(vig_pct) / 100.0, 0.9999), 0.0001)
|
| 111 |
|
| 112 |
-
def american_to_prob(price: float) -> float:
|
| 113 |
-
try:
|
| 114 |
-
p = float(price)
|
| 115 |
-
except:
|
| 116 |
-
return np.nan
|
| 117 |
-
if p < 0:
|
| 118 |
-
return -p / (-p + 100.0)
|
| 119 |
-
else:
|
| 120 |
-
return 100.0 / (p + 100.0)
|
| 121 |
-
|
| 122 |
-
def devig_pair(p_over_raw: float, p_under_raw: float) -> float:
|
| 123 |
-
"""Return devigged Over probability given raw implied probs of over/under."""
|
| 124 |
-
if np.isnan(p_over_raw) or np.isnan(p_under_raw):
|
| 125 |
-
return np.nan
|
| 126 |
-
s = p_over_raw + p_under_raw
|
| 127 |
-
if s <= 0:
|
| 128 |
-
return np.nan
|
| 129 |
-
return p_over_raw / s
|
| 130 |
-
|
| 131 |
# ======================
|
| 132 |
# Modeling helpers (NBA-style blend kept for non-FG if needed later)
|
| 133 |
# ======================
|
|
@@ -159,23 +137,14 @@ def determine_best_fit(data):
|
|
| 159 |
scale=np.exp(np.mean(log_data) if len(log_data) > 1 else 0))) if ks_p > 0.01 else -np.inf
|
| 160 |
return bayesian_model_weights(poisson_ll, nb_ll, gamma_ll, lognorm_ll)
|
| 161 |
|
| 162 |
-
def calculate_probability(mean, variance, weights, milestones, raw_data
|
| 163 |
-
"""Use Poisson for counts; Normal (approx) for yards."""
|
| 164 |
probabilities = {}
|
| 165 |
-
if
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
else:
|
| 172 |
-
# Yardage: use Normal with hist variance (or cheap fallback)
|
| 173 |
-
sd = float(np.sqrt(max(variance, 1e-6)))
|
| 174 |
-
mu = float(mean)
|
| 175 |
-
for m in milestones:
|
| 176 |
-
z = (m - mu) / sd
|
| 177 |
-
p = 1 - norm.cdf(z)
|
| 178 |
-
probabilities[f"{m}+"] = round(float(max(min(p, 0.999), 0.001)), 4)
|
| 179 |
return probabilities
|
| 180 |
|
| 181 |
def generate_pie(weights, player, stat):
|
|
@@ -198,20 +167,27 @@ def generate_pie(weights, player, stat):
|
|
| 198 |
ax.axis('equal')
|
| 199 |
return fig
|
| 200 |
|
| 201 |
-
def generate_histogram(raw_data, weights, mean, variance
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
if
|
| 205 |
-
|
| 206 |
-
else
|
| 207 |
-
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
fig, ax = plt.subplots()
|
| 210 |
bins = range(int(raw_data.min()) if len(raw_data) > 0 else 0,
|
| 211 |
int(raw_data.max()) + 2 if len(raw_data) > 0 else 2)
|
| 212 |
ax.hist(raw_data, bins=bins, density=True, alpha=0.6)
|
| 213 |
-
ax.plot(x,
|
| 214 |
-
ax.set_title("Histogram vs. Model PDF")
|
| 215 |
ax.legend()
|
| 216 |
return fig
|
| 217 |
|
|
@@ -249,14 +225,18 @@ def _p_make(a, b, R, d):
|
|
| 249 |
return 1.0 / (1.0 + np.exp(-z))
|
| 250 |
|
| 251 |
def _fg_lambda_p_from_slice(pdata: pd.DataFrame, timeframe: str):
|
|
|
|
| 252 |
suf = {"Game":"", "1H":"_1h", "1Q":"_1q"}[timeframe]
|
| 253 |
att_cols = [f"fg_att_{k}{suf}" for k,_ in FG_BINS]
|
| 254 |
made_cols = [f"fg_made_{k}{suf}" for k,_ in FG_BINS]
|
|
|
|
| 255 |
atts = np.array([pd.to_numeric(pdata.get(c, pd.Series(dtype=float)), errors="coerce").fillna(0).sum() for c in att_cols])
|
| 256 |
made = np.array([pd.to_numeric(pdata.get(c, pd.Series(dtype=float)), errors="coerce").fillna(0).sum() for c in made_cols])
|
|
|
|
| 257 |
games = pdata["game_id"].nunique() if "game_id" in pdata.columns else len(pdata)
|
| 258 |
games = games if games > 0 else 1
|
| 259 |
lam_bins = atts / games
|
|
|
|
| 260 |
dist_mid = np.array([mid for _, mid in FG_BINS])
|
| 261 |
p_hat = np.array([_beta_smooth_rate(m, a) for m, a in zip(made, atts)])
|
| 262 |
a, b, R = _fit_logistic_from_bins(dist_mid, p_hat)
|
|
@@ -265,126 +245,136 @@ def _fg_lambda_p_from_slice(pdata: pd.DataFrame, timeframe: str):
|
|
| 265 |
def fg_kball_mean_var_for_slice(pdata: pd.DataFrame, timeframe: str,
|
| 266 |
range_gain_yds=7.0, acc_bump_abs=0.02, long_propensity=0.10):
|
| 267 |
lam_bins, (a, b, R), dist_mid = _fg_lambda_p_from_slice(pdata, timeframe)
|
|
|
|
|
|
|
| 268 |
tilt = np.ones_like(lam_bins)
|
| 269 |
long_idx = np.array([i for i,(k,_) in enumerate(FG_BINS) if k in ("55_59","60_plus")])
|
| 270 |
tilt[long_idx] = 1.0 + float(long_propensity)
|
| 271 |
lam_bins = lam_bins * tilt
|
|
|
|
|
|
|
| 272 |
total = lam_bins.sum()
|
| 273 |
if total > 1e-12:
|
| 274 |
lam_bins = lam_bins * (total / lam_bins.sum())
|
|
|
|
|
|
|
| 275 |
def p_range(d): return _p_make(a, b, R + float(range_gain_yds), d)
|
| 276 |
p_vec = np.array([np.clip(p_range(d) + float(acc_bump_abs), 0.0, 1.0) for d in dist_mid])
|
|
|
|
| 277 |
mu = float(np.sum(lam_bins * p_vec))
|
| 278 |
var = float(np.sum(lam_bins * p_vec * (1.0 - p_vec)))
|
| 279 |
return mu, var
|
| 280 |
|
| 281 |
# ====================
|
| 282 |
-
# Market
|
| 283 |
# ====================
|
| 284 |
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
for key in [f"{base}_line", f"{base}_over_odds", f"{base}_under_odds"]:
|
| 291 |
-
variants += [f"{key}{tf_suf}", f"{base}{tf_suf}_{key.split('_',1)[1]}"]
|
| 292 |
-
return variants
|
| 293 |
-
|
| 294 |
-
def _find_first_existing(cols, df_cols):
|
| 295 |
-
for c in cols:
|
| 296 |
-
if c in df_cols:
|
| 297 |
-
return c
|
| 298 |
-
return None
|
| 299 |
-
|
| 300 |
-
def find_odds_columns(df: pd.DataFrame, stat_key: str, timeframe: str):
|
| 301 |
-
"""Return (line_col, over_col, under_col) or (None,None,None) if not found."""
|
| 302 |
-
base = STAT_MAP.get(stat_key, stat_key)
|
| 303 |
-
tf_suf = TIMEFRAMES.get(timeframe, "")
|
| 304 |
-
# Try with suffix; also try without suffix for Game
|
| 305 |
-
candidates = _possible_odds_cols(base, tf_suf)
|
| 306 |
-
if timeframe == "Game":
|
| 307 |
-
candidates += _possible_odds_cols(base, "")
|
| 308 |
-
# Build maps
|
| 309 |
-
line_col = _find_first_existing([c for c in candidates if "line" in c], df.columns)
|
| 310 |
-
over_col = _find_first_existing([c for c in candidates if "over_odds" in c], df.columns)
|
| 311 |
-
under_col = _find_first_existing([c for c in candidates if "under_odds" in c], df.columns)
|
| 312 |
-
return line_col, over_col, under_col
|
| 313 |
-
|
| 314 |
-
def invert_poisson_tail_for_mean(k: float, p_over: float, lo=0.0001, hi=8.0, tol=1e-5, iters=40):
|
| 315 |
-
"""Solve for lambda such that P(X>=k) = p_over. Brackets widened adaptively if needed."""
|
| 316 |
-
# Ensure bracket supports probability
|
| 317 |
-
def tail(lmbd): return 1 - poisson.cdf(k-1, lmbd)
|
| 318 |
-
# Expand hi if needed
|
| 319 |
-
while tail(hi) > p_over and hi < 50:
|
| 320 |
-
hi *= 2.0
|
| 321 |
-
while tail(lo) < p_over and lo > 1e-6:
|
| 322 |
-
lo *= 0.5
|
| 323 |
-
a, b = lo, hi
|
| 324 |
-
for _ in range(iters):
|
| 325 |
-
m = 0.5*(a+b)
|
| 326 |
-
tm = tail(m)
|
| 327 |
-
if abs(tm - p_over) < tol: return float(m)
|
| 328 |
-
if tm > p_over:
|
| 329 |
-
a = m
|
| 330 |
-
else:
|
| 331 |
-
b = m
|
| 332 |
-
return float(m)
|
| 333 |
-
|
| 334 |
-
def market_mean_for_row(row, stat_key, timeframe):
|
| 335 |
-
"""Given a single row with line/odds, return market-implied mean for that row."""
|
| 336 |
-
line_col, over_col, under_col = find_odds_columns(row.to_frame().T, stat_key, timeframe)
|
| 337 |
-
if not line_col or not over_col or not under_col:
|
| 338 |
-
return np.nan
|
| 339 |
-
line = pd.to_numeric(row.get(line_col), errors="coerce")
|
| 340 |
-
over = pd.to_numeric(row.get(over_col), errors="coerce")
|
| 341 |
-
under = pd.to_numeric(row.get(under_col), errors="coerce")
|
| 342 |
-
if pd.isna(line) or pd.isna(over) or pd.isna(under):
|
| 343 |
return np.nan
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
if pd.isna(p_over):
|
| 349 |
return np.nan
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
if stat_key in COUNT_STATS:
|
| 353 |
-
# If the book quotes e.g. 1.5, the ">= 2" threshold is k=2.
|
| 354 |
-
k = np.ceil(float(line) + 1e-9)
|
| 355 |
-
return invert_poisson_tail_for_mean(k, float(p_over))
|
| 356 |
-
else:
|
| 357 |
-
# We'll fill yardage market mean later when estimating, using sd from history:
|
| 358 |
-
# here just pass line & p_over via a tuple wrapper to avoid double lookup.
|
| 359 |
-
return (float(line), float(p_over)) # placeholder for yardage
|
| 360 |
-
|
| 361 |
-
def market_mean_for_player(df, player, stat_key, timeframe, hist_sd=None):
|
| 362 |
-
"""Aggregate market-implied mean across player's rows with odds (since 2023+)."""
|
| 363 |
-
pdata = df[df.get("player") == player]
|
| 364 |
-
if pdata.empty:
|
| 365 |
return np.nan
|
|
|
|
| 366 |
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
for
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
else:
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
|
| 389 |
# ====================
|
| 390 |
# Globals
|
|
@@ -392,7 +382,6 @@ def market_mean_for_player(df, player, stat_key, timeframe, hist_sd=None):
|
|
| 392 |
df = None
|
| 393 |
model_weights_df = None
|
| 394 |
temp_df = None
|
| 395 |
-
status_warnings = []
|
| 396 |
|
| 397 |
# =========================
|
| 398 |
# Loading + preprocessing
|
|
@@ -402,51 +391,24 @@ def csv_col_for(stat_key: str, timeframe_key: str) -> str:
|
|
| 402 |
|
| 403 |
def load_and_prepare_data(file):
|
| 404 |
"""Populate globals and return UI choices."""
|
| 405 |
-
global df, model_weights_df, temp_df
|
| 406 |
-
status_warnings = []
|
| 407 |
-
|
| 408 |
if file is None:
|
| 409 |
return (gr.update(choices=[], value=None),
|
| 410 |
gr.update(choices=[], value=[]),
|
| 411 |
gr.update(choices=[], value=None),
|
|
|
|
| 412 |
"Upload the CSV to begin.")
|
| 413 |
|
| 414 |
-
# Try to read with header; if the first row doesn't contain strings, pandas may still create numeric headers.
|
| 415 |
raw = pd.read_csv(file.name)
|
| 416 |
-
|
| 417 |
-
raw.columns = [str(c).strip() for c in raw.columns]
|
| 418 |
df = raw.copy()
|
| 419 |
temp_df = raw.copy()
|
| 420 |
|
| 421 |
-
# Basic sanity columns
|
| 422 |
-
if "player" not in df.columns:
|
| 423 |
-
# Try to infer a likely "player" column by max unique strings
|
| 424 |
-
# If none found → create a placeholder and warn.
|
| 425 |
-
status_warnings.append("⚠️ Could not find 'player' column. App will not function fully without it.")
|
| 426 |
-
df["player"] = ""
|
| 427 |
-
|
| 428 |
-
if "position" not in df.columns:
|
| 429 |
-
status_warnings.append("⚠️ 'position' not found. Position filters will be limited.")
|
| 430 |
-
df["position"] = ""
|
| 431 |
-
|
| 432 |
-
if "player_team" not in df.columns:
|
| 433 |
-
status_warnings.append("⚠️ 'player_team' not found. Team tab filtering will be limited.")
|
| 434 |
-
df["player_team"] = ""
|
| 435 |
-
|
| 436 |
teams = sorted(df.get("player_team", pd.Series(dtype=str)).dropna().astype(str).str.strip().replace("", np.nan).dropna().unique().tolist())
|
| 437 |
players = sorted(df.get("player", pd.Series(dtype=str)).dropna().astype(str).str.strip().replace("", np.nan).dropna().unique().tolist())
|
| 438 |
positions = sorted(df.get("position", pd.Series(dtype=str)).dropna().astype(str).str.strip().replace("", np.nan).dropna().unique().tolist())
|
| 439 |
|
| 440 |
-
#
|
| 441 |
-
probe_cols = []
|
| 442 |
-
for stat_key in ["fg_made","receptions","pass_yds"]:
|
| 443 |
-
for tf in TIMEFRAMES.keys():
|
| 444 |
-
lc, oc, uc = find_odds_columns(df.head(1), stat_key, tf)
|
| 445 |
-
probe_cols += [lc, oc, uc]
|
| 446 |
-
if not any([c for c in probe_cols if c in df.columns and c is not None]):
|
| 447 |
-
status_warnings.append("⚠️ No recognizable odds columns were found. Market integration will be disabled until headers are added (e.g., 'receptions_line', 'receptions_over_odds', 'receptions_under_odds').")
|
| 448 |
-
|
| 449 |
-
# Precompute per-player model weights (kept as before; used for charts on non-yardage if you want)
|
| 450 |
rows = []
|
| 451 |
for player in players:
|
| 452 |
pdata = df[df["player"] == player]
|
|
@@ -466,18 +428,19 @@ def load_and_prepare_data(file):
|
|
| 466 |
model_weights_df = pd.DataFrame(rows)
|
| 467 |
|
| 468 |
msg = "✅ File loaded."
|
| 469 |
-
if
|
| 470 |
-
msg
|
| 471 |
|
| 472 |
return (gr.update(choices=teams, value=(teams[0] if teams else None)),
|
| 473 |
gr.update(choices=players, value=[]),
|
| 474 |
gr.update(choices=positions, value=None),
|
|
|
|
| 475 |
msg)
|
| 476 |
|
| 477 |
# =========================
|
| 478 |
-
# Player tab (position filter + league average + K-ball knobs + Market
|
| 479 |
# =========================
|
| 480 |
-
def estimate(player, stat_key, timeframe,
|
| 481 |
range_gain_yds, acc_bump_abs, long_propensity):
|
| 482 |
if df is None or model_weights_df is None:
|
| 483 |
return "⚠️ Upload the CSV.", pd.DataFrame(), {"Poisson":1,"Negative Binomial":0,"Gamma-Poisson":0,"Log-Normal":0}, pd.Series(dtype=float)
|
|
@@ -487,42 +450,37 @@ def estimate(player, stat_key, timeframe, source_sel, market_w, custom_mean, vig
|
|
| 487 |
|
| 488 |
stat_col = csv_col_for(stat_key, timeframe)
|
| 489 |
pdata = df[df['player'] == player]
|
| 490 |
-
|
|
|
|
|
|
|
|
|
|
| 491 |
|
| 492 |
-
# Historical
|
| 493 |
-
hist_mean =
|
| 494 |
-
variance
|
| 495 |
|
| 496 |
-
# K-ball
|
| 497 |
is_kicker = (pdata.get("position") == "K").any()
|
| 498 |
if stat_key == "fg_made" and is_kicker:
|
| 499 |
-
mu_k, var_k = fg_kball_mean_var_for_slice(pdata, timeframe,
|
| 500 |
-
|
| 501 |
-
variance =
|
| 502 |
-
|
| 503 |
-
# Market mean from
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
if
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
if pd.isna(market_mean):
|
| 519 |
-
mean_final = mean_model
|
| 520 |
-
src_note = "Historical (market missing)"
|
| 521 |
-
else:
|
| 522 |
-
mean_final = w * market_mean + (1 - w) * mean_model
|
| 523 |
-
src_note = f"Blended (w_market={w:.2f})"
|
| 524 |
-
|
| 525 |
-
# Weights (for plots)
|
| 526 |
try:
|
| 527 |
wrow = model_weights_df[model_weights_df['player'] == player].iloc[0]
|
| 528 |
weights = {
|
|
@@ -535,10 +493,9 @@ def estimate(player, stat_key, timeframe, source_sel, market_w, custom_mean, vig
|
|
| 535 |
weights = {"Poisson":1.0, "Negative Binomial":0.0, "Gamma-Poisson":0.0, "Log-Normal":0.0}
|
| 536 |
|
| 537 |
ms = milestones_for(stat_key, timeframe)
|
| 538 |
-
# Full set
|
| 539 |
-
probs_all = calculate_probability(mean_final, variance, weights, ms, raw_series, stat_key)
|
| 540 |
|
| 541 |
-
#
|
|
|
|
| 542 |
if show_extremes == "Hide Extreme Probabilities":
|
| 543 |
probs = {k: v for k, v in probs_all.items() if 0.005 <= v <= 0.99}
|
| 544 |
if len(probs) == 0:
|
|
@@ -549,13 +506,12 @@ def estimate(player, stat_key, timeframe, source_sel, market_w, custom_mean, vig
|
|
| 549 |
games = pdata['game_id'].nunique() if "game_id" in pdata.columns else len(pdata)
|
| 550 |
hit_rates = []
|
| 551 |
for m in ms:
|
| 552 |
-
hits = (
|
| 553 |
hit_rates.append(round((hits / games) * 100, 2) if games > 0 else 0.0)
|
| 554 |
|
| 555 |
filtered = list(probs.keys())
|
| 556 |
filt_hr = [hr for m, hr in zip(ms, hit_rates) if f"{m}+" in filtered]
|
| 557 |
|
| 558 |
-
# Optional: also show model-only and market-only columns for context (if available)
|
| 559 |
table = pd.DataFrame({
|
| 560 |
"Milestone": filtered,
|
| 561 |
"Probability": [f"{v * 100:.2f}%" for v in probs.values()],
|
|
@@ -564,27 +520,18 @@ def estimate(player, stat_key, timeframe, source_sel, market_w, custom_mean, vig
|
|
| 564 |
"Hit Rate": [f"{x:.2f}%" for x in filt_hr],
|
| 565 |
})
|
| 566 |
|
| 567 |
-
|
| 568 |
-
row_msgs = []
|
| 569 |
-
if pd.isna(market_mean):
|
| 570 |
-
row_msgs.append("Market odds not found/usable for this player/stat/timeframe; using historical only in blends.")
|
| 571 |
-
|
| 572 |
summary_md = f"""
|
| 573 |
### 📊 {player} — {STAT_LABELS.get(stat_key, stat_key)} ({timeframe})
|
| 574 |
- Games in sample: **{games}**
|
| 575 |
-
-
|
| 576 |
-
-
|
| 577 |
-
-
|
| 578 |
-
-
|
| 579 |
-
- Variance (historical/model side): **{variance:.3f}**
|
| 580 |
-
- Vig: **{vig:.2f}%** — Extremes: **{'Shown' if show_extremes=='Show All' else 'Hidden (fallback if empty)'}**
|
| 581 |
-
{('> ' + ' '.join(row_msgs)) if row_msgs else ''}
|
| 582 |
"""
|
| 583 |
-
return summary_md, table, weights,
|
| 584 |
|
| 585 |
-
def league_avg_by_position(position, stat_key, timeframe, vig,
|
| 586 |
-
range_gain_yds, acc_bump_abs, long_propensity,
|
| 587 |
-
source_sel, market_w):
|
| 588 |
if df is None:
|
| 589 |
return pd.DataFrame(), "Upload a CSV first."
|
| 590 |
if not position:
|
|
@@ -594,44 +541,28 @@ def league_avg_by_position(position, stat_key, timeframe, vig,
|
|
| 594 |
if slice_df.empty:
|
| 595 |
return pd.DataFrame(), f"No rows for position {position}."
|
| 596 |
|
| 597 |
-
stat_col = csv_col_for(stat_key, timeframe)
|
| 598 |
ms = milestones_for(stat_key, timeframe)
|
|
|
|
| 599 |
|
| 600 |
-
# Historical mean
|
| 601 |
-
series = pd.to_numeric(slice_df.get(stat_col, pd.Series(dtype=float)), errors="coerce").dropna()
|
| 602 |
-
hist_mean = series.mean() if len(series) else 0.0
|
| 603 |
-
variance = series.var() if len(series) else 1.0
|
| 604 |
-
|
| 605 |
-
# K-ball adjustment for league average ONLY when position=K & stat is fg_made
|
| 606 |
if stat_key == "fg_made" and position == "K":
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
# Market mean from odds across the slice
|
| 612 |
-
hist_sd = float(np.sqrt(max(variance, 1e-6))) if stat_key in YARDAGE_STATS else None
|
| 613 |
-
# Average player-level market means within the position slice
|
| 614 |
-
market_means = []
|
| 615 |
-
for player in slice_df["player"].dropna().astype(str).unique():
|
| 616 |
-
mm = market_mean_for_player(slice_df, player, stat_key, timeframe, hist_sd=hist_sd)
|
| 617 |
-
if pd.notna(mm):
|
| 618 |
-
market_means.append(mm)
|
| 619 |
-
market_mean = float(np.mean(market_means)) if len(market_means) else np.nan
|
| 620 |
-
|
| 621 |
-
# Blend per controls
|
| 622 |
-
if source_sel == "Historical Only":
|
| 623 |
-
mean_final = hist_mean
|
| 624 |
-
elif source_sel == "Market Only":
|
| 625 |
-
mean_final = market_mean if pd.notna(market_mean) else hist_mean
|
| 626 |
else:
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 632 |
|
| 633 |
-
|
| 634 |
-
probs = calculate_probability(mean_final, variance, {"Poisson":1, "Negative Binomial":0, "Gamma-Poisson":0, "Log-Normal":0}, ms, series, stat_key)
|
| 635 |
table = pd.DataFrame({
|
| 636 |
"Milestone": list(probs.keys()),
|
| 637 |
"Probability": [f"{p*100:.2f}%" for p in probs.values()],
|
|
@@ -664,22 +595,16 @@ def team_table(team, stat_key, timeframe, vig, subset_players):
|
|
| 664 |
for player, pdata in work.groupby("player", dropna=True):
|
| 665 |
series = pd.to_numeric(pdata.get(stat_col, pd.Series(dtype=float)), errors="coerce").dropna()
|
| 666 |
games = pdata['game_id'].nunique() if "game_id" in pdata.columns else len(pdata)
|
| 667 |
-
# Team tab uses historical rates only (no K-ball knobs here for simplicity)
|
| 668 |
mean = series.mean() if len(series) else 0.0
|
| 669 |
-
variance = series.var() if len(series) else 1.0
|
| 670 |
-
|
| 671 |
-
# Count vs yardage tail probs
|
| 672 |
-
probs = calculate_probability(mean, variance, {"Poisson":1,"Negative Binomial":0,"Gamma-Poisson":0,"Log-Normal":0}, milestones, series, stat_key)
|
| 673 |
for m in milestones:
|
| 674 |
-
|
| 675 |
-
p = probs.get(key, 0.001)
|
| 676 |
rows.append({
|
| 677 |
"Player": player,
|
| 678 |
"Games": games,
|
| 679 |
-
"Milestone":
|
| 680 |
-
"Prob": round(
|
| 681 |
-
"True Odds": to_american_odds(
|
| 682 |
-
"Odds w/ Vig": to_american_odds(apply_vig(
|
| 683 |
"Mean": round(mean, 2),
|
| 684 |
})
|
| 685 |
|
|
@@ -704,8 +629,8 @@ with gr.Blocks(title="🏈 NFL Milestone Explorer",
|
|
| 704 |
|
| 705 |
# Populate choices on upload
|
| 706 |
def on_file(f):
|
| 707 |
-
team_upd, players_upd, pos_upd, msg = load_and_prepare_data(f)
|
| 708 |
-
return team_upd, players_upd, pos_upd, msg
|
| 709 |
|
| 710 |
with gr.Tabs():
|
| 711 |
|
|
@@ -730,15 +655,9 @@ with gr.Blocks(title="🏈 NFL Milestone Explorer",
|
|
| 730 |
with gr.Row():
|
| 731 |
p_stat = gr.Dropdown(label="Stat", choices=STAT_OPTIONS, value="rec_yds")
|
| 732 |
p_time = gr.Radio(list(TIMEFRAMES.keys()), value="Game", label="Timeframe")
|
| 733 |
-
|
| 734 |
-
gr.Markdown("### Projection Source")
|
| 735 |
with gr.Row():
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
market_weight = gr.Slider(0, 100, value=60, step=5, label="Market Weight (%) — when Blended")
|
| 739 |
-
|
| 740 |
-
with gr.Row():
|
| 741 |
-
custom_proj = gr.Number(label="Custom Mean (only used if Source = Use Custom Projection)", precision=3)
|
| 742 |
p_vig = gr.Number(label="Vig (%)", value=4.92, precision=2)
|
| 743 |
extreme_toggle = gr.Radio(["Hide Extreme Probabilities", "Show All"], label="Extreme Probabilities", value="Hide Extreme Probabilities")
|
| 744 |
|
|
@@ -765,16 +684,12 @@ with gr.Blocks(title="🏈 NFL Milestone Explorer",
|
|
| 765 |
league_range_gain = gr.Slider(0, 10, value=7, step=1, label="K-ball: Range gain (yds)")
|
| 766 |
league_acc_bump = gr.Slider(0, 0.10, value=0.02, step=0.01, label="K-ball: Accuracy bump (abs prob)")
|
| 767 |
league_long_prop = gr.Slider(0, 0.5, value=0.10, step=0.05, label="K-ball: Long-attempt propensity")
|
| 768 |
-
with gr.Row():
|
| 769 |
-
league_source = gr.Radio(["Historical Only", "Market Only", "Blended"], value="Blended", label="Projection Source")
|
| 770 |
-
league_market_weight = gr.Slider(0, 100, value=60, step=5, label="Market Weight (%) — when Blended")
|
| 771 |
-
|
| 772 |
league_btn = gr.Button("Compute League Avg", elem_classes=["green-button"])
|
| 773 |
league_title = gr.Markdown(visible=False)
|
| 774 |
league_table = gr.Dataframe(visible=False, label="League Avg Milestones")
|
| 775 |
|
| 776 |
-
# Hook file load
|
| 777 |
-
file_in.change(on_file, inputs=[file_in], outputs=[team_dd, players_ms, position_dd, status])
|
| 778 |
|
| 779 |
# Team roster subset choices
|
| 780 |
def on_team_change(team):
|
|
@@ -808,7 +723,7 @@ with gr.Blocks(title="🏈 NFL Milestone Explorer",
|
|
| 808 |
outputs=[team_title, team_table_out])
|
| 809 |
|
| 810 |
# Player run (multi-player; charts only when a single player is selected)
|
| 811 |
-
def run_players(pos, players, stat, timeframe,
|
| 812 |
if df is None:
|
| 813 |
return (gr.update(visible=False), gr.update(visible=False),
|
| 814 |
gr.update(visible=False), gr.update(visible=False),
|
|
@@ -830,8 +745,8 @@ with gr.Blocks(title="🏈 NFL Milestone Explorer",
|
|
| 830 |
first_raw = None
|
| 831 |
|
| 832 |
for i, player in enumerate(players):
|
| 833 |
-
summary_md, table, weights, raw = estimate(player, stat, timeframe,
|
| 834 |
-
|
| 835 |
t = table.copy()
|
| 836 |
t.insert(0, "Player", player)
|
| 837 |
all_rows.append(t)
|
|
@@ -844,13 +759,11 @@ with gr.Blocks(title="🏈 NFL Milestone Explorer",
|
|
| 844 |
if len(players) == 1 and first_weights is not None:
|
| 845 |
pie = generate_pie(first_weights, players[0], stat)
|
| 846 |
raw = first_raw if first_raw is not None else pd.Series(dtype=float)
|
| 847 |
-
# For yardage, histogram overlay uses Normal approx; for counts uses Poisson
|
| 848 |
hist = generate_histogram(
|
| 849 |
raw,
|
| 850 |
first_weights,
|
| 851 |
-
custom_mean if
|
| 852 |
-
raw.var() if len(raw)>0 else
|
| 853 |
-
stat
|
| 854 |
)
|
| 855 |
recent = temp_df[temp_df['player'] == players[0]].copy()
|
| 856 |
if "game_id" in recent.columns and "gameday" in recent.columns:
|
|
@@ -873,19 +786,17 @@ with gr.Blocks(title="🏈 NFL Milestone Explorer",
|
|
| 873 |
gr.update(visible=False))
|
| 874 |
|
| 875 |
player_btn.click(run_players,
|
| 876 |
-
inputs=[position_dd, players_ms, p_stat, p_time,
|
| 877 |
-
custom_proj, p_vig, extreme_toggle,
|
| 878 |
range_gain, acc_bump, long_prop],
|
| 879 |
outputs=[p_summary, p_table, p_pie, p_hist, recent_games])
|
| 880 |
|
| 881 |
# League average run (by position)
|
| 882 |
-
def run_league_avg(pos, stat, timeframe, vig, rg, acc, lp
|
| 883 |
-
table, title = league_avg_by_position(pos, stat, timeframe, vig, rg, acc, lp
|
| 884 |
return gr.update(value=title, visible=True), gr.update(value=table, visible=True)
|
| 885 |
league_btn.click(run_league_avg,
|
| 886 |
inputs=[league_pos, league_stat, league_time, league_vig,
|
| 887 |
-
league_range_gain, league_acc_bump, league_long_prop,
|
| 888 |
-
league_source, league_market_weight],
|
| 889 |
outputs=[league_title, league_table])
|
| 890 |
|
| 891 |
demo.launch()
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import numpy as np
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
+
from scipy.stats import poisson, nbinom, gamma, lognorm, kstest
|
| 5 |
+
import scipy.stats as sps
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
# =========================
|
|
|
|
| 37 |
"fg_made": "Field Goals Made",
|
| 38 |
}
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
STAT_OPTIONS = [
|
| 41 |
"pass_att", "pass_yds", "pass_td",
|
| 42 |
"rush_att", "rush_yds", "rush_td",
|
|
|
|
| 46 |
|
| 47 |
TIMEFRAMES = {"Game": "", "1H": "_1h", "1Q": "_1q"}
|
| 48 |
|
| 49 |
+
# Timeframe-specific milestones
|
| 50 |
NFL_MILESTONES = {
|
| 51 |
"Game": {
|
| 52 |
"pass_att": [15, 20, 25, 30, 35, 40, 45, 50],
|
|
|
|
| 106 |
def apply_vig(prob: float, vig_pct: float) -> float:
|
| 107 |
return max(min(float(prob) + float(vig_pct) / 100.0, 0.9999), 0.0001)
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
# ======================
|
| 110 |
# Modeling helpers (NBA-style blend kept for non-FG if needed later)
|
| 111 |
# ======================
|
|
|
|
| 137 |
scale=np.exp(np.mean(log_data) if len(log_data) > 1 else 0))) if ks_p > 0.01 else -np.inf
|
| 138 |
return bayesian_model_weights(poisson_ll, nb_ll, gamma_ll, lognorm_ll)
|
| 139 |
|
| 140 |
+
def calculate_probability(mean, variance, weights, milestones, raw_data):
|
|
|
|
| 141 |
probabilities = {}
|
| 142 |
+
if mean <= 0:
|
| 143 |
+
return {f"{m}+": 0.001 for m in milestones}
|
| 144 |
+
for m in milestones:
|
| 145 |
+
# Poisson tail — fits counts well and is what we use post K-ball mean for FG
|
| 146 |
+
poisson_prob = 1 - poisson.cdf(m - 1, mean)
|
| 147 |
+
probabilities[f"{m}+"] = round(max(poisson_prob, 0.001), 4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
return probabilities
|
| 149 |
|
| 150 |
def generate_pie(weights, player, stat):
|
|
|
|
| 167 |
ax.axis('equal')
|
| 168 |
return fig
|
| 169 |
|
| 170 |
+
def generate_histogram(raw_data, weights, mean, variance):
|
| 171 |
+
x = np.linspace(0, max(raw_data.max() if len(raw_data) else 1, 10) + 10, 500)
|
| 172 |
+
poisson_pdf = poisson.pmf(np.floor(x), mean) if mean > 0 else np.zeros_like(x)
|
| 173 |
+
nb_r = mean ** 2 / (variance - mean) if variance > mean else 10
|
| 174 |
+
nb_p = mean / variance if variance > mean else 0.5
|
| 175 |
+
nb_pdf = nbinom.pmf(np.floor(x), nb_r, nb_p) if nb_r > 0 and nb_p > 0 else np.zeros_like(x)
|
| 176 |
+
gamma_pdf = gamma.pdf(x, a=mean, scale=variance / mean) if mean > 0 and variance > 0 else np.zeros_like(x)
|
| 177 |
+
lognorm_pdf = np.zeros_like(x)
|
| 178 |
+
log_data = np.log1p(raw_data[raw_data > 0])
|
| 179 |
+
if len(log_data) > 1:
|
| 180 |
+
lognorm_pdf = lognorm.pdf(x, s=np.std(log_data), scale=np.exp(np.mean(log_data)))
|
| 181 |
+
blended_pdf = (weights["Poisson"] * poisson_pdf +
|
| 182 |
+
weights["Negative Binomial"] * nb_pdf +
|
| 183 |
+
weights["Gamma-Poisson"] * gamma_pdf +
|
| 184 |
+
weights["Log-Normal"] * lognorm_pdf)
|
| 185 |
fig, ax = plt.subplots()
|
| 186 |
bins = range(int(raw_data.min()) if len(raw_data) > 0 else 0,
|
| 187 |
int(raw_data.max()) + 2 if len(raw_data) > 0 else 2)
|
| 188 |
ax.hist(raw_data, bins=bins, density=True, alpha=0.6)
|
| 189 |
+
ax.plot(x, blended_pdf, 'k-', label="Blended Model", linewidth=2)
|
| 190 |
+
ax.set_title("Histogram vs. Blended Model PDF")
|
| 191 |
ax.legend()
|
| 192 |
return fig
|
| 193 |
|
|
|
|
| 225 |
return 1.0 / (1.0 + np.exp(-z))
|
| 226 |
|
| 227 |
def _fg_lambda_p_from_slice(pdata: pd.DataFrame, timeframe: str):
|
| 228 |
+
"""Return per-game attempt rates per bin and smoothed accuracy per bin."""
|
| 229 |
suf = {"Game":"", "1H":"_1h", "1Q":"_1q"}[timeframe]
|
| 230 |
att_cols = [f"fg_att_{k}{suf}" for k,_ in FG_BINS]
|
| 231 |
made_cols = [f"fg_made_{k}{suf}" for k,_ in FG_BINS]
|
| 232 |
+
|
| 233 |
atts = np.array([pd.to_numeric(pdata.get(c, pd.Series(dtype=float)), errors="coerce").fillna(0).sum() for c in att_cols])
|
| 234 |
made = np.array([pd.to_numeric(pdata.get(c, pd.Series(dtype=float)), errors="coerce").fillna(0).sum() for c in made_cols])
|
| 235 |
+
|
| 236 |
games = pdata["game_id"].nunique() if "game_id" in pdata.columns else len(pdata)
|
| 237 |
games = games if games > 0 else 1
|
| 238 |
lam_bins = atts / games
|
| 239 |
+
|
| 240 |
dist_mid = np.array([mid for _, mid in FG_BINS])
|
| 241 |
p_hat = np.array([_beta_smooth_rate(m, a) for m, a in zip(made, atts)])
|
| 242 |
a, b, R = _fit_logistic_from_bins(dist_mid, p_hat)
|
|
|
|
| 245 |
def fg_kball_mean_var_for_slice(pdata: pd.DataFrame, timeframe: str,
|
| 246 |
range_gain_yds=7.0, acc_bump_abs=0.02, long_propensity=0.10):
|
| 247 |
lam_bins, (a, b, R), dist_mid = _fg_lambda_p_from_slice(pdata, timeframe)
|
| 248 |
+
|
| 249 |
+
# Tilt long attempts
|
| 250 |
tilt = np.ones_like(lam_bins)
|
| 251 |
long_idx = np.array([i for i,(k,_) in enumerate(FG_BINS) if k in ("55_59","60_plus")])
|
| 252 |
tilt[long_idx] = 1.0 + float(long_propensity)
|
| 253 |
lam_bins = lam_bins * tilt
|
| 254 |
+
|
| 255 |
+
# Keep total attempts per game constant after tilting
|
| 256 |
total = lam_bins.sum()
|
| 257 |
if total > 1e-12:
|
| 258 |
lam_bins = lam_bins * (total / lam_bins.sum())
|
| 259 |
+
|
| 260 |
+
# Range + accuracy bump
|
| 261 |
def p_range(d): return _p_make(a, b, R + float(range_gain_yds), d)
|
| 262 |
p_vec = np.array([np.clip(p_range(d) + float(acc_bump_abs), 0.0, 1.0) for d in dist_mid])
|
| 263 |
+
|
| 264 |
mu = float(np.sum(lam_bins * p_vec))
|
| 265 |
var = float(np.sum(lam_bins * p_vec * (1.0 - p_vec)))
|
| 266 |
return mu, var
|
| 267 |
|
| 268 |
# ====================
|
| 269 |
+
# Market helpers (partial availability safe)
|
| 270 |
# ====================
|
| 271 |
|
| 272 |
+
def american_to_prob(odds):
|
| 273 |
+
"""Convert American odds (e.g. -120 / +110) to raw implied probability in [0,1]."""
|
| 274 |
+
try:
|
| 275 |
+
o = float(odds)
|
| 276 |
+
except Exception:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
return np.nan
|
| 278 |
+
if o < 0:
|
| 279 |
+
return (-o) / ((-o) + 100.0)
|
| 280 |
+
else:
|
| 281 |
+
return 100.0 / (o + 100.0)
|
| 282 |
|
| 283 |
+
def devig_over_prob(p_over_raw, p_under_raw):
|
| 284 |
+
"""Remove vig using simple normalization; both inputs in [0,1]."""
|
| 285 |
+
if not np.isfinite(p_over_raw) or not np.isfinite(p_under_raw):
|
|
|
|
| 286 |
return np.nan
|
| 287 |
+
s = p_over_raw + p_under_raw
|
| 288 |
+
if s <= 0:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
return np.nan
|
| 290 |
+
return p_over_raw / s
|
| 291 |
|
| 292 |
+
def _resolve_odds_columns(base, suf, df_cols):
|
| 293 |
+
"""
|
| 294 |
+
Try a few patterns for (line, over, under) with/without suffix adjacency.
|
| 295 |
+
Returns (line_col, over_col, under_col) or (None,None,None).
|
| 296 |
+
"""
|
| 297 |
+
candidates = [
|
| 298 |
+
(f"{base}_line{suf}", f"{base}_over_odds{suf}", f"{base}_under_odds{suf}"),
|
| 299 |
+
(f"{base}{suf}_line", f"{base}{suf}_over_odds", f"{base}{suf}_under_odds"),
|
| 300 |
+
(f"{base}_line", f"{base}_over_odds", f"{base}_under_odds"),
|
| 301 |
+
]
|
| 302 |
+
for L, O, U in candidates:
|
| 303 |
+
if L in df_cols and O in df_cols and U in df_cols:
|
| 304 |
+
return L, O, U
|
| 305 |
+
return None, None, None
|
| 306 |
+
|
| 307 |
+
def _invert_poisson_tail_at_line(L, p_over, max_lambda=10.0, tol=1e-5, iters=60):
|
| 308 |
+
"""
|
| 309 |
+
Solve for lambda s.t. P(X >= ceil(L)) = p_over with X~Poisson(lambda).
|
| 310 |
+
"""
|
| 311 |
+
k = int(np.ceil(L))
|
| 312 |
+
lo, hi = 0.0, max_lambda
|
| 313 |
+
def tail(lam): return 1.0 - poisson.cdf(k-1, lam)
|
| 314 |
+
while tail(hi) > p_over and hi < 200:
|
| 315 |
+
hi *= 2.0
|
| 316 |
+
for _ in range(iters):
|
| 317 |
+
mid = 0.5 * (lo + hi)
|
| 318 |
+
t = tail(mid)
|
| 319 |
+
if abs(t - p_over) < tol:
|
| 320 |
+
return mid
|
| 321 |
+
if t > p_over:
|
| 322 |
+
lo = mid
|
| 323 |
else:
|
| 324 |
+
hi = mid
|
| 325 |
+
return 0.5 * (lo + hi)
|
| 326 |
+
|
| 327 |
+
def _market_mean_from_rows(slice_df, stat_key, timeframe, is_yards):
|
| 328 |
+
"""
|
| 329 |
+
Build market-implied mean using ONLY rows that have (line, over, under).
|
| 330 |
+
Returns (market_mean or np.nan, n_rows_used).
|
| 331 |
+
"""
|
| 332 |
+
suf = {"Game":"", "1H":"_1h", "1Q":"_1q"}[timeframe]
|
| 333 |
+
base = STAT_MAP.get(stat_key, stat_key)
|
| 334 |
+
L_col, O_col, U_col = _resolve_odds_columns(base, suf, slice_df.columns)
|
| 335 |
+
if L_col is None:
|
| 336 |
+
return np.nan, 0
|
| 337 |
+
|
| 338 |
+
usable = slice_df[[L_col, O_col, U_col]].copy()
|
| 339 |
+
usable = usable.replace([np.inf, -np.inf], np.nan).dropna(subset=[L_col, O_col, U_col])
|
| 340 |
+
if usable.empty:
|
| 341 |
+
return np.nan, 0
|
| 342 |
+
|
| 343 |
+
means = []
|
| 344 |
+
if is_yards:
|
| 345 |
+
# Estimate sigma from historical distribution of the stat/timeframe in the slice
|
| 346 |
+
stat_col = f"{base}{suf}"
|
| 347 |
+
series = pd.to_numeric(slice_df.get(stat_col, pd.Series(dtype=float)), errors="coerce").dropna()
|
| 348 |
+
sigma = float(series.std(ddof=1)) if len(series) > 1 else 1.0
|
| 349 |
+
for _, row in usable.iterrows():
|
| 350 |
+
try:
|
| 351 |
+
L = float(row[L_col])
|
| 352 |
+
p_over_raw = american_to_prob(row[O_col])
|
| 353 |
+
p_under_raw = american_to_prob(row[U_col])
|
| 354 |
+
p_over = devig_over_prob(p_over_raw, p_under_raw)
|
| 355 |
+
if not (np.isfinite(L) and 0.0 < p_over < 1.0):
|
| 356 |
+
continue
|
| 357 |
+
mu = L + sigma * float(sps.norm.ppf(np.clip(p_over, 1e-6, 1-1e-6)))
|
| 358 |
+
means.append(mu)
|
| 359 |
+
except Exception:
|
| 360 |
+
continue
|
| 361 |
+
else:
|
| 362 |
+
for _, row in usable.iterrows():
|
| 363 |
+
try:
|
| 364 |
+
L = float(row[L_col])
|
| 365 |
+
p_over_raw = american_to_prob(row[O_col])
|
| 366 |
+
p_under_raw = american_to_prob(row[U_col])
|
| 367 |
+
p_over = devig_over_prob(p_over_raw, p_under_raw)
|
| 368 |
+
if not (np.isfinite(L) and 0.0 < p_over < 1.0):
|
| 369 |
+
continue
|
| 370 |
+
lam = _invert_poisson_tail_at_line(L, p_over)
|
| 371 |
+
means.append(lam)
|
| 372 |
+
except Exception:
|
| 373 |
+
continue
|
| 374 |
+
|
| 375 |
+
if len(means) == 0:
|
| 376 |
+
return np.nan, 0
|
| 377 |
+
return float(np.mean(means)), len(means)
|
| 378 |
|
| 379 |
# ====================
|
| 380 |
# Globals
|
|
|
|
| 382 |
df = None
|
| 383 |
model_weights_df = None
|
| 384 |
temp_df = None
|
|
|
|
| 385 |
|
| 386 |
# =========================
|
| 387 |
# Loading + preprocessing
|
|
|
|
| 391 |
|
| 392 |
def load_and_prepare_data(file):
|
| 393 |
"""Populate globals and return UI choices."""
|
| 394 |
+
global df, model_weights_df, temp_df
|
|
|
|
|
|
|
| 395 |
if file is None:
|
| 396 |
return (gr.update(choices=[], value=None),
|
| 397 |
gr.update(choices=[], value=[]),
|
| 398 |
gr.update(choices=[], value=None),
|
| 399 |
+
gr.update(choices=[], value=None),
|
| 400 |
"Upload the CSV to begin.")
|
| 401 |
|
|
|
|
| 402 |
raw = pd.read_csv(file.name)
|
| 403 |
+
raw.columns = [c.strip() for c in raw.columns]
|
|
|
|
| 404 |
df = raw.copy()
|
| 405 |
temp_df = raw.copy()
|
| 406 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
teams = sorted(df.get("player_team", pd.Series(dtype=str)).dropna().astype(str).str.strip().replace("", np.nan).dropna().unique().tolist())
|
| 408 |
players = sorted(df.get("player", pd.Series(dtype=str)).dropna().astype(str).str.strip().replace("", np.nan).dropna().unique().tolist())
|
| 409 |
positions = sorted(df.get("position", pd.Series(dtype=str)).dropna().astype(str).str.strip().replace("", np.nan).dropna().unique().tolist())
|
| 410 |
|
| 411 |
+
# Precompute per-player model weights (non-FG; same as before)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 412 |
rows = []
|
| 413 |
for player in players:
|
| 414 |
pdata = df[df["player"] == player]
|
|
|
|
| 428 |
model_weights_df = pd.DataFrame(rows)
|
| 429 |
|
| 430 |
msg = "✅ File loaded."
|
| 431 |
+
if "player_team" not in df.columns:
|
| 432 |
+
msg = "⚠️ 'player_team' not found. Team tab will not filter correctly until your exporter adds it."
|
| 433 |
|
| 434 |
return (gr.update(choices=teams, value=(teams[0] if teams else None)),
|
| 435 |
gr.update(choices=players, value=[]),
|
| 436 |
gr.update(choices=positions, value=None),
|
| 437 |
+
gr.update(choices=positions, value=None), # also wire league_pos choices
|
| 438 |
msg)
|
| 439 |
|
| 440 |
# =========================
|
| 441 |
+
# Player tab (position filter + league average + K-ball knobs + Market)
|
| 442 |
# =========================
|
| 443 |
+
def estimate(player, stat_key, timeframe, mean_sel, custom_mean, vig, show_extremes,
|
| 444 |
range_gain_yds, acc_bump_abs, long_propensity):
|
| 445 |
if df is None or model_weights_df is None:
|
| 446 |
return "⚠️ Upload the CSV.", pd.DataFrame(), {"Poisson":1,"Negative Binomial":0,"Gamma-Poisson":0,"Log-Normal":0}, pd.Series(dtype=float)
|
|
|
|
| 450 |
|
| 451 |
stat_col = csv_col_for(stat_key, timeframe)
|
| 452 |
pdata = df[df['player'] == player]
|
| 453 |
+
raw_data = pd.to_numeric(pdata.get(stat_col, pd.Series(dtype=float)), errors="coerce").dropna()
|
| 454 |
+
|
| 455 |
+
# Determine type
|
| 456 |
+
is_yards = stat_key in {"pass_yds", "rush_yds", "rec_yds"}
|
| 457 |
|
| 458 |
+
# Historical/K-ball mean/variance
|
| 459 |
+
hist_mean = raw_data.mean() if len(raw_data) else 0.0
|
| 460 |
+
variance = raw_data.var() if len(raw_data) else 0.0
|
| 461 |
|
| 462 |
+
# K-ball: ONLY if FG & kicker
|
| 463 |
is_kicker = (pdata.get("position") == "K").any()
|
| 464 |
if stat_key == "fg_made" and is_kicker:
|
| 465 |
+
mu_k, var_k = fg_kball_mean_var_for_slice(pdata, timeframe,
|
| 466 |
+
range_gain_yds, acc_bump_abs, long_propensity)
|
| 467 |
+
hist_mean, variance = mu_k, var_k
|
| 468 |
+
|
| 469 |
+
# Market-implied mean from available rows (partial OK)
|
| 470 |
+
market_mean, n_mkt_rows = _market_mean_from_rows(pdata, stat_key, timeframe, is_yards)
|
| 471 |
+
|
| 472 |
+
# Choose projection: keep Historical (or Custom). If you want to auto-use market, flip logic here.
|
| 473 |
+
mean_to_use = hist_mean
|
| 474 |
+
source_label = "Historical"
|
| 475 |
+
if mean_sel == "Use Custom Projection" and (custom_mean is not None):
|
| 476 |
+
mean_to_use = float(custom_mean)
|
| 477 |
+
source_label = "Custom"
|
| 478 |
+
# Example: prefer market when available
|
| 479 |
+
# elif np.isfinite(market_mean):
|
| 480 |
+
# mean_to_use = market_mean
|
| 481 |
+
# source_label = f"Market (n={n_mkt_rows})"
|
| 482 |
+
|
| 483 |
+
# weights (for plots; Poisson-only fallback OK)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 484 |
try:
|
| 485 |
wrow = model_weights_df[model_weights_df['player'] == player].iloc[0]
|
| 486 |
weights = {
|
|
|
|
| 493 |
weights = {"Poisson":1.0, "Negative Binomial":0.0, "Gamma-Poisson":0.0, "Log-Normal":0.0}
|
| 494 |
|
| 495 |
ms = milestones_for(stat_key, timeframe)
|
|
|
|
|
|
|
| 496 |
|
| 497 |
+
# Probabilities
|
| 498 |
+
probs_all = calculate_probability(mean_to_use, variance, weights, ms, raw_data)
|
| 499 |
if show_extremes == "Hide Extreme Probabilities":
|
| 500 |
probs = {k: v for k, v in probs_all.items() if 0.005 <= v <= 0.99}
|
| 501 |
if len(probs) == 0:
|
|
|
|
| 506 |
games = pdata['game_id'].nunique() if "game_id" in pdata.columns else len(pdata)
|
| 507 |
hit_rates = []
|
| 508 |
for m in ms:
|
| 509 |
+
hits = (raw_data >= m).sum() if len(raw_data) else 0
|
| 510 |
hit_rates.append(round((hits / games) * 100, 2) if games > 0 else 0.0)
|
| 511 |
|
| 512 |
filtered = list(probs.keys())
|
| 513 |
filt_hr = [hr for m, hr in zip(ms, hit_rates) if f"{m}+" in filtered]
|
| 514 |
|
|
|
|
| 515 |
table = pd.DataFrame({
|
| 516 |
"Milestone": filtered,
|
| 517 |
"Probability": [f"{v * 100:.2f}%" for v in probs.values()],
|
|
|
|
| 520 |
"Hit Rate": [f"{x:.2f}%" for x in filt_hr],
|
| 521 |
})
|
| 522 |
|
| 523 |
+
market_str = ("n/a" if not np.isfinite(market_mean) else f"{market_mean:.2f}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 524 |
summary_md = f"""
|
| 525 |
### 📊 {player} — {STAT_LABELS.get(stat_key, stat_key)} ({timeframe})
|
| 526 |
- Games in sample: **{games}**
|
| 527 |
+
- Mean used: **{mean_to_use:.2f}** ({source_label}{' + K-ball' if (stat_key=='fg_made' and is_kicker and source_label=='Historical') else ''})
|
| 528 |
+
- Market mean (if available): **{market_str}** (rows used: {n_mkt_rows})
|
| 529 |
+
- Variance: **{variance:.2f}**
|
| 530 |
+
- Vig: **{vig:.2f}%** — Extremes: **{'Shown' if show_extremes == 'Show All' else 'Hidden (fallback if empty)'}**
|
|
|
|
|
|
|
|
|
|
| 531 |
"""
|
| 532 |
+
return summary_md, table, weights, raw_data
|
| 533 |
|
| 534 |
+
def league_avg_by_position(position, stat_key, timeframe, vig, range_gain_yds, acc_bump_abs, long_propensity):
|
|
|
|
|
|
|
| 535 |
if df is None:
|
| 536 |
return pd.DataFrame(), "Upload a CSV first."
|
| 537 |
if not position:
|
|
|
|
| 541 |
if slice_df.empty:
|
| 542 |
return pd.DataFrame(), f"No rows for position {position}."
|
| 543 |
|
|
|
|
| 544 |
ms = milestones_for(stat_key, timeframe)
|
| 545 |
+
is_yards = stat_key in {"pass_yds", "rush_yds", "rec_yds"}
|
| 546 |
|
| 547 |
+
# Historical mean (K-ball only if K & fg_made)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 548 |
if stat_key == "fg_made" and position == "K":
|
| 549 |
+
mu_hist, _ = fg_kball_mean_var_for_slice(slice_df, timeframe,
|
| 550 |
+
range_gain_yds, acc_bump_abs, long_propensity)
|
| 551 |
+
hist_mean = mu_hist
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 552 |
else:
|
| 553 |
+
series = pd.to_numeric(slice_df.get(csv_col_for(stat_key, timeframe), pd.Series(dtype=float)), errors="coerce").dropna()
|
| 554 |
+
hist_mean = series.mean() if len(series) else 0.0
|
| 555 |
+
|
| 556 |
+
# Market mean from all rows in the slice that have odds (partial OK)
|
| 557 |
+
market_mean, n_mkt_rows = _market_mean_from_rows(slice_df, stat_key, timeframe, is_yards)
|
| 558 |
+
|
| 559 |
+
# Choose projection (keep historical by default; you can change to market/blend)
|
| 560 |
+
mean_to_use = hist_mean
|
| 561 |
+
# Example blend if you want:
|
| 562 |
+
# if np.isfinite(market_mean):
|
| 563 |
+
# mean_to_use = 0.6*market_mean + 0.4*hist_mean
|
| 564 |
|
| 565 |
+
probs = {f"{m}+": round(1 - poisson.cdf(m-1, mean_to_use), 4) for m in ms}
|
|
|
|
| 566 |
table = pd.DataFrame({
|
| 567 |
"Milestone": list(probs.keys()),
|
| 568 |
"Probability": [f"{p*100:.2f}%" for p in probs.values()],
|
|
|
|
| 595 |
for player, pdata in work.groupby("player", dropna=True):
|
| 596 |
series = pd.to_numeric(pdata.get(stat_col, pd.Series(dtype=float)), errors="coerce").dropna()
|
| 597 |
games = pdata['game_id'].nunique() if "game_id" in pdata.columns else len(pdata)
|
|
|
|
| 598 |
mean = series.mean() if len(series) else 0.0
|
|
|
|
|
|
|
|
|
|
|
|
|
| 599 |
for m in milestones:
|
| 600 |
+
prob_hist = float((series >= m).sum()) / float(games) if (games > 0 and len(series)) else (1 - poisson.cdf(m-1, mean))
|
|
|
|
| 601 |
rows.append({
|
| 602 |
"Player": player,
|
| 603 |
"Games": games,
|
| 604 |
+
"Milestone": f"{m}+",
|
| 605 |
+
"Prob": round(prob_hist, 4),
|
| 606 |
+
"True Odds": to_american_odds(prob_hist),
|
| 607 |
+
"Odds w/ Vig": to_american_odds(apply_vig(prob_hist, vig)),
|
| 608 |
"Mean": round(mean, 2),
|
| 609 |
})
|
| 610 |
|
|
|
|
| 629 |
|
| 630 |
# Populate choices on upload
|
| 631 |
def on_file(f):
|
| 632 |
+
team_upd, players_upd, pos_upd, pos_upd_league, msg = load_and_prepare_data(f)
|
| 633 |
+
return team_upd, players_upd, pos_upd, pos_upd_league, msg
|
| 634 |
|
| 635 |
with gr.Tabs():
|
| 636 |
|
|
|
|
| 655 |
with gr.Row():
|
| 656 |
p_stat = gr.Dropdown(label="Stat", choices=STAT_OPTIONS, value="rec_yds")
|
| 657 |
p_time = gr.Radio(list(TIMEFRAMES.keys()), value="Game", label="Timeframe")
|
|
|
|
|
|
|
| 658 |
with gr.Row():
|
| 659 |
+
mean_sel = gr.Radio(["Use Per-Game Average", "Use Custom Projection"], label="Projection Source", value="Use Per-Game Average")
|
| 660 |
+
custom_proj = gr.Number(label="Custom Mean (if selected)", precision=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 661 |
p_vig = gr.Number(label="Vig (%)", value=4.92, precision=2)
|
| 662 |
extreme_toggle = gr.Radio(["Hide Extreme Probabilities", "Show All"], label="Extreme Probabilities", value="Hide Extreme Probabilities")
|
| 663 |
|
|
|
|
| 684 |
league_range_gain = gr.Slider(0, 10, value=7, step=1, label="K-ball: Range gain (yds)")
|
| 685 |
league_acc_bump = gr.Slider(0, 0.10, value=0.02, step=0.01, label="K-ball: Accuracy bump (abs prob)")
|
| 686 |
league_long_prop = gr.Slider(0, 0.5, value=0.10, step=0.05, label="K-ball: Long-attempt propensity")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 687 |
league_btn = gr.Button("Compute League Avg", elem_classes=["green-button"])
|
| 688 |
league_title = gr.Markdown(visible=False)
|
| 689 |
league_table = gr.Dataframe(visible=False, label="League Avg Milestones")
|
| 690 |
|
| 691 |
+
# Hook file load — also wires league_pos choices now
|
| 692 |
+
file_in.change(on_file, inputs=[file_in], outputs=[team_dd, players_ms, position_dd, league_pos, status])
|
| 693 |
|
| 694 |
# Team roster subset choices
|
| 695 |
def on_team_change(team):
|
|
|
|
| 723 |
outputs=[team_title, team_table_out])
|
| 724 |
|
| 725 |
# Player run (multi-player; charts only when a single player is selected)
|
| 726 |
+
def run_players(pos, players, stat, timeframe, mean_sel_v, custom_mean, vig, extreme_v, rg, acc, lp):
|
| 727 |
if df is None:
|
| 728 |
return (gr.update(visible=False), gr.update(visible=False),
|
| 729 |
gr.update(visible=False), gr.update(visible=False),
|
|
|
|
| 745 |
first_raw = None
|
| 746 |
|
| 747 |
for i, player in enumerate(players):
|
| 748 |
+
summary_md, table, weights, raw = estimate(player, stat, timeframe, mean_sel_v, custom_mean, vig, extreme_v,
|
| 749 |
+
rg, acc, lp)
|
| 750 |
t = table.copy()
|
| 751 |
t.insert(0, "Player", player)
|
| 752 |
all_rows.append(t)
|
|
|
|
| 759 |
if len(players) == 1 and first_weights is not None:
|
| 760 |
pie = generate_pie(first_weights, players[0], stat)
|
| 761 |
raw = first_raw if first_raw is not None else pd.Series(dtype=float)
|
|
|
|
| 762 |
hist = generate_histogram(
|
| 763 |
raw,
|
| 764 |
first_weights,
|
| 765 |
+
custom_mean if mean_sel_v == "Use Custom Projection" and custom_mean is not None else (raw.mean() if len(raw)>0 else 0.0),
|
| 766 |
+
raw.var() if len(raw)>0 else 0.0
|
|
|
|
| 767 |
)
|
| 768 |
recent = temp_df[temp_df['player'] == players[0]].copy()
|
| 769 |
if "game_id" in recent.columns and "gameday" in recent.columns:
|
|
|
|
| 786 |
gr.update(visible=False))
|
| 787 |
|
| 788 |
player_btn.click(run_players,
|
| 789 |
+
inputs=[position_dd, players_ms, p_stat, p_time, mean_sel, custom_proj, p_vig, extreme_toggle,
|
|
|
|
| 790 |
range_gain, acc_bump, long_prop],
|
| 791 |
outputs=[p_summary, p_table, p_pie, p_hist, recent_games])
|
| 792 |
|
| 793 |
# League average run (by position)
|
| 794 |
+
def run_league_avg(pos, stat, timeframe, vig, rg, acc, lp):
|
| 795 |
+
table, title = league_avg_by_position(pos, stat, timeframe, vig, rg, acc, lp)
|
| 796 |
return gr.update(value=title, visible=True), gr.update(value=table, visible=True)
|
| 797 |
league_btn.click(run_league_avg,
|
| 798 |
inputs=[league_pos, league_stat, league_time, league_vig,
|
| 799 |
+
league_range_gain, league_acc_bump, league_long_prop],
|
|
|
|
| 800 |
outputs=[league_title, league_table])
|
| 801 |
|
| 802 |
demo.launch()
|