File size: 16,525 Bytes
cafc91a
 
 
81cc526
 
 
 
 
 
 
 
f61ed1d
81cc526
d8774fb
 
 
 
 
81cc526
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8e68af
81cc526
 
cafc91a
81cc526
 
cafc91a
81cc526
 
 
 
cafc91a
 
81cc526
d8e68af
cafc91a
d8e68af
 
cafc91a
81cc526
 
cafc91a
81cc526
cafc91a
81cc526
 
 
 
 
 
 
 
 
 
 
d8774fb
f61ed1d
d8774fb
 
81cc526
d8774fb
81cc526
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8e68af
 
 
 
 
 
 
 
 
 
 
 
81cc526
d8e68af
81cc526
 
d8e68af
 
81cc526
 
 
 
 
 
39039a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81cc526
39039a4
81cc526
 
39039a4
81cc526
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8e68af
81cc526
 
 
39039a4
81cc526
d8e68af
81cc526
 
 
d8774fb
81cc526
39039a4
cafc91a
 
 
81cc526
d8e68af
39039a4
81cc526
 
 
 
 
 
 
 
 
 
d8e68af
81cc526
39039a4
81cc526
39039a4
 
 
 
 
 
d8e68af
39039a4
d8e68af
81cc526
 
 
d8e68af
 
d8774fb
bcbb8bf
d8e68af
cafc91a
d8e68af
cafc91a
 
 
 
 
 
d8e68af
 
 
81cc526
 
 
 
d8e68af
 
81cc526
 
39039a4
 
d8e68af
39039a4
d8e68af
39039a4
d8e68af
39039a4
d8e68af
39039a4
 
 
d8e68af
81cc526
39039a4
cafc91a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
## == 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()