BraydenMoore commited on
Commit
79b4417
1 Parent(s): 1163916

Update main.py

Browse files
Files changed (2) hide show
  1. get_record.py +102 -6
  2. main.py +1 -1
get_record.py CHANGED
@@ -1,4 +1,4 @@
1
- from datetime import datetime
2
  import numpy as np
3
  import pandas as pd
4
  pd.set_option('chained_assignment',None)
@@ -47,17 +47,103 @@ predictions_df['over_under_correct'] = (predictions_df['predicted_over_under']==
47
  predictions_df['over_under_incorrect'] = ((predictions_df['predicted_over_under']!=predictions_df['over_under']) & (predictions_df['over_under']!='Push'))
48
  predictions_df['over_under_push'] = (predictions_df['over_under']=='Push')
49
 
50
- predictions_df['winner_return'] = [ao-1 if (pa and wc) else ho-1 if (ph and wc) else -1 for ao,ho,pa,ph,wc in predictions_df[['Away Odds Close','Home Odds Close','picked_away','picked_home','winner_correct']].values]
51
- predictions_df['over_under_return'] = [0.91 if ouc else -1 for ouc in predictions_df['over_under_correct']]
 
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  threshold = 0.6
54
 
55
  winners_correct = predictions_df.loc[predictions_df['predicted_winner_probability']>threshold, 'winner_correct'].sum()
 
56
  winners_incorrect = predictions_df.loc[predictions_df['predicted_winner_probability']>threshold,'winner_incorrect'].sum()
57
  winners_tie = predictions_df.loc[predictions_df['predicted_winner_probability']>threshold,'winner_tie'].sum()
58
  winners_return = predictions_df.loc[predictions_df['predicted_winner_probability']>threshold, 'winner_return'].sum()
59
 
60
  over_unders_correct = predictions_df.loc[predictions_df['predicted_over_under_probability']>threshold,'over_under_correct'].sum()
 
61
  over_unders_incorrect = predictions_df.loc[predictions_df['predicted_over_under_probability']>threshold,'over_under_incorrect'].sum()
62
  over_unders_push = predictions_df.loc[predictions_df['predicted_over_under_probability']>threshold,'over_under_push'].sum()
63
  over_unders_return = predictions_df.loc[predictions_df['predicted_over_under_probability']>threshold,'over_under_return'].sum()
@@ -65,15 +151,25 @@ over_unders_return = predictions_df.loc[predictions_df['predicted_over_under_pro
65
  max_date = predictions_df['game_date'].max()
66
  latest_game = pd.Timestamp(max_date).strftime("%A, %m/%d")
67
 
 
 
 
 
 
 
 
 
68
  record = {"winners_correct":str(winners_correct),
69
  "winners_incorrect":str(winners_incorrect),
70
  "winners_tie":("-"+str(winners_tie) if winners_tie>0 else ''),
71
- "winners_return":str(round(winners_return,1))+"x return",
72
  "over_unders_correct":str(over_unders_correct),
73
  "over_unders_incorrect":str(over_unders_incorrect),
74
  "over_unders_push":("-"+str(over_unders_push) if over_unders_push>0 else ''),
75
- "over_unders_return":str(round(over_unders_return,1))+"x return",
76
- "latest_game":latest_game}
 
 
77
 
78
  import json
79
  with open('Source/Data/record.json', 'w') as f:
 
1
+ from datetime import date, datetime
2
  import numpy as np
3
  import pandas as pd
4
  pd.set_option('chained_assignment',None)
 
47
  predictions_df['over_under_incorrect'] = ((predictions_df['predicted_over_under']!=predictions_df['over_under']) & (predictions_df['over_under']!='Push'))
48
  predictions_df['over_under_push'] = (predictions_df['over_under']=='Push')
49
 
50
+ predictions_df['winner_return'] = [0 if tie else ao-1 if (pa and wc) else ho-1 if (ph and wc) else -1 for ao,ho,pa,ph,wc,tie in predictions_df[['Away Odds Close','Home Odds Close','picked_away','picked_home','winner_correct','winner_tie']].values]
51
+ predictions_df['over_under_return'] = [0 if push else 0.91 if ouc else -1 for ouc,push in predictions_df[['over_under_correct','over_under_push']].values]
52
+ predictions_df = predictions_df.loc[predictions_df['game_date']>datetime(year=2023,month=9,day=19)]
53
 
54
+ # Save
55
+ predictions_df.to_csv('Source/Data/predictions.csv')
56
+ bins = np.arange(0.5, 1.05, 0.05)
57
+ bin_midpoints = [(bins[i] + bins[i+1]) / 2 for i in range(len(bins) - 1)]
58
+
59
+ predictions_df['winner_probability_bin'] = pd.cut(predictions_df['predicted_winner_probability'], bins=bins, labels=bin_midpoints)
60
+ predictions_df['over_under_probability_bin'] = pd.cut(predictions_df['predicted_over_under_probability'], bins=bins, labels=bin_midpoints)
61
+ winner_binned = predictions_df.groupby('winner_probability_bin')['winner_correct'].mean().reset_index()
62
+ over_under_binned = predictions_df.groupby('over_under_probability_bin')['over_under_correct'].mean().reset_index()
63
+
64
+ ## plot
65
+
66
+ import matplotlib.pyplot as plt
67
+ import numpy as np
68
+
69
+ def style_plot(ax, title):
70
+ ax.set_facecolor('black')
71
+ ax.set_title(title, color='white')
72
+ ax.set_xlabel('MARCI Predicted Probability', color='white')
73
+ ax.set_ylabel('Actual Probability', color='white')
74
+ ax.tick_params(axis='x', colors='white')
75
+ ax.tick_params(axis='y', colors='white')
76
+ ax.spines['bottom'].set_color('white')
77
+ ax.spines['top'].set_color('white')
78
+ ax.spines['left'].set_color('white')
79
+ ax.spines['right'].set_color('white')
80
+ #ax.grid(True, linestyle='--', linewidth=0.5, color='grey')
81
+ ax.set_ylim((0,1.1))
82
+
83
+ def add_identity_line(ax, max_x):
84
+ x = np.linspace(0.5, max_x, 100)
85
+ ax.plot(x, x, linestyle='--', color='grey')
86
+
87
+ def add_best_fit_line(ax, x_values, y_values):
88
+ x_values = x_values.astype('float64')
89
+ y_values = y_values.astype('float64')
90
+ mask = ~np.isnan(x_values) & ~np.isnan(y_values)
91
+ x_values = x_values[mask]
92
+ y_values = y_values[mask]
93
+ coef = np.polyfit(x_values, y_values, 1)
94
+ poly1d_fn = np.poly1d(coef)
95
+ ax.plot(x_values, poly1d_fn(x_values), color='green')
96
+ corr = np.corrcoef(x_values, y_values)[0,1]
97
+ max_x = np.max(x_values)
98
+ max_y = poly1d_fn(max_x)
99
+ #ax.text(max_x, max_y, f'Corr: {corr:.2f}', color='green')
100
+
101
+ # Create the Winner scatter plot
102
+ x_values_winner = winner_binned['winner_probability_bin']
103
+ y_values_winner = winner_binned['winner_correct']
104
+ fig1 = plt.figure(facecolor='black')
105
+ ax1 = fig1.add_subplot(1, 1, 1)
106
+ ax1.scatter(x_values_winner,
107
+ y_values_winner,
108
+ color=(0/255, 128/255, 0/255), s=100, marker='o')
109
+ add_identity_line(ax1, predictions_df['predicted_winner_probability'].max())
110
+ add_best_fit_line(ax1, predictions_df['predicted_winner_probability'], predictions_df['winner_correct'])
111
+ line, = ax1.plot([], [], linestyle='--', color='grey')
112
+ marci_line, = ax1.plot([], [], color='green')
113
+ ax1.legend([line, marci_line], ['Perfect Model', 'MARCI'], loc='upper left', facecolor='black', edgecolor='white', labelcolor='white')
114
+ style_plot(ax1, 'Winner Predictions')
115
+ plt.savefig('Static/Winner_Predictions_dark.png', facecolor='black')
116
+ plt.close(fig1)
117
+
118
+ # Create the Over/Under scatter plot
119
+ x_values_over_under = over_under_binned['over_under_probability_bin']
120
+ y_values_over_under = over_under_binned['over_under_correct']
121
+ fig2 = plt.figure(facecolor='black')
122
+ ax2 = fig2.add_subplot(1, 1, 1)
123
+ ax2.scatter(x_values_over_under,
124
+ y_values_over_under,
125
+ color=(0/255, 128/255, 0/255), s=100, marker='o')
126
+ add_identity_line(ax2, predictions_df['predicted_over_under_probability'].max())
127
+ add_best_fit_line(ax2, predictions_df['predicted_over_under_probability'], predictions_df['over_under_correct'])
128
+ line, = ax2.plot([], [], linestyle='--', color='grey')
129
+ marci_line, = ax2.plot([], [], color='green')
130
+ ax2.legend([line, marci_line], ['Perfect Model', 'MARCI'], loc='upper left', facecolor='black', edgecolor='white', labelcolor='white')
131
+ style_plot(ax2, 'Over/Under Predictions')
132
+ plt.savefig('Static/Over_Under_Predictions_dark.png', facecolor='black')
133
+ plt.close(fig2)
134
+
135
+
136
+ ## get record
137
  threshold = 0.6
138
 
139
  winners_correct = predictions_df.loc[predictions_df['predicted_winner_probability']>threshold, 'winner_correct'].sum()
140
+ winners_accuracy = predictions_df.loc[predictions_df['predicted_winner_probability']>threshold, 'winner_correct'].mean()
141
  winners_incorrect = predictions_df.loc[predictions_df['predicted_winner_probability']>threshold,'winner_incorrect'].sum()
142
  winners_tie = predictions_df.loc[predictions_df['predicted_winner_probability']>threshold,'winner_tie'].sum()
143
  winners_return = predictions_df.loc[predictions_df['predicted_winner_probability']>threshold, 'winner_return'].sum()
144
 
145
  over_unders_correct = predictions_df.loc[predictions_df['predicted_over_under_probability']>threshold,'over_under_correct'].sum()
146
+ over_unders_accuracy = predictions_df.loc[predictions_df['predicted_over_under_probability']>threshold,'over_under_correct'].mean()
147
  over_unders_incorrect = predictions_df.loc[predictions_df['predicted_over_under_probability']>threshold,'over_under_incorrect'].sum()
148
  over_unders_push = predictions_df.loc[predictions_df['predicted_over_under_probability']>threshold,'over_under_push'].sum()
149
  over_unders_return = predictions_df.loc[predictions_df['predicted_over_under_probability']>threshold,'over_under_return'].sum()
 
151
  max_date = predictions_df['game_date'].max()
152
  latest_game = pd.Timestamp(max_date).strftime("%A, %m/%d")
153
 
154
+ ## get binom prob
155
+ from scipy.stats import binom
156
+
157
+ def compare_to_coinflip(c,n):
158
+ prob_fewer = binom.cdf(c, n, 0.5)
159
+ prob_more = 1 - prob_fewer
160
+ return f"{round(prob_more*100,1)}% chance of equal or better performance by flipping a coin."
161
+
162
  record = {"winners_correct":str(winners_correct),
163
  "winners_incorrect":str(winners_incorrect),
164
  "winners_tie":("-"+str(winners_tie) if winners_tie>0 else ''),
165
+ "winners_return": str(round(winners_accuracy*100,1))+"% accuracy, " + str(round(winners_return,1))+"x return",
166
  "over_unders_correct":str(over_unders_correct),
167
  "over_unders_incorrect":str(over_unders_incorrect),
168
  "over_unders_push":("-"+str(over_unders_push) if over_unders_push>0 else ''),
169
+ "over_unders_return": str(round(over_unders_accuracy*100,1))+"% accuracy, " + str(round(over_unders_return,1))+"x return",
170
+ "latest_game":latest_game,
171
+ "over_unders_binom":compare_to_coinflip(over_unders_correct, (over_unders_incorrect+over_unders_correct)),
172
+ "winners_binom":compare_to_coinflip(winners_correct, (winners_incorrect+winners_correct))}
173
 
174
  import json
175
  with open('Source/Data/record.json', 'w') as f:
main.py CHANGED
@@ -23,7 +23,7 @@ app.secret_key = 'green-flounder'
23
  # get week, season
24
  current_week, season = predict.get_week()
25
  current_games = predict.get_games(current_week)[['Date','Away Team','Home Team']]
26
- available_weeks = list(range(current_week+1))[2:]
27
  available_weeks.reverse()
28
 
29
  # load current data by default
 
23
  # get week, season
24
  current_week, season = predict.get_week()
25
  current_games = predict.get_games(current_week)[['Date','Away Team','Home Team']]
26
+ available_weeks = list(range(current_week+1))[3:]
27
  available_weeks.reverse()
28
 
29
  # load current data by default