tylerdevos commited on
Commit
ecb8eec
Β·
verified Β·
1 Parent(s): 5fb339e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +281 -317
app.py CHANGED
@@ -1,9 +1,9 @@
1
  import pandas as pd
2
  import numpy as np
3
  import matplotlib.pyplot as plt
4
- import re
5
  from scipy.stats import poisson, nbinom, gamma, lognorm, kstest
6
  import gradio as gr
 
7
 
8
  # =========================
9
  # Stat mapping & milestones
@@ -107,7 +107,7 @@ 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 generality)
111
  # ======================
112
  def bayesian_model_weights(poisson_ll, nb_ll, gamma_ll, lognorm_ll):
113
  log_likelihoods = np.array([poisson_ll, nb_ll, gamma_ll, lognorm_ll])
@@ -190,53 +190,6 @@ def generate_histogram(raw_data, weights, mean, variance):
190
  ax.legend()
191
  return fig
192
 
193
- # ====================
194
- # Recency weighting helpers
195
- # ====================
196
- def _row_recency_order(pdata: pd.DataFrame) -> pd.Series:
197
- """
198
- Assign 'age' (0 = newest, 1 = next newest, ...) per row so we can do half-life weighting.
199
- Tries (season, week, gameday); falls back to index order.
200
- """
201
- temp = pdata.copy()
202
- # Build a sortable key
203
- if "season" in temp.columns and "week" in temp.columns:
204
- temp = temp.sort_values(by=["season", "week", "game_id"] if "game_id" in temp.columns else ["season","week"])
205
- elif "gameday" in temp.columns:
206
- temp = temp.sort_values(by=["gameday"])
207
- else:
208
- temp = temp.sort_index()
209
-
210
- # newest should have age 0
211
- order = np.arange(len(temp))[::-1]
212
- ages = pd.Series(order, index=temp.index, dtype=float)
213
- return ages
214
-
215
- def _weights_from_ages(ages: pd.Series, mode: str = "Exponential Half-Life", half_life_games: float = 6.0) -> np.ndarray:
216
- if ages is None or len(ages) == 0:
217
- return np.array([])
218
- if mode == "Uniform":
219
- w = np.ones(len(ages))
220
- else:
221
- # Exponential decay with half-life in "games"
222
- lam = np.log(2.0) / max(half_life_games, 0.1)
223
- w = np.exp(-lam * ages.values.astype(float))
224
- s = np.sum(w)
225
- if s <= 0:
226
- return np.ones(len(ages)) / len(ages)
227
- return w / s
228
-
229
- def _weighted_mean_var(values: pd.Series, ages: pd.Series, mode: str, half_life_games: float):
230
- if values is None or len(values) == 0:
231
- return 0.0, 0.0
232
- ages = ages.reindex(values.index)
233
- w = _weights_from_ages(ages, mode, half_life_games)
234
- if len(w) != len(values):
235
- return float(values.mean()), float(values.var() if len(values) > 1 else 0.0)
236
- mu = float(np.sum(w * values.values))
237
- var = float(np.sum(w * (values.values - mu) ** 2))
238
- return mu, var
239
-
240
  # ====================
241
  # FG K-ball model helpers (used ONLY for fg_made and kickers)
242
  # ====================
@@ -278,7 +231,7 @@ def _fg_lambda_p_from_slice(pdata: pd.DataFrame, timeframe: str):
278
  atts = np.array([pd.to_numeric(pdata.get(c, pd.Series(dtype=float)), errors="coerce").fillna(0).sum() for c in att_cols])
279
  made = np.array([pd.to_numeric(pdata.get(c, pd.Series(dtype=float)), errors="coerce").fillna(0).sum() for c in made_cols])
280
 
281
- games = _games_played(pdata)
282
  games = games if games > 0 else 1
283
  lam_bins = atts / games
284
 
@@ -290,26 +243,151 @@ def _fg_lambda_p_from_slice(pdata: pd.DataFrame, timeframe: str):
290
  def fg_kball_mean_var_for_slice(pdata: pd.DataFrame, timeframe: str,
291
  range_gain_yds=7.0, acc_bump_abs=0.02, long_propensity=0.10):
292
  lam_bins, (a, b, R), dist_mid = _fg_lambda_p_from_slice(pdata, timeframe)
293
-
294
  # Tilt long attempts
295
  tilt = np.ones_like(lam_bins)
296
  long_idx = np.array([i for i,(k,_) in enumerate(FG_BINS) if k in ("55_59","60_plus")])
297
  tilt[long_idx] = 1.0 + float(long_propensity)
298
  lam_bins = lam_bins * tilt
299
-
300
  # Keep total attempts per game constant after tilting
301
  total = lam_bins.sum()
302
  if total > 1e-12:
303
  lam_bins = lam_bins * (total / lam_bins.sum())
304
-
305
  # Range + accuracy bump
306
  def p_range(d): return _p_make(a, b, R + float(range_gain_yds), d)
307
  p_vec = np.array([np.clip(p_range(d) + float(acc_bump_abs), 0.0, 1.0) for d in dist_mid])
308
-
309
  mu = float(np.sum(lam_bins * p_vec))
310
  var = float(np.sum(lam_bins * p_vec * (1.0 - p_vec)))
311
  return mu, var
312
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313
  # ====================
314
  # Globals
315
  # ====================
@@ -323,11 +401,6 @@ temp_df = None
323
  def csv_col_for(stat_key: str, timeframe_key: str) -> str:
324
  return f"{STAT_MAP.get(stat_key, stat_key)}{TIMEFRAMES.get(timeframe_key, '')}"
325
 
326
- def _games_played(pdata: pd.DataFrame) -> int:
327
- if "game_id" in pdata.columns:
328
- return int(pdata["game_id"].nunique())
329
- return len(pdata)
330
-
331
  def load_and_prepare_data(file):
332
  """Populate globals and return UI choices."""
333
  global df, model_weights_df, temp_df
@@ -338,8 +411,8 @@ def load_and_prepare_data(file):
338
  gr.update(choices=[], value=None),
339
  "Upload the CSV to begin.")
340
 
341
- raw = pd.read_csv(file.name, header=0)
342
- raw.columns = [str(c).strip() for c in raw.columns]
343
  df = raw.copy()
344
  temp_df = raw.copy()
345
 
@@ -351,7 +424,7 @@ def load_and_prepare_data(file):
351
  rows = []
352
  for player in players:
353
  pdata = df[df["player"] == player]
354
- row = {"player": player, "games_played": _games_played(pdata)}
355
  for stat_key in STAT_OPTIONS:
356
  stat_col = csv_col_for(stat_key, "Game")
357
  if stat_col not in pdata.columns:
@@ -373,123 +446,36 @@ def load_and_prepare_data(file):
373
  return (gr.update(choices=teams, value=(teams[0] if teams else None)),
374
  gr.update(choices=players, value=[]),
375
  gr.update(choices=positions, value=None),
376
- gr.update(choices=positions, value=None), # mirror into league position
377
  msg)
378
 
379
  # =========================
380
- # Market helpers (robust column finder + recency weighting)
381
- # =========================
382
- def _find_market_cols(stat_key: str, timeframe: str):
383
- """
384
- Try to find <stat>_line and <stat>_odds columns for the timeframe.
385
- Very tolerant: matches columns that contain the base token and 'line'/'odds' + suffix.
386
- """
387
- base = STAT_MAP.get(stat_key, stat_key) # e.g., receptions
388
- suf = TIMEFRAMES.get(timeframe, "")
389
- cand_line = None
390
- cand_odds = None
391
-
392
- cols = [c for c in df.columns]
393
- low = {c: c.lower() for c in cols}
394
-
395
- # regex patterns like r"(receptions).*line.*(_1h)?$"
396
- pat_line = re.compile(rf"{re.escape(base)}.*line{re.escape(suf)}$", re.IGNORECASE)
397
- pat_odds = re.compile(rf"{re.escape(base)}.*odds{re.escape(suf)}$", re.IGNORECASE)
398
-
399
- # first pass: strict suffix match
400
- for c in cols:
401
- lc = low[c]
402
- if pat_line.search(lc):
403
- cand_line = c
404
- if pat_odds.search(lc):
405
- cand_odds = c
406
-
407
- # second pass: looser contains + suffix anywhere
408
- if cand_line is None:
409
- for c in cols:
410
- lc = low[c]
411
- if (base in lc) and ("line" in lc) and (suf in lc):
412
- cand_line = c
413
- break
414
- if cand_odds is None:
415
- for c in cols:
416
- lc = low[c]
417
- if (base in lc) and ("odds" in lc) and (suf in lc):
418
- cand_odds = c
419
- break
420
-
421
- # final very loose fallback: just base + "line"/"odds"
422
- if cand_line is None:
423
- for c in cols:
424
- lc = low[c]
425
- if (base in lc) and ("line" in lc):
426
- cand_line = c
427
- break
428
- if cand_odds is None:
429
- for c in cols:
430
- lc = low[c]
431
- if (base in lc) and ("odds" in lc):
432
- cand_odds = c
433
- break
434
-
435
- return cand_line, cand_odds
436
-
437
- def compute_market_mean(pdata: pd.DataFrame, stat_key: str, timeframe: str,
438
- weighting_mode: str, half_life_games: float):
439
- """
440
- Weighted mean of market 'line' for the slice using selected recency scheme.
441
- Accepts missing rows; uses whatever exists. Returns (mean, n_rows_used).
442
- """
443
- line_col, _ = _find_market_cols(stat_key, timeframe)
444
- if line_col is None or line_col not in pdata.columns:
445
- return None, 0
446
-
447
- vals = pd.to_numeric(pdata[line_col], errors="coerce").dropna()
448
- if vals.empty:
449
- return None, 0
450
-
451
- ages = _row_recency_order(pdata).reindex(vals.index)
452
- mu, _ = _weighted_mean_var(vals, ages, mode=weighting_mode, half_life_games=half_life_games)
453
- return float(mu), int(vals.shape[0])
454
-
455
  # =========================
456
- # League averages (for shrink)
457
- # =========================
458
- def league_hist_mean_var(position: str, stat_key: str, timeframe: str,
459
- hist_weighting_mode: str, hist_half_life_games: float):
460
- """
461
- League (by position) historical mean/variance using the same historical weighting config.
462
- For K + fg_made, use the K-ball mean/var over the position slice.
463
- """
464
  if df is None or not position:
465
- return 0.0, 0.0
466
-
467
- slice_df = df[df.get("position") == position].copy()
468
  if slice_df.empty:
469
- return 0.0, 0.0
470
 
 
471
  if stat_key == "fg_made" and position == "K":
472
- mu, var = fg_kball_mean_var_for_slice(slice_df, timeframe)
473
- return float(mu), float(var)
474
 
475
  stat_col = csv_col_for(stat_key, timeframe)
476
- vals = pd.to_numeric(slice_df.get(stat_col, pd.Series(dtype=float)), errors="coerce").dropna()
477
- if vals.empty:
478
- return 0.0, 0.0
479
-
480
- ages = _row_recency_order(slice_df).reindex(vals.index)
481
- mu, var = _weighted_mean_var(vals, ages, mode=hist_weighting_mode, half_life_games=hist_half_life_games)
482
- return float(mu), float(var)
483
 
484
  # =========================
485
- # Player tab (position filter + league average + K-ball knobs + market)
486
  # =========================
487
- def estimate(player, stat_key, timeframe,
488
- mean_sel, custom_mean, mkt_blend_w, vig, show_extremes,
489
- mkt_weighting_mode, mkt_half_life_games,
490
- hist_weighting_mode, hist_half_life_games,
491
  range_gain_yds, acc_bump_abs, long_propensity,
492
- enable_league_shrink, min_games_for_shrink):
 
 
493
  if df is None or model_weights_df is None:
494
  return "⚠️ Upload the CSV.", pd.DataFrame(), {"Poisson":1,"Negative Binomial":0,"Gamma-Poisson":0,"Log-Normal":0}, pd.Series(dtype=float)
495
 
@@ -497,80 +483,69 @@ def estimate(player, stat_key, timeframe,
497
  return f"⚠️ Player '{player}' not found.", pd.DataFrame(), {"Poisson":1,"Negative Binomial":0,"Gamma-Poisson":0,"Log-Normal":0}, pd.Series(dtype=float)
498
 
499
  stat_col = csv_col_for(stat_key, timeframe)
500
- pdata = df[df['player'] == player].copy()
 
 
 
 
 
 
501
 
502
- # ---------- Historical component ----------
503
  is_kicker = (pdata.get("position") == "K").any()
504
- hist_source = "Historical"
505
  if stat_key == "fg_made" and is_kicker:
506
- # K-ball for kickers
507
- hist_mean, variance = fg_kball_mean_var_for_slice(
508
- pdata, timeframe,
509
- range_gain_yds, acc_bump_abs, long_propensity
510
- )
511
- hist_source = "Historical (K-ball)"
512
- raw_data = pd.to_numeric(pdata.get(stat_col, pd.Series(dtype=float)), errors="coerce").dropna()
513
- else:
514
- # standard historical with recency weighting
515
- series = pd.to_numeric(pdata.get(stat_col, pd.Series(dtype=float)), errors="coerce").dropna()
516
- ages = _row_recency_order(pdata).reindex(series.index)
517
- hist_mean, variance = _weighted_mean_var(series, ages, mode=hist_weighting_mode, half_life_games=hist_half_life_games)
518
- raw_data = series
519
-
520
- # League shrink for tiny samples (applies to historical path only)
521
- games = _games_played(pdata)
522
- if enable_league_shrink and games < int(min_games_for_shrink):
523
- Gstar = max(int(min_games_for_shrink), 1)
524
- alpha = min(1.0, max(0.0, (Gstar - games) / float(Gstar)))
525
-
526
- # infer player's position
527
- pos_series = pdata.get("position")
528
- player_pos = None
529
- if pos_series is not None and pos_series.notna().any():
530
- player_pos = str(pos_series.dropna().iloc[0]).strip()
531
-
532
- if player_pos:
533
- league_mu, league_var = league_hist_mean_var(
534
- player_pos, stat_key, timeframe,
535
- hist_weighting_mode, hist_half_life_games
536
- )
537
- hist_mean = (1 - alpha) * float(hist_mean) + alpha * float(league_mu)
538
- variance = (1 - alpha) * float(variance) + alpha * float(league_var)
539
- hist_source += f" β†’ shrunk to {player_pos} league avg (Ξ±={alpha:.2f}, G={games}/{Gstar})"
540
-
541
- # ---------- Market component ----------
542
- mkt_mean, mkt_rows = compute_market_mean(
543
- pdata, stat_key, timeframe,
544
- weighting_mode=mkt_weighting_mode,
545
- half_life_games=mkt_half_life_games
546
- )
547
 
548
- # ---------- Choose projection ----------
549
- if mean_sel == "Use Custom Projection" and (custom_mean is not None):
550
- proj_mean = float(custom_mean)
551
- proj_source = "Custom"
552
- elif mean_sel == "Use Market Mean (if available)":
553
- if mkt_mean is not None:
554
- proj_mean = float(mkt_mean)
555
- proj_source = f"Market (n={mkt_rows})"
556
- else:
557
- proj_mean = float(hist_mean)
558
- proj_source = f"{hist_source} (market n/a)"
559
- elif mean_sel == "Historical + Market Blend":
560
- if mkt_mean is not None:
561
- w = max(0.0, min(1.0, float(mkt_blend_w)))
562
- proj_mean = (1 - w) * float(hist_mean) + w * float(mkt_mean)
563
- proj_source = f"Blend: {(1-w):.2f}Γ—{hist_source} + {w:.2f}Γ—Market(n={mkt_rows})"
564
- else:
565
- proj_mean = float(hist_mean)
566
- proj_source = f"{hist_source} (market n/a)"
 
 
567
  else:
568
- proj_mean = float(hist_mean)
569
- proj_source = hist_source
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
570
 
571
  ms = milestones_for(stat_key, timeframe)
 
572
 
573
- probs_all = calculate_probability(proj_mean, variance, {"Poisson":1,"Negative Binomial":0,"Gamma-Poisson":0,"Log-Normal":0}, ms, raw_data)
574
  if show_extremes == "Hide Extreme Probabilities":
575
  probs = {k: v for k, v in probs_all.items() if 0.005 <= v <= 0.99}
576
  if len(probs) == 0:
@@ -578,8 +553,8 @@ def estimate(player, stat_key, timeframe,
578
  else:
579
  probs = probs_all
580
 
581
- # hit rates from raw_data (pure historical)
582
- games = _games_played(pdata)
583
  hit_rates = []
584
  for m in ms:
585
  hits = (raw_data >= m).sum() if len(raw_data) else 0
@@ -596,31 +571,24 @@ def estimate(player, stat_key, timeframe,
596
  "Hit Rate": [f"{x:.2f}%" for x in filt_hr],
597
  })
598
 
599
- # weights for charts (keep Poisson as default)
600
- try:
601
- wrow = model_weights_df[model_weights_df['player'] == player].iloc[0]
602
- weights = {
603
- "Poisson": wrow.get(f"Poisson_Weight_{stat_key}", 1.0),
604
- "Negative Binomial": wrow.get(f"Negative_Binomial_Weight_{stat_key}", 0.0),
605
- "Gamma-Poisson": wrow.get(f"Gamma_Poisson_Weight_{stat_key}", 0.0),
606
- "Log-Normal": wrow.get(f"Log_Normal_Weight_{stat_key}", 0.0),
607
- }
608
- except Exception:
609
- weights = {"Poisson":1.0, "Negative Binomial":0.0, "Gamma-Poisson":0.0, "Log-Normal":0.0}
610
 
611
  summary_md = f"""
612
  ### πŸ“Š {player} β€” {STAT_LABELS.get(stat_key, stat_key)} ({timeframe})
613
- - Games in sample: **{games}**
614
- - Historical source: **{hist_source}**
615
- - Market rows used: **{mkt_rows}**
616
- - Projection: **{proj_mean:.3f}** _({proj_source})_
617
- - Variance (hist-based): **{variance:.3f}**
618
- - Vig: **{vig:.2f}%** β€” Extremes: **{'Shown' if show_extremes == 'Show All' else 'Hidden (fallback if empty)'}**
619
  """
620
  return summary_md, table, weights, raw_data
621
 
622
- def league_avg_by_position(position, stat_key, timeframe, vig, range_gain_yds, acc_bump_abs, long_propensity,
623
- hist_weighting_mode, hist_half_life_games):
 
624
  if df is None:
625
  return pd.DataFrame(), "Upload a CSV first."
626
  if not position:
@@ -630,19 +598,22 @@ def league_avg_by_position(position, stat_key, timeframe, vig, range_gain_yds, a
630
  if slice_df.empty:
631
  return pd.DataFrame(), f"No rows for position {position}."
632
 
 
633
  ms = milestones_for(stat_key, timeframe)
634
 
635
- # historical mean (K-ball for K+FG, else recency-weighted)
636
  if stat_key == "fg_made" and position == "K":
637
- mu, var = fg_kball_mean_var_for_slice(slice_df, timeframe,
638
- range_gain_yds, acc_bump_abs, long_propensity)
639
  else:
640
- stat_col = csv_col_for(stat_key, timeframe)
641
  series = pd.to_numeric(slice_df.get(stat_col, pd.Series(dtype=float)), errors="coerce").dropna()
642
- ages = _row_recency_order(slice_df).reindex(series.index)
643
- mu, var = _weighted_mean_var(series, ages, mode=hist_weighting_mode, half_life_games=hist_half_life_games)
 
 
644
 
645
- probs = {f"{m}+": round(1 - poisson.cdf(m-1, mu), 4) for m in ms}
 
646
  table = pd.DataFrame({
647
  "Milestone": list(probs.keys()),
648
  "Probability": [f"{p*100:.2f}%" for p in probs.values()],
@@ -650,6 +621,8 @@ def league_avg_by_position(position, stat_key, timeframe, vig, range_gain_yds, a
650
  "Odds w/Vig": [to_american_odds(apply_vig(p, vig)) for p in probs.values()],
651
  })
652
  title = f"{position} League Avg β€” {STAT_LABELS.get(stat_key, stat_key)} ({timeframe})"
 
 
653
  return table, title
654
 
655
  # =========================
@@ -673,17 +646,9 @@ def team_table(team, stat_key, timeframe, vig, subset_players):
673
 
674
  rows = []
675
  for player, pdata in work.groupby("player", dropna=True):
676
- # historical mean only (team tab stays historical)
677
- if stat_key == "fg_made" and (pdata.get("position") == "K").any():
678
- mean, _ = fg_kball_mean_var_for_slice(pdata, timeframe)
679
- series = pd.to_numeric(pdata.get(stat_col, pd.Series(dtype=float)), errors="coerce").dropna()
680
- games = _games_played(pdata)
681
- else:
682
- series = pd.to_numeric(pdata.get(stat_col, pd.Series(dtype=float)), errors="coerce").dropna()
683
- games = _games_played(pdata)
684
- ages = _row_recency_order(pdata).reindex(series.index)
685
- mean, _ = _weighted_mean_var(series, ages, mode="Exponential Half-Life", half_life_games=6.0)
686
-
687
  for m in milestones:
688
  prob_hist = float((series >= m).sum()) / float(games) if (games > 0 and len(series)) else (1 - poisson.cdf(m-1, mean))
689
  rows.append({
@@ -693,7 +658,7 @@ def team_table(team, stat_key, timeframe, vig, subset_players):
693
  "Prob": round(prob_hist, 4),
694
  "True Odds": to_american_odds(prob_hist),
695
  "Odds w/ Vig": to_american_odds(apply_vig(prob_hist, vig)),
696
- "Mean": round(mean, 3),
697
  })
698
 
699
  out = pd.DataFrame(rows)
@@ -710,15 +675,15 @@ def team_table(team, stat_key, timeframe, vig, subset_players):
710
  with gr.Blocks(title="🏈 NFL Milestone Explorer",
711
  css=".green-button {background-color: #28a745 !important; color: white !important; font-weight: bold;}") as demo:
712
  gr.Markdown("## 🏈 NFL Milestone Explorer")
713
- gr.Markdown("Upload the `nfl_milestone_logs.csv`. Team tab uses **player_team**. Player tab supports position filters, market & recency weighting, K-ball for kickers, and league shrink for tiny samples.")
714
 
715
  file_in = gr.File(label="Upload nfl_milestone_logs.csv", file_types=[".csv"])
716
  status = gr.Markdown("")
717
 
718
  # Populate choices on upload
719
  def on_file(f):
720
- team_upd, players_upd, pos_upd, pos2_upd, msg = load_and_prepare_data(f)
721
- return team_upd, players_upd, pos_upd, pos2_upd, msg
722
 
723
  with gr.Tabs():
724
 
@@ -744,38 +709,33 @@ with gr.Blocks(title="🏈 NFL Milestone Explorer",
744
  p_stat = gr.Dropdown(label="Stat", choices=STAT_OPTIONS, value="rec_yds")
745
  p_time = gr.Radio(list(TIMEFRAMES.keys()), value="Game", label="Timeframe")
746
 
747
- gr.Markdown("### Projection source & blending")
748
- with gr.Row():
749
- mean_sel = gr.Radio(
750
- ["Use Per-Game Average", "Use Market Mean (if available)", "Historical + Market Blend", "Use Custom Projection"],
751
- label="Projection Source",
752
- value="Historical + Market Blend"
753
- )
754
- mkt_blend_w = gr.Slider(0, 1, value=0.5, step=0.05, label="Blend weight w (0=all Historical, 1=all Market)")
755
- custom_proj = gr.Number(label="Custom Mean (if selected)", precision=3)
756
-
757
- gr.Markdown("### Recency weighting")
758
  with gr.Row():
759
- hist_weighting_mode = gr.Radio(["Exponential Half-Life", "Uniform"], label="Historical weighting", value="Exponential Half-Life")
760
- hist_half_life = gr.Slider(1, 20, value=6, step=1, label="Historical Half-life (games)")
761
- mkt_weighting_mode = gr.Radio(["Exponential Half-Life", "Uniform"], label="Market weighting", value="Exponential Half-Life")
762
- mkt_half_life = gr.Slider(1, 20, value=6, step=1, label="Market Half-life (games)")
763
 
764
- gr.Markdown("### League shrink for small samples (applies to Historical component only)")
765
  with gr.Row():
766
- enable_league_shrink = gr.Checkbox(value=True, label="Enable league-average shrink for small samples")
767
- min_games_for_shrink = gr.Slider(1, 16, value=7, step=1, label="Min games for full shrink (G*)")
 
 
768
 
769
- gr.Markdown("### (Optional) K-ball Adjustments (only used when Stat = FG Made & Position/Player is K)")
770
  with gr.Row():
771
- range_gain = gr.Slider(0, 10, value=7, step=1, label="K-ball: Range gain (yds)")
772
- acc_bump = gr.Slider(0, 0.10, value=0.02, step=0.01, label="K-ball: Accuracy bump (abs prob)")
773
- long_prop = gr.Slider(0, 0.5, value=0.10, step=0.05, label="K-ball: Long-attempt propensity")
774
 
775
  with gr.Row():
776
  p_vig = gr.Number(label="Vig (%)", value=4.92, precision=2)
777
  extreme_toggle = gr.Radio(["Hide Extreme Probabilities", "Show All"], label="Extreme Probabilities", value="Hide Extreme Probabilities")
778
 
 
 
 
 
 
 
779
  player_btn = gr.Button("Compute Selected Players", elem_classes=["green-button"])
780
  p_summary = gr.Markdown(visible=False)
781
  p_table = gr.Dataframe(visible=False, label="Milestones")
@@ -793,13 +753,14 @@ with gr.Blocks(title="🏈 NFL Milestone Explorer",
793
  league_range_gain = gr.Slider(0, 10, value=7, step=1, label="K-ball: Range gain (yds)")
794
  league_acc_bump = gr.Slider(0, 0.10, value=0.02, step=0.01, label="K-ball: Accuracy bump (abs prob)")
795
  league_long_prop = gr.Slider(0, 0.5, value=0.10, step=0.05, label="K-ball: Long-attempt propensity")
796
- league_hist_weighting_mode = gr.Radio(["Exponential Half-Life", "Uniform"], label="Historical weighting (league)", value="Exponential Half-Life")
797
- league_hist_half_life = gr.Slider(1, 20, value=6, step=1, label="Historical Half-life (league)")
 
798
  league_btn = gr.Button("Compute League Avg", elem_classes=["green-button"])
799
  league_title = gr.Markdown(visible=False)
800
  league_table = gr.Dataframe(visible=False, label="League Avg Milestones")
801
 
802
- # Hook file load (also set league_pos choices)
803
  file_in.change(on_file, inputs=[file_in], outputs=[team_dd, players_ms, position_dd, league_pos, status])
804
 
805
  # Team roster subset choices
@@ -834,9 +795,9 @@ with gr.Blocks(title="🏈 NFL Milestone Explorer",
834
  outputs=[team_title, team_table_out])
835
 
836
  # Player run (multi-player; charts only when a single player is selected)
837
- def run_players(pos, players, stat, timeframe, mean_sel_v, custom_mean, blend_w, vig, extreme_v,
838
- mkt_w_mode, mkt_hl, hist_w_mode, hist_hl, rg, acc, lp,
839
- enable_shrink, min_g):
840
  if df is None:
841
  return (gr.update(visible=False), gr.update(visible=False),
842
  gr.update(visible=False), gr.update(visible=False),
@@ -859,9 +820,11 @@ with gr.Blocks(title="🏈 NFL Milestone Explorer",
859
 
860
  for i, player in enumerate(players):
861
  summary_md, table, weights, raw = estimate(
862
- player, stat, timeframe, mean_sel_v, custom_mean, blend_w, vig, extreme_v,
863
- mkt_w_mode, mkt_hl, hist_w_mode, hist_hl, rg, acc, lp,
864
- enable_shrink, min_g
 
 
865
  )
866
  t = table.copy()
867
  t.insert(0, "Player", player)
@@ -875,7 +838,7 @@ with gr.Blocks(title="🏈 NFL Milestone Explorer",
875
  if len(players) == 1 and first_weights is not None:
876
  pie = generate_pie(first_weights, players[0], stat)
877
  raw = first_raw if first_raw is not None else pd.Series(dtype=float)
878
- hist = generate_histogram(
879
  raw,
880
  first_weights,
881
  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),
@@ -891,7 +854,7 @@ with gr.Blocks(title="🏈 NFL Milestone Explorer",
891
  return (gr.update(value=first_summary, visible=True),
892
  gr.update(value=stacked, visible=True),
893
  gr.update(value=pie, visible=True),
894
- gr.update(value=hist, visible=True),
895
  gr.update(value=recent_disp, visible=True))
896
  else:
897
  title = f"{', '.join(players[:6])}{' …' if len(players) > 6 else ''} β€” {STAT_LABELS.get(stat, stat)} ({timeframe})"
@@ -903,21 +866,22 @@ with gr.Blocks(title="🏈 NFL Milestone Explorer",
903
 
904
  player_btn.click(
905
  run_players,
906
- inputs=[position_dd, players_ms, p_stat, p_time, mean_sel, custom_proj, mkt_blend_w, p_vig, extreme_toggle,
907
- mkt_weighting_mode, mkt_half_life, hist_weighting_mode, hist_half_life,
908
  range_gain, acc_bump, long_prop,
909
- enable_league_shrink, min_games_for_shrink],
910
  outputs=[p_summary, p_table, p_pie, p_hist, recent_games]
911
  )
912
 
913
  # League average run (by position)
914
- def run_league_avg(pos, stat, timeframe, vig, rg, acc, lp, hist_mode, hist_hl):
915
- table, title = league_avg_by_position(pos, stat, timeframe, vig, rg, acc, lp, hist_mode, hist_hl)
916
  return gr.update(value=title, visible=True), gr.update(value=table, visible=True)
917
- league_btn.click(run_league_avg,
918
- inputs=[league_pos, league_stat, league_time, league_vig,
919
- league_range_gain, league_acc_bump, league_long_prop,
920
- league_hist_weighting_mode, league_hist_half_life],
921
- outputs=[league_title, league_table])
 
 
922
 
923
  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 gradio as gr
6
+ import re
7
 
8
  # =========================
9
  # Stat mapping & milestones
 
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
  # ======================
112
  def bayesian_model_weights(poisson_ll, nb_ll, gamma_ll, lognorm_ll):
113
  log_likelihoods = np.array([poisson_ll, nb_ll, gamma_ll, lognorm_ll])
 
190
  ax.legend()
191
  return fig
192
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
  # ====================
194
  # FG K-ball model helpers (used ONLY for fg_made and kickers)
195
  # ====================
 
231
  atts = np.array([pd.to_numeric(pdata.get(c, pd.Series(dtype=float)), errors="coerce").fillna(0).sum() for c in att_cols])
232
  made = np.array([pd.to_numeric(pdata.get(c, pd.Series(dtype=float)), errors="coerce").fillna(0).sum() for c in made_cols])
233
 
234
+ games = pdata["game_id"].nunique() if "game_id" in pdata.columns else len(pdata)
235
  games = games if games > 0 else 1
236
  lam_bins = atts / games
237
 
 
243
  def fg_kball_mean_var_for_slice(pdata: pd.DataFrame, timeframe: str,
244
  range_gain_yds=7.0, acc_bump_abs=0.02, long_propensity=0.10):
245
  lam_bins, (a, b, R), dist_mid = _fg_lambda_p_from_slice(pdata, timeframe)
 
246
  # Tilt long attempts
247
  tilt = np.ones_like(lam_bins)
248
  long_idx = np.array([i for i,(k,_) in enumerate(FG_BINS) if k in ("55_59","60_plus")])
249
  tilt[long_idx] = 1.0 + float(long_propensity)
250
  lam_bins = lam_bins * tilt
 
251
  # Keep total attempts per game constant after tilting
252
  total = lam_bins.sum()
253
  if total > 1e-12:
254
  lam_bins = lam_bins * (total / lam_bins.sum())
 
255
  # Range + accuracy bump
256
  def p_range(d): return _p_make(a, b, R + float(range_gain_yds), d)
257
  p_vec = np.array([np.clip(p_range(d) + float(acc_bump_abs), 0.0, 1.0) for d in dist_mid])
 
258
  mu = float(np.sum(lam_bins * p_vec))
259
  var = float(np.sum(lam_bins * p_vec * (1.0 - p_vec)))
260
  return mu, var
261
 
262
+ # ====================
263
+ # Market helpers (robust column finder + recency weighting)
264
+ # ====================
265
+ def _timeframe_synonyms(timeframe: str):
266
+ if timeframe == "1H":
267
+ return ["_1h", "1h", "first_half", "fh", "_fh"]
268
+ if timeframe == "1Q":
269
+ return ["_1q", "1q", "q1", "first_quarter", "_q1", "fq"]
270
+ return ["", "game", "full", "final", "fg", "full_game"]
271
+
272
+ def _normalize_text(s: str) -> str:
273
+ s = str(s).lower().strip()
274
+ s = s.replace("-", "_").replace(" ", "_")
275
+ s = re.sub(r"__+", "_", s)
276
+ return s
277
+
278
+ def _market_line_candidates(stat_key: str, timeframe: str):
279
+ base = STAT_MAP.get(stat_key, stat_key)
280
+ base_alt = list({base, base.replace("rec_", "receiving_"), base.replace("pass_", "passing_"),
281
+ base.replace("rush_", "rushing_"), base.replace("_yds","_yards")})
282
+ line_words = ["line", "prop", "projection", "close", "closing_line", "market"]
283
+ tfs = _timeframe_synonyms(timeframe)
284
+ pats = []
285
+ for b in base_alt:
286
+ for tf in tfs:
287
+ for lw in line_words:
288
+ pats.append(re.compile(rf"{re.escape(b)}.*{lw}.*{re.escape(tf)}", re.IGNORECASE))
289
+ if tf:
290
+ pats.append(re.compile(rf"{re.escape(tf)}.*{re.escape(b)}.*{lw}", re.IGNORECASE))
291
+ for b in base_alt:
292
+ for lw in line_words:
293
+ pats.append(re.compile(rf"{re.escape(b)}.*{lw}", re.IGNORECASE))
294
+ return pats
295
+
296
+ def _choose_best_column(pdata: pd.DataFrame, patterns):
297
+ if pdata is None or pdata.empty:
298
+ return None, 0
299
+ cols = list(pdata.columns)
300
+ norm = {c: _normalize_text(c) for c in cols}
301
+ for pat in patterns:
302
+ matches = [c for c in cols if pat.search(norm[c])]
303
+ if not matches:
304
+ continue
305
+ best_col = None
306
+ best_n = -1
307
+ for c in matches:
308
+ vals = pd.to_numeric(pdata[c], errors="coerce")
309
+ n = int(vals.notna().sum())
310
+ if n > best_n:
311
+ best_n = n
312
+ best_col = c
313
+ if best_col is not None and best_n > 0:
314
+ return best_col, best_n
315
+ return None, 0
316
+
317
+ # ====================
318
+ # Recency weighting helpers (shared by historical & market)
319
+ # ====================
320
+ def _row_recency_order(frame: pd.DataFrame) -> pd.Series:
321
+ # Derive a recency index (0=oldest, ... increasing to most recent)
322
+ # Prefer (season, week), else gameday, else index order
323
+ if {"season", "week"}.issubset(frame.columns):
324
+ tmp = frame[["season","week"]].copy()
325
+ tmp = tmp.assign(_order = (pd.to_numeric(tmp["season"], errors="coerce").fillna(0).astype(int) * 100 +
326
+ pd.to_numeric(tmp["week"], errors="coerce").fillna(0).astype(int)))
327
+ order = tmp["_order"].rank(method="first")
328
+ return order
329
+ if "gameday" in frame.columns:
330
+ try:
331
+ gd = pd.to_datetime(frame["gameday"], errors="coerce")
332
+ order = gd.rank(method="first")
333
+ return order
334
+ except Exception:
335
+ pass
336
+ # fallback: existing row order
337
+ return pd.Series(np.arange(len(frame)), index=frame.index, dtype=float)
338
+
339
+ def _weights_from_ages(ages: pd.Series, mode: str, half_life_games: float) -> np.ndarray:
340
+ n = len(ages)
341
+ if n == 0:
342
+ return np.array([])
343
+ if mode == "Uniform":
344
+ return np.ones(n) / n
345
+ if mode == "Linear (newer heavier)":
346
+ # higher age -> more recent -> larger weight
347
+ r = ages.rank(method="first")
348
+ w = r.values
349
+ s = w.sum()
350
+ return w / s if s > 0 else np.ones(n)/n
351
+ # Exponential (EWMA) by default
352
+ if half_life_games <= 0:
353
+ return np.ones(n)/n
354
+ # Map rank to age steps
355
+ r = ages.rank(method="first").values
356
+ # Newest gets highest rank -> age = 0 for newest
357
+ age_steps = (r.max() - r)
358
+ lam = np.log(2) / half_life_games
359
+ w = np.exp(-lam * age_steps)
360
+ s = w.sum()
361
+ return w / s if s > 0 else np.ones(n)/n
362
+
363
+ def _weighted_mean_var(series: pd.Series, ages: pd.Series, mode: str, half_life_games: float):
364
+ x = pd.to_numeric(series, errors="coerce").replace([np.inf, -np.inf], np.nan).dropna()
365
+ if x.empty:
366
+ return 0.0, 0.0
367
+ ages = ages.reindex(x.index)
368
+ w = _weights_from_ages(ages, mode, half_life_games)
369
+ if len(w) != len(x) or len(w) == 0:
370
+ return float(x.mean()), float(x.var() if len(x) > 1 else 0.0)
371
+ mu = float(np.sum(w * x.values))
372
+ # Weighted variance (unbiased not necessary here)
373
+ var = float(np.sum(w * (x.values - mu)**2))
374
+ return mu, var
375
+
376
+ def compute_market_mean(pdata: pd.DataFrame, stat_key: str, timeframe: str,
377
+ weighting_mode: str, half_life_games: float):
378
+ if pdata is None or pdata.empty:
379
+ return None, 0
380
+ patterns = _market_line_candidates(stat_key, timeframe)
381
+ line_col, nn = _choose_best_column(pdata, patterns)
382
+ if line_col is None:
383
+ return None, 0
384
+ vals = pd.to_numeric(pdata[line_col], errors="coerce").replace([np.inf, -np.inf], np.nan).dropna()
385
+ if vals.empty:
386
+ return None, 0
387
+ ages = _row_recency_order(pdata).reindex(vals.index)
388
+ mu, _ = _weighted_mean_var(vals, ages, mode=weighting_mode, half_life_games=half_life_games)
389
+ return float(mu), int(vals.shape[0])
390
+
391
  # ====================
392
  # Globals
393
  # ====================
 
401
  def csv_col_for(stat_key: str, timeframe_key: str) -> str:
402
  return f"{STAT_MAP.get(stat_key, stat_key)}{TIMEFRAMES.get(timeframe_key, '')}"
403
 
 
 
 
 
 
404
  def load_and_prepare_data(file):
405
  """Populate globals and return UI choices."""
406
  global df, model_weights_df, temp_df
 
411
  gr.update(choices=[], value=None),
412
  "Upload the CSV to begin.")
413
 
414
+ raw = pd.read_csv(file.name)
415
+ raw.columns = [c.strip() for c in raw.columns]
416
  df = raw.copy()
417
  temp_df = raw.copy()
418
 
 
424
  rows = []
425
  for player in players:
426
  pdata = df[df["player"] == player]
427
+ row = {"player": player, "games_played": pdata["game_id"].nunique() if "game_id" in pdata.columns else len(pdata)}
428
  for stat_key in STAT_OPTIONS:
429
  stat_col = csv_col_for(stat_key, "Game")
430
  if stat_col not in pdata.columns:
 
446
  return (gr.update(choices=teams, value=(teams[0] if teams else None)),
447
  gr.update(choices=players, value=[]),
448
  gr.update(choices=positions, value=None),
449
+ gr.update(choices=positions, value=None),
450
  msg)
451
 
452
  # =========================
453
+ # League-average fallback (for small samples)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
454
  # =========================
455
+ def league_avg_mean_for(position: str, stat_key: str, timeframe: str):
 
 
 
 
 
 
 
456
  if df is None or not position:
457
+ return None
458
+ slice_df = df[df.get("position") == position]
 
459
  if slice_df.empty:
460
+ return None
461
 
462
+ # FG K-ball used only for K + fg_made
463
  if stat_key == "fg_made" and position == "K":
464
+ mu, _ = fg_kball_mean_var_for_slice(slice_df, timeframe)
465
+ return mu
466
 
467
  stat_col = csv_col_for(stat_key, timeframe)
468
+ series = pd.to_numeric(slice_df.get(stat_col, pd.Series(dtype=float)), errors="coerce").dropna()
469
+ return float(series.mean()) if len(series) else None
 
 
 
 
 
470
 
471
  # =========================
472
+ # Player tab (position filter + market/historical blend + recency + K-ball knobs)
473
  # =========================
474
+ def estimate(player, stat_key, timeframe, mean_sel, custom_mean, vig, show_extremes,
 
 
 
475
  range_gain_yds, acc_bump_abs, long_propensity,
476
+ hist_weight_mode, hist_half_life,
477
+ mkt_weight_mode, mkt_half_life,
478
+ alpha_market, small_sample_n):
479
  if df is None or model_weights_df is None:
480
  return "⚠️ Upload the CSV.", pd.DataFrame(), {"Poisson":1,"Negative Binomial":0,"Gamma-Poisson":0,"Log-Normal":0}, pd.Series(dtype=float)
481
 
 
483
  return f"⚠️ Player '{player}' not found.", pd.DataFrame(), {"Poisson":1,"Negative Binomial":0,"Gamma-Poisson":0,"Log-Normal":0}, pd.Series(dtype=float)
484
 
485
  stat_col = csv_col_for(stat_key, timeframe)
486
+ pdata = df[df['player'] == player]
487
+ raw_data = pd.to_numeric(pdata.get(stat_col, pd.Series(dtype=float)), errors="coerce").dropna()
488
+
489
+ # base historical mean/variance with recency weighting
490
+ ages = _row_recency_order(pdata).reindex(pdata.index)
491
+ hist_mu, hist_var = _weighted_mean_var(pd.to_numeric(pdata.get(stat_col, pd.Series(dtype=float)), errors="coerce"),
492
+ ages, mode=hist_weight_mode, half_life_games=float(hist_half_life))
493
 
494
+ # FG K-ball model ONLY for kickers & fg_made β€” overrides historical mean/var used in probabilities
495
  is_kicker = (pdata.get("position") == "K").any()
 
496
  if stat_key == "fg_made" and is_kicker:
497
+ k_mu, k_var = fg_kball_mean_var_for_slice(pdata, timeframe, range_gain_yds, acc_bump_abs, long_propensity)
498
+ hist_mu, hist_var = k_mu, k_var
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
499
 
500
+ # Market mean (robust column finder) + recency weighting
501
+ mkt_mu, mkt_n = compute_market_mean(pdata, stat_key, timeframe, weighting_mode=mkt_weight_mode, half_life_games=float(mkt_half_life))
502
+
503
+ # Small-sample fallback to league average mean (same position)
504
+ position = str(pdata["position"].dropna().iloc[0]) if "position" in pdata.columns and not pdata["position"].dropna().empty else None
505
+ if position:
506
+ league_mu = league_avg_mean_for(position, stat_key, timeframe)
507
+ else:
508
+ league_mu = None
509
+
510
+ # If very small historical sample, blend toward league mean
511
+ hist_n = int(pdata["game_id"].nunique()) if "game_id" in pdata.columns else int(len(pdata))
512
+ if league_mu is not None and hist_n < int(small_sample_n):
513
+ # weight toward league avg smoothly: weight = 1 - hist_n / small_sample_n
514
+ w = max(0.0, min(1.0, 1.0 - (hist_n / float(small_sample_n))))
515
+ hist_mu = (1 - w) * hist_mu + w * league_mu
516
+
517
+ # Choose mean to use
518
+ if mean_sel == "Use Custom Projection" and custom_mean is not None:
519
+ mean_to_use = float(custom_mean)
520
+ src_tag = "Custom"
521
  else:
522
+ # Blend: alpha * market + (1-alpha) * historical
523
+ # If market missing -> treat alpha=0 effectively
524
+ if mkt_mu is None:
525
+ mean_to_use = hist_mu
526
+ src_tag = "Historical"
527
+ mkt_used_rows = 0
528
+ else:
529
+ mean_to_use = float(alpha_market) * float(mkt_mu) + (1 - float(alpha_market)) * float(hist_mu)
530
+ src_tag = f"Blend (Ξ±={alpha_market:.2f})"
531
+ mkt_used_rows = mkt_n
532
+
533
+ # weights (for plot)
534
+ try:
535
+ wrow = model_weights_df[model_weights_df['player'] == player].iloc[0]
536
+ weights = {
537
+ "Poisson": wrow.get(f"Poisson_Weight_{stat_key}", 1.0),
538
+ "Negative Binomial": wrow.get(f"Negative_Binomial_Weight_{stat_key}", 0.0),
539
+ "Gamma-Poisson": wrow.get(f"Gamma_Poisson_Weight_{stat_key}", 0.0),
540
+ "Log-Normal": wrow.get(f"Log_Normal_Weight_{stat_key}", 0.0),
541
+ }
542
+ except Exception:
543
+ weights = {"Poisson":1.0, "Negative Binomial":0.0, "Gamma-Poisson":0.0, "Log-Normal":0.0}
544
 
545
  ms = milestones_for(stat_key, timeframe)
546
+ probs_all = calculate_probability(mean_to_use, hist_var, weights, ms, raw_data)
547
 
548
+ # hide extremes but never return empty
549
  if show_extremes == "Hide Extreme Probabilities":
550
  probs = {k: v for k, v in probs_all.items() if 0.005 <= v <= 0.99}
551
  if len(probs) == 0:
 
553
  else:
554
  probs = probs_all
555
 
556
+ # hit rates (historical)
557
+ games = pdata['game_id'].nunique() if "game_id" in pdata.columns else len(pdata)
558
  hit_rates = []
559
  for m in ms:
560
  hits = (raw_data >= m).sum() if len(raw_data) else 0
 
571
  "Hit Rate": [f"{x:.2f}%" for x in filt_hr],
572
  })
573
 
574
+ mkt_info = f"Market mean: **{('n/a' if mkt_mu is None else f'{mkt_mu:.2f}')}** (rows: {mkt_n if mkt_mu is not None else 0})"
575
+ hist_info = f"Historical mean (recency-weighted): **{hist_mu:.2f}** (games: {hist_n})"
576
+ if league_mu is not None and hist_n < int(small_sample_n):
577
+ hist_info += f" β†’ blended with league avg **{league_mu:.2f}** for small sample"
 
 
 
 
 
 
 
578
 
579
  summary_md = f"""
580
  ### πŸ“Š {player} β€” {STAT_LABELS.get(stat_key, stat_key)} ({timeframe})
581
+ - Projection source: **{src_tag}**
582
+ - Mean used: **{mean_to_use:.2f}**
583
+ - {hist_info}
584
+ - {mkt_info}
585
+ - Vig: **{vig:.2f}%**, Extremes: **{'Shown' if show_extremes == 'Show All' else 'Hidden (with fallback)'}**
 
586
  """
587
  return summary_md, table, weights, raw_data
588
 
589
+ def league_avg_by_position(position, stat_key, timeframe, vig,
590
+ range_gain_yds, acc_bump_abs, long_propensity,
591
+ mkt_weight_mode, mkt_half_life):
592
  if df is None:
593
  return pd.DataFrame(), "Upload a CSV first."
594
  if not position:
 
598
  if slice_df.empty:
599
  return pd.DataFrame(), f"No rows for position {position}."
600
 
601
+ stat_col = csv_col_for(stat_key, timeframe)
602
  ms = milestones_for(stat_key, timeframe)
603
 
604
+ # FG K-ball mean ONLY if position is K and stat is fg_made
605
  if stat_key == "fg_made" and position == "K":
606
+ mu_hist, _ = fg_kball_mean_var_for_slice(slice_df, timeframe,
607
+ range_gain_yds, acc_bump_abs, long_propensity)
608
  else:
 
609
  series = pd.to_numeric(slice_df.get(stat_col, pd.Series(dtype=float)), errors="coerce").dropna()
610
+ mu_hist = series.mean() if len(series) else 0.0
611
+
612
+ # Optionally show market-only league info too (best-effort)
613
+ mkt_mu, mkt_n = compute_market_mean(slice_df, stat_key, timeframe, weighting_mode=mkt_weight_mode, half_life_games=float(mkt_half_life))
614
 
615
+ # For pricing table we use historical/k-ball league mean (not markets) to avoid book-bias here
616
+ probs = {f"{m}+": round(1 - poisson.cdf(m-1, mu_hist), 4) for m in ms}
617
  table = pd.DataFrame({
618
  "Milestone": list(probs.keys()),
619
  "Probability": [f"{p*100:.2f}%" for p in probs.values()],
 
621
  "Odds w/Vig": [to_american_odds(apply_vig(p, vig)) for p in probs.values()],
622
  })
623
  title = f"{position} League Avg β€” {STAT_LABELS.get(stat_key, stat_key)} ({timeframe})"
624
+ if mkt_mu is not None:
625
+ title += f" | Market mean ~ {mkt_mu:.2f} (rows {mkt_n})"
626
  return table, title
627
 
628
  # =========================
 
646
 
647
  rows = []
648
  for player, pdata in work.groupby("player", dropna=True):
649
+ series = pd.to_numeric(pdata.get(stat_col, pd.Series(dtype=float)), errors="coerce").dropna()
650
+ games = pdata['game_id'].nunique() if "game_id" in pdata.columns else len(pdata)
651
+ mean = series.mean() if len(series) else 0.0
 
 
 
 
 
 
 
 
652
  for m in milestones:
653
  prob_hist = float((series >= m).sum()) / float(games) if (games > 0 and len(series)) else (1 - poisson.cdf(m-1, mean))
654
  rows.append({
 
658
  "Prob": round(prob_hist, 4),
659
  "True Odds": to_american_odds(prob_hist),
660
  "Odds w/ Vig": to_american_odds(apply_vig(prob_hist, vig)),
661
+ "Mean": round(mean, 2),
662
  })
663
 
664
  out = pd.DataFrame(rows)
 
675
  with gr.Blocks(title="🏈 NFL Milestone Explorer",
676
  css=".green-button {background-color: #28a745 !important; color: white !important; font-weight: bold;}") as demo:
677
  gr.Markdown("## 🏈 NFL Milestone Explorer")
678
+ gr.Markdown("Upload the `nfl_milestone_logs.csv`. Team tab uses the **player_team** column. Player tab supports position filters, market/historical blending, and K-ball adjustments for kickers only.")
679
 
680
  file_in = gr.File(label="Upload nfl_milestone_logs.csv", file_types=[".csv"])
681
  status = gr.Markdown("")
682
 
683
  # Populate choices on upload
684
  def on_file(f):
685
+ team_upd, players_upd, pos_upd, league_pos_upd, msg = load_and_prepare_data(f)
686
+ return team_upd, players_upd, pos_upd, league_pos_upd, msg
687
 
688
  with gr.Tabs():
689
 
 
709
  p_stat = gr.Dropdown(label="Stat", choices=STAT_OPTIONS, value="rec_yds")
710
  p_time = gr.Radio(list(TIMEFRAMES.keys()), value="Game", label="Timeframe")
711
 
712
+ gr.Markdown("### Projection controls")
 
 
 
 
 
 
 
 
 
 
713
  with gr.Row():
714
+ mean_sel = gr.Radio(["Use Per-Game Average", "Use Custom Projection"], label="Projection Source", value="Use Per-Game Average")
715
+ custom_proj = gr.Number(label="Custom Mean (if selected)", precision=2)
716
+ alpha_market = gr.Slider(0, 1, value=0.50, step=0.05, label="Blend weight Ξ± (Market vs Historical)")
 
717
 
718
+ gr.Markdown("**Recency weighting**")
719
  with gr.Row():
720
+ hist_weight_mode = gr.Radio(choices=["EWMA (half-life)", "Linear (newer heavier)", "Uniform"], value="EWMA (half-life)", label="Historical weighting")
721
+ hist_half_life = gr.Slider(1, 20, value=6, step=1, label="Historical half-life (games)")
722
+ mkt_weight_mode = gr.Radio(choices=["EWMA (half-life)", "Linear (newer heavier)", "Uniform"], value="EWMA (half-life)", label="Market weighting")
723
+ mkt_half_life = gr.Slider(1, 20, value=6, step=1, label="Market half-life (games)")
724
 
725
+ gr.Markdown("**Small-sample fallback**")
726
  with gr.Row():
727
+ small_sample_n = gr.Slider(3, 12, value=7, step=1, label="Blend to league avg if < N games (same position)")
 
 
728
 
729
  with gr.Row():
730
  p_vig = gr.Number(label="Vig (%)", value=4.92, precision=2)
731
  extreme_toggle = gr.Radio(["Hide Extreme Probabilities", "Show All"], label="Extreme Probabilities", value="Hide Extreme Probabilities")
732
 
733
+ gr.Markdown("### (Optional) K-ball Adjustments (only used when Stat = FG Made & Position = K)")
734
+ with gr.Row():
735
+ range_gain = gr.Slider(0, 10, value=7, step=1, label="K-ball: Range gain (yds)")
736
+ acc_bump = gr.Slider(0, 0.10, value=0.02, step=0.01, label="K-ball: Accuracy bump (abs prob)")
737
+ long_prop = gr.Slider(0, 0.5, value=0.10, step=0.05, label="K-ball: Long-attempt propensity")
738
+
739
  player_btn = gr.Button("Compute Selected Players", elem_classes=["green-button"])
740
  p_summary = gr.Markdown(visible=False)
741
  p_table = gr.Dataframe(visible=False, label="Milestones")
 
753
  league_range_gain = gr.Slider(0, 10, value=7, step=1, label="K-ball: Range gain (yds)")
754
  league_acc_bump = gr.Slider(0, 0.10, value=0.02, step=0.01, label="K-ball: Accuracy bump (abs prob)")
755
  league_long_prop = gr.Slider(0, 0.5, value=0.10, step=0.05, label="K-ball: Long-attempt propensity")
756
+ with gr.Row():
757
+ league_mkt_weight_mode = gr.Radio(choices=["EWMA (half-life)", "Linear (newer heavier)", "Uniform"], value="EWMA (half-life)", label="(Optional) Market weighting (info)")
758
+ league_mkt_half_life = gr.Slider(1, 20, value=6, step=1, label="(Optional) Market half-life (games)")
759
  league_btn = gr.Button("Compute League Avg", elem_classes=["green-button"])
760
  league_title = gr.Markdown(visible=False)
761
  league_table = gr.Dataframe(visible=False, label="League Avg Milestones")
762
 
763
+ # Hook file load
764
  file_in.change(on_file, inputs=[file_in], outputs=[team_dd, players_ms, position_dd, league_pos, status])
765
 
766
  # Team roster subset choices
 
795
  outputs=[team_title, team_table_out])
796
 
797
  # Player run (multi-player; charts only when a single player is selected)
798
+ def run_players(pos, players, stat, timeframe, mean_sel_v, custom_mean, vig, extreme_v,
799
+ rg, acc, lp,
800
+ hist_mode, hist_hl, mkt_mode, mkt_hl, alpha_mkt, smallN):
801
  if df is None:
802
  return (gr.update(visible=False), gr.update(visible=False),
803
  gr.update(visible=False), gr.update(visible=False),
 
820
 
821
  for i, player in enumerate(players):
822
  summary_md, table, weights, raw = estimate(
823
+ player, stat, timeframe, mean_sel_v, custom_mean, vig, extreme_v,
824
+ rg, acc, lp,
825
+ hist_mode, hist_hl,
826
+ mkt_mode, mkt_hl,
827
+ alpha_mkt, smallN
828
  )
829
  t = table.copy()
830
  t.insert(0, "Player", player)
 
838
  if len(players) == 1 and first_weights is not None:
839
  pie = generate_pie(first_weights, players[0], stat)
840
  raw = first_raw if first_raw is not None else pd.Series(dtype=float)
841
+ hist_fig = generate_histogram(
842
  raw,
843
  first_weights,
844
  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),
 
854
  return (gr.update(value=first_summary, visible=True),
855
  gr.update(value=stacked, visible=True),
856
  gr.update(value=pie, visible=True),
857
+ gr.update(value=hist_fig, visible=True),
858
  gr.update(value=recent_disp, visible=True))
859
  else:
860
  title = f"{', '.join(players[:6])}{' …' if len(players) > 6 else ''} β€” {STAT_LABELS.get(stat, stat)} ({timeframe})"
 
866
 
867
  player_btn.click(
868
  run_players,
869
+ inputs=[position_dd, players_ms, p_stat, p_time, mean_sel, custom_proj, p_vig, extreme_toggle,
 
870
  range_gain, acc_bump, long_prop,
871
+ hist_weight_mode, hist_half_life, mkt_weight_mode, mkt_half_life, alpha_market, small_sample_n],
872
  outputs=[p_summary, p_table, p_pie, p_hist, recent_games]
873
  )
874
 
875
  # League average run (by position)
876
+ def run_league_avg(pos, stat, timeframe, vig, rg, acc, lp, mkt_mode, mkt_hl):
877
+ table, title = league_avg_by_position(pos, stat, timeframe, vig, rg, acc, lp, mkt_mode, mkt_hl)
878
  return gr.update(value=title, visible=True), gr.update(value=table, visible=True)
879
+ league_btn.click(
880
+ run_league_avg,
881
+ inputs=[league_pos, league_stat, league_time, league_vig,
882
+ league_range_gain, league_acc_bump, league_long_prop,
883
+ league_mkt_weight_mode, league_mkt_half_life],
884
+ outputs=[league_title, league_table]
885
+ )
886
 
887
  demo.launch()