## == In this version, I expand on p6v1 (p7 variations aren't working). Snowflake now pulls roto min projections ## == No longer need the ETR file upload. I will amend the data upload/clean function here. import pandas as pd import numpy as np import matplotlib.pyplot as plt from scipy.stats import poisson, nbinom, gamma, lognorm, kstest import gradio as gr # === Milestone thresholds === milestones = { "points": [8, 10, 12, 15, 18, 20, 25, 30, 35, 40, 45, 50], "rebounds": [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], "assists": [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], "threes_made": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], "steals": [1, 2, 3, 4, 5, 6, 7], "blocks": [1, 2, 3, 4, 5, 6, 7], "turnovers": [1, 2, 3, 4, 5, 6, 7] } # === Helper Functions === def bayesian_model_weights(poisson_ll, nb_ll, gamma_ll, lognorm_ll): log_likelihoods = np.array([poisson_ll, nb_ll, gamma_ll, lognorm_ll]) log_likelihoods = np.nan_to_num(log_likelihoods, nan=-np.inf) max_log_likelihood = np.max(log_likelihoods) exp_likelihoods = np.exp(log_likelihoods - max_log_likelihood) sum_likelihoods = np.sum(exp_likelihoods) if sum_likelihoods == 0: return {"Poisson": 0.25, "Negative Binomial": 0.25, "Gamma-Poisson": 0.25, "Log-Normal": 0.25} weights = exp_likelihoods / sum_likelihoods return { "Poisson": weights[0], "Negative Binomial": weights[1], "Gamma-Poisson": weights[2], "Log-Normal": weights[3] } def determine_best_fit(data): data = data.dropna() mean = np.mean(data) variance = np.var(data) if len(data) < 5: return bayesian_model_weights(-np.inf, -np.inf, -np.inf, -np.inf) if mean <= 0 or variance <= 0: return bayesian_model_weights(-np.inf, -np.inf, -np.inf, -np.inf) poisson_ll = np.sum(poisson.logpmf(data, mean)) if mean > 0 else -np.inf nb_r = mean ** 2 / (variance - mean) if variance > mean else 10 nb_p = mean / variance if variance > mean else 0.5 nb_ll = np.sum(nbinom.logpmf(data, nb_r, nb_p)) if nb_r > 0 and nb_p > 0 else -np.inf gamma_ll = np.sum(gamma.logpdf(data, a=nb_r, scale=nb_p)) if nb_r > 0 and nb_p > 0 else -np.inf log_data = np.log1p(data[data > 0]) ks_stat, ks_p_value = kstest(log_data, 'norm', args=(np.mean(log_data), np.std(log_data))) lognorm_ll = np.sum(lognorm.logpdf(data[data > 0], s=np.std(log_data), scale=np.exp(np.mean(log_data)))) if ks_p_value > 0.01 else -np.inf return bayesian_model_weights(poisson_ll, nb_ll, gamma_ll, lognorm_ll) def convert_to_american_odds(prob): if prob >= 1: return "-10000" elif prob >= 0.5: odds = - (prob / (1 - prob)) * 100 return f"{int(odds):+}" elif prob > 0: odds = ((1 - prob) / prob) * 100 return f"+{int(odds)}" else: return "+10000" def adjust_for_vig(prob, vig_percent): vig_prob = max(prob + (vig_percent / 100), 0.001) return convert_to_american_odds(vig_prob) # === Global data placeholders === df = None model_weights_df = None temp_df = None # === Core Functions === def load_and_prepare_data(log_file, apply_filter, threshold): global df, model_weights_df if log_file is None: return gr.update(choices=[]) game_logs = pd.read_csv(log_file.name) # binary outlier indicator based on user input threshold game_logs['is_minutes_outlier'] = game_logs['MINS_PCT_DIFF'].abs() > threshold global temp_df temp_df = game_logs.copy() temp_df.columns = temp_df.columns.str.lower() # Filter for instances where a player's actual minutes played deviates from roto projected minutes by more than x% # only apply filter if user enables it if apply_filter: game_logs = game_logs[game_logs['MINS_PCT_DIFF'].abs() <= threshold] df = game_logs.copy() df.columns = df.columns.str.lower() player_stats = [] for player in df['player'].unique(): pdata = df[df['player'] == player] row = { "player": player, "games_played": pdata['game_date_pacific'].nunique(), "points": pdata['points'].mean(), "rebounds": pdata['rebounds'].mean(), "assists": pdata['assists'].mean(), "threes_made": pdata['threes_made'].mean(), "steals": pdata['steals'].mean(), "blocks": pdata['blocks'].mean(), "turnovers": pdata['turnovers'].mean() } for stat in ['points', 'rebounds', 'assists', 'threes_made', 'steals', 'blocks', 'turnovers']: raw_data = pdata[stat] weights = determine_best_fit(raw_data) row[f"Poisson_Weight_{stat}"] = round(weights["Poisson"], 4) row[f"Negative_Binomial_Weight_{stat}"] = round(weights["Negative Binomial"], 4) row[f"Gamma_Poisson_Weight_{stat}"] = round(weights["Gamma-Poisson"], 4) row[f"Log_Normal_Weight_{stat}"] = round(weights["Log-Normal"], 4) player_stats.append(row) model_weights_df = pd.DataFrame(player_stats) return gr.update(choices=sorted(df['player'].unique()), interactive=True) def calculate_probability(mean, variance, weights, milestones, raw_data): probabilities = {} if mean <= 0 or variance <= 0: return {f"{milestone}+": 0.001 for milestone in milestones} for milestone in milestones: poisson_prob = 1 - poisson.cdf(milestone - 1, mean) if mean > 0 else 0 nb_r = mean ** 2 / (variance - mean) if variance > mean else 10 nb_p = mean / variance if variance > mean else 0.5 nb_prob = 1 - nbinom.cdf(milestone - 1, nb_r, nb_p) if nb_r > 0 and nb_p > 0 else 0 gamma_prob = 1 - gamma.cdf(milestone, a=mean, scale=variance / mean) if mean > 0 and variance > 0 else 0 lognorm_prob = 0 if weights["Log-Normal"] > 0: log_data = np.log1p(raw_data[raw_data > 0]) if len(log_data) > 1: lognorm_prob = 1 - lognorm.cdf(milestone, s=np.std(log_data), scale=np.exp(np.mean(log_data))) blended_prob = ( weights["Poisson"] * poisson_prob + weights["Negative Binomial"] * nb_prob + weights["Gamma-Poisson"] * gamma_prob + weights["Log-Normal"] * lognorm_prob ) probabilities[f"{milestone}+"] = round(blended_prob if blended_prob > 0 else 0.001, 4) return probabilities def estimate(player, stat, mean_sel, custom_mean, vig, show_extremes): if df is None or model_weights_df is None or player not in df['player'].values: return "⚠️ Please upload both files and select a valid player.", pd.DataFrame(), None, None pdata = df[df['player'] == player] raw_data = pdata[stat].dropna() hist_mean = raw_data.mean() variance = raw_data.var() mean_to_use = custom_mean if mean_sel == "Use Custom Projection" else hist_mean weights_row = model_weights_df[model_weights_df['player'] == player].iloc[0] weights = { "Poisson": weights_row[f"Poisson_Weight_{stat}"], "Negative Binomial": weights_row[f"Negative_Binomial_Weight_{stat}"], "Gamma-Poisson": weights_row[f"Gamma_Poisson_Weight_{stat}"], "Log-Normal": weights_row[f"Log_Normal_Weight_{stat}"] } probs = calculate_probability(mean_to_use, variance, weights, milestones[stat], raw_data) if show_extremes == "Hide Extreme Probabilities": probs = {k: v for k, v in probs.items() if 0.005 <= v <= 0.99} summary_md = f""" ### 📊 Summary for {player} – {stat.capitalize()} - **Games Played in Sample**: {weights_row['games_played']} - **Per-Game Average**: {hist_mean:.2f} - **Variance**: {variance:.2f} - **Projection Used for Estimation**: {mean_to_use:.2f} {"(Custom)" if mean_sel == "Use Custom Projection" else "(Historical)"} - **Applied Vig**: {vig:.2f}% - **Extreme Probabilities**: {'Shown' if show_extremes == 'Show All' else 'Hidden ( <0.05% or >99%)'} """ hit_rates = [] total_games = len(raw_data) for milestone in milestones[stat]: hits = (raw_data >= milestone).sum() hit_rate = round(hits / total_games, 4) if total_games > 0 else 0.0 hit_rates.append(f"{hit_rate * 100:.2f}%") # Ensure we align the hit rate with the filtered milestones (if extremes were hidden) filtered_milestones = list(probs.keys()) filtered_hit_rates = [hr for i, hr in zip(milestones[stat], hit_rates) if f"{i}+" in filtered_milestones] table = pd.DataFrame({ "Milestone": filtered_milestones, "Probability": [f"{v * 100:.2f}%" for v in probs.values()], "True Odds": [convert_to_american_odds(v) for v in probs.values()], "Odds w/Vig": [adjust_for_vig(v, vig) for v in probs.values()], "Hit Rate": filtered_hit_rates }) return summary_md, table, weights, raw_data def generate_pie(weights, player, stat): fig, ax = plt.subplots() labels = list(weights.keys()) sizes = list(weights.values()) # Separate non-zero and zero-weight indices non_zero_indices = [i for i, s in enumerate(sizes) if s > 0] zero_indices = [i for i, s in enumerate(sizes) if s == 0] # Extract non-zero values for pie chart sizes_non_zero = [sizes[i] for i in non_zero_indices] labels_non_zero = [labels[i] for i in non_zero_indices] wedges, _ = ax.pie(sizes_non_zero, startangle=140, radius=1.0) # Annotate non-zero slices without jitter for i, wedge in enumerate(wedges): angle = (wedge.theta2 + wedge.theta1) / 2. radians = np.deg2rad(angle) x = 1.2 * np.cos(radians) y = 1.2 * np.sin(radians) ax.annotate( f"{labels_non_zero[i]} ({sizes_non_zero[i]*100:.1f}%)", xy=(np.cos(radians), np.sin(radians)), xytext=(x, y), ha='center', va='center', textcoords='data', arrowprops=dict(arrowstyle='-', lw=0.8, color='gray'), fontsize=9 ) ax.set_title(f"Distribution Blend – {player} ({stat})") ax.axis('equal') # Ensure pie is circular return fig def generate_histogram(raw_data, weights, mean, variance): x = np.linspace(0, raw_data.max() + 10, 500) poisson_pdf = poisson.pmf(np.floor(x), mean) nb_r = mean ** 2 / (variance - mean) if variance > mean else 10 nb_p = mean / variance if variance > mean else 0.5 nb_pdf = nbinom.pmf(np.floor(x), nb_r, nb_p) gamma_pdf = gamma.pdf(x, a=mean, scale=variance / mean) lognorm_pdf = np.zeros_like(x) log_data = np.log1p(raw_data[raw_data > 0]) if len(log_data) > 1: lognorm_pdf = lognorm.pdf(x, s=np.std(log_data), scale=np.exp(np.mean(log_data))) blended_pdf = ( weights["Poisson"] * poisson_pdf + weights["Negative Binomial"] * nb_pdf + weights["Gamma-Poisson"] * gamma_pdf + weights["Log-Normal"] * lognorm_pdf ) fig, ax = plt.subplots() ax.hist(raw_data, bins=range(int(raw_data.min()), int(raw_data.max()) + 2), density=True, alpha=0.6) ax.plot(x, blended_pdf, 'k-', label="Blended Model", linewidth=2) ax.set_title("📈 Histogram vs. Blended Model PDF") ax.legend() return fig # === Gradio App === with gr.Blocks(title="🏀 Player Milestone Estimator", css=".green-button {background-color: #28a745 !important; color: white !important; font-weight: bold;}") as demo: gr.Markdown("## 🏀 Player Milestone Probability Estimator") with gr.Row(): log_file = gr.File(label="Upload Game Logs from Snowflake", file_types=[".csv"]) outlier_filter_toggle = gr.Checkbox(label="Filter game logs where projected minutes deviate from actual minutes played by ", value=True) outlier_threshold_input = gr.Slider(label="Minutes Deviation Threshold (%)", minimum=0, maximum=2.0, value=0.9, step=0.01) player_dropdown = gr.Dropdown(label="Select a Player", interactive=True) stat_dropdown = gr.Dropdown(["points", "rebounds", "assists", "threes_made", "steals", "blocks", "turnovers"], label=" Select a Stat") # File and filter logic triggers log_file.change(load_and_prepare_data, [log_file, outlier_filter_toggle, outlier_threshold_input], player_dropdown) outlier_filter_toggle.change(load_and_prepare_data, [log_file, outlier_filter_toggle, outlier_threshold_input], player_dropdown) outlier_threshold_input.change(load_and_prepare_data, [log_file, outlier_filter_toggle, outlier_threshold_input], player_dropdown) submit_button = gr.Button("Submit", elem_classes=["green-button"]) with gr.Row(): mean_sel = gr.Radio(["Use Per-Game Average", "Use Custom Projection"], label="Projection Source", value="Use Per-Game Average") custom_proj = gr.Number(label="Custom Mean (if selected)", precision=2) vig_input = gr.Number(label="Vig (%)", value=4.92, precision=2) extreme_toggle = gr.Radio(["Hide Extreme Probabilities", "Show All"], label="Extreme Probabilities", value="Hide Extreme Probabilities") summary_output = gr.Markdown(visible=False) table_output = gr.Dataframe(visible=False) hist_plot = gr.Plot(visible=False) pie_chart = gr.Plot(visible=False) recent_games_output = gr.Dataframe(visible=False, label="🕒 Recent Game Logs") # Define function that handles updating all outputs def run_all(player, stat, mean_sel, custom_proj, vig_input, extreme_toggle): if not player or not stat: return ( gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False) ) summary_md, table, weights, raw_data = estimate(player, stat, mean_sel, custom_proj, vig_input, extreme_toggle) pie = generate_pie(weights, player, stat) hist = generate_histogram(raw_data, weights, custom_proj if mean_sel == "Use Custom Projection" else raw_data.mean(), raw_data.var()) recent_games = temp_df[temp_df['player'] == player].copy() recent_games['minutes_played'] = round(recent_games['minutes_played'], 1) recent_games['mins_pct_diff'] = (recent_games['mins_pct_diff'] * 100).map(lambda x: f"{x:.2f}%") recent_games = recent_games.sort_values(by='game_date_pacific', ascending=False).head(10) recent_games_display = recent_games[['game_date_pacific', 'minutes_played', 'points', 'rebounds', 'assists', 'threes_made', 'steals', 'blocks', 'turnovers', 'is_minutes_outlier', 'projected_minutes', 'mins_pct_diff']].rename( columns={ "game_date_pacific": "date", "minutes_played": "mins", "threes_made": "made 3's", "turnovers": "tos", "projected_minutes": "proj. mins", "is_minutes_outlier": "outlier" } ) return ( gr.update(value=summary_md, visible=True), gr.update(value=table, visible=True), gr.update(value=pie, visible=True), gr.update(value=hist, visible=True), gr.update(value=recent_games_display, visible=True) ) # Connect all relevant input components to auto-update outputs mean_sel.change(run_all, [player_dropdown, stat_dropdown, mean_sel, custom_proj, vig_input, extreme_toggle], [summary_output, table_output, pie_chart, hist_plot, recent_games_output]) custom_proj.change(run_all, [player_dropdown, stat_dropdown, mean_sel, custom_proj, vig_input, extreme_toggle], [summary_output, table_output, pie_chart, hist_plot, recent_games_output]) vig_input.change(run_all, [player_dropdown, stat_dropdown, mean_sel, custom_proj, vig_input, extreme_toggle], [summary_output, table_output, pie_chart, hist_plot, recent_games_output]) extreme_toggle.change(run_all, [player_dropdown, stat_dropdown, mean_sel, custom_proj, vig_input, extreme_toggle], [summary_output, table_output, pie_chart, hist_plot, recent_games_output]) # Still allow user to manually trigger with submit button submit_button.click(run_all, [player_dropdown, stat_dropdown, mean_sel, custom_proj, vig_input, extreme_toggle], [summary_output, table_output, pie_chart, hist_plot, recent_games_output]) demo.launch()