Multichem commited on
Commit
8a5a3f1
·
1 Parent(s): 3b90fa6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +275 -5
app.py CHANGED
@@ -34,6 +34,11 @@ def init_conn():
34
  gspreadcon = init_conn()
35
 
36
  master_hold = 'https://docs.google.com/spreadsheets/d/1NmKa-b-2D3w7rRxwMPSchh31GKfJ1XcDI2GU8rXWnHI/edit#gid=195454038'
 
 
 
 
 
37
 
38
  @st.cache_resource(ttl=300)
39
  def pull_baselines():
@@ -48,24 +53,29 @@ def pull_baselines():
48
  prop_table = prop_display[['Player', 'Position', 'Team', 'Opp', 'Team_Total', 'Player SOG', 'Player Goals', 'Player Assists',
49
  'Player TP', 'Player Blocks', 'Player Saves']]
50
 
 
 
 
 
 
51
  worksheet = sh.worksheet('Timestamp')
52
  timestamp = worksheet.acell('A1').value
53
 
54
- return prop_table, timestamp
55
 
56
  def convert_df_to_csv(df):
57
  return df.to_csv().encode('utf-8')
58
 
59
- prop_display, timestamp = pull_baselines()
60
  t_stamp = f"Last Update: " + str(timestamp) + f" CST"
61
 
62
- tab1, tab2 = st.tabs(["Player Stat Table", 'Game Betting Model'])
63
 
64
  with tab1:
65
  st.info(t_stamp)
66
  if st.button("Reset Data", key='reset1'):
67
  st.cache_data.clear()
68
- prop_display, timestamp = pull_baselines()
69
  prop_frame = prop_display
70
  st.dataframe(prop_frame.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
71
 
@@ -78,4 +88,264 @@ with tab1:
78
  )
79
 
80
  with tab2:
81
- st.info('Coming soon!')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  gspreadcon = init_conn()
35
 
36
  master_hold = 'https://docs.google.com/spreadsheets/d/1NmKa-b-2D3w7rRxwMPSchh31GKfJ1XcDI2GU8rXWnHI/edit#gid=195454038'
37
+ prop_table_options = ['SOG', 'points', 'blocked_shots', 'assists']
38
+ prop_format = {'L5 Success': '{:.2%}', 'L10_Success': '{:.2%}', 'L20_success': '{:.2%}', 'Matchup Boost': '{:.2%}', 'Trending Over': '{:.2%}', 'Trending Under': '{:.2%}',
39
+ 'Implied Over': '{:.2%}', 'Implied Under': '{:.2%}', 'Over Edge': '{:.2%}', 'Under Edge': '{:.2%}'}
40
+ all_sim_vars = ['SOG', 'points', 'blocked_shots', 'assists']
41
+ sim_all_hold = pd.DataFrame(columns=['Player', 'Prop type', 'Prop', 'Mean_Outcome', 'Imp Over', 'Over%', 'Imp Under', 'Under%', 'Bet?', 'Edge'])
42
 
43
  @st.cache_resource(ttl=300)
44
  def pull_baselines():
 
53
  prop_table = prop_display[['Player', 'Position', 'Team', 'Opp', 'Team_Total', 'Player SOG', 'Player Goals', 'Player Assists',
54
  'Player TP', 'Player Blocks', 'Player Saves']]
55
 
56
+ worksheet = sh.worksheet('prop_trends')
57
+ raw_display = pd.DataFrame(worksheet.get_all_records())
58
+ raw_display.replace('', np.nan, inplace=True)
59
+ prop_trends = raw_display.dropna(subset='Player')
60
+
61
  worksheet = sh.worksheet('Timestamp')
62
  timestamp = worksheet.acell('A1').value
63
 
64
+ return prop_table, prop_trends, timestamp
65
 
66
  def convert_df_to_csv(df):
67
  return df.to_csv().encode('utf-8')
68
 
69
+ prop_display, prop_trends, timestamp = pull_baselines()
70
  t_stamp = f"Last Update: " + str(timestamp) + f" CST"
71
 
72
+ tab1, tab2, tab3 = st.tabs(["Player Stat Table", 'Prop Trend Table', 'Stat Specific Simulations'])
73
 
74
  with tab1:
75
  st.info(t_stamp)
76
  if st.button("Reset Data", key='reset1'):
77
  st.cache_data.clear()
78
+ prop_display, prop_trends, timestamp = pull_baselines()
79
  prop_frame = prop_display
80
  st.dataframe(prop_frame.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
81
 
 
88
  )
89
 
90
  with tab2:
91
+ st.info(t_stamp)
92
+ if st.button("Reset Data", key='reset3'):
93
+ st.cache_data.clear()
94
+ prop_display, prop_trends, timestamp = pull_baselines()
95
+ t_stamp = f"Last Update: " + str(timestamp) + f" CST"
96
+ split_var5 = st.radio("Would you like to view all teams or specific ones?", ('All', 'Specific Teams'), key='split_var5')
97
+ if split_var5 == 'Specific Teams':
98
+ team_var5 = st.multiselect('Which teams would you like to include in the tables?', options = prop_trends['Team'].unique(), key='team_var5')
99
+ elif split_var5 == 'All':
100
+ team_var5 = prop_trends.Team.values.tolist()
101
+ prop_type_var2 = st.selectbox('Select type of prop are you wanting to view', options = prop_table_options)
102
+ prop_frame_disp = prop_trends[prop_trends['Team'].isin(team_var5)]
103
+ prop_frame_disp = prop_frame_disp[prop_frame_disp['prop_type'] == prop_type_var2]
104
+ prop_frame_disp = prop_frame_disp.set_index('Player')
105
+ prop_frame_disp = prop_frame_disp.sort_values(by='Trending Over', ascending=False)
106
+ st.dataframe(prop_frame_disp.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(prop_format, precision=2), use_container_width = True)
107
+ st.download_button(
108
+ label="Export Prop Trends Model",
109
+ data=convert_df_to_csv(prop_frame),
110
+ file_name='NHL_prop_trends_export.csv',
111
+ mime='text/csv',
112
+ )
113
+
114
+ with tab3:
115
+ st.info(t_stamp)
116
+ st.info('The Over and Under percentages are a composite percentage based on simulations, historical performance, and implied probabilities, and may be different than you would expect based purely on the median projection. Likewise, the Edge of a bet is not the only indicator of if you should make the bet or not as the suggestion is using a base acceptable threshold to determine how much edge you should have for each stat category.')
117
+ if st.button("Reset Data/Load Data", key='reset5'):
118
+ st.cache_data.clear()
119
+ prop_display, prop_trends, timestamp = pull_baselines()
120
+ t_stamp = f"Last Update: " + str(timestamp) + f" CST"
121
+ col1, col2 = st.columns([1, 5])
122
+
123
+ with col2:
124
+ df_hold_container = st.empty()
125
+ info_hold_container = st.empty()
126
+ plot_hold_container = st.empty()
127
+ export_container = st.empty()
128
+
129
+ with col1:
130
+ prop_type_var = st.selectbox('Select prop category', options = ['All Props', 'SOG', 'points', 'blocked_shots', 'assists'])
131
+ if prop_type_var == 'All Props':
132
+ st.info('please note that the All Props run can take some time, you will see progress as tables show up in the sim area to the right')
133
+
134
+ if st.button('Simulate Prop Category'):
135
+ with col2:
136
+ with df_hold_container.container():
137
+ if prop_type_var == 'All Props':
138
+ for prop in all_sim_vars:
139
+
140
+ prop_df = prop_trends[['Player', 'over_prop', 'over_line', 'under_line', 'prop_type']]
141
+ prop_df = prop_df.loc[prop_df['prop_type'] == prop]
142
+ prop_df = prop_df[['Player', 'over_prop', 'over_line', 'under_line']]
143
+ prop_df.rename(columns={"over_prop": "Prop"}, inplace = True)
144
+ prop_df = prop_df.loc[prop_df['Prop'] != 0]
145
+ st.table(prop_df)
146
+ prop_df['Over'] = np.where(prop_df['over_line'] < 0, (-(prop_df['over_line'])/((-(prop_df['over_line']))+101)), 101/(prop_df['over_line']+101))
147
+ prop_df['Under'] = np.where(prop_df['under_line'] < 0, (-(prop_df['under_line'])/((-(prop_df['under_line']))+101)), 101/(prop_df['under_line']+101))
148
+ df = pd.merge(prop_display, prop_df, how='left', left_on=['Player'], right_on = ['Player'])
149
+
150
+ prop_dict = dict(zip(df.Player, df.Prop))
151
+ over_dict = dict(zip(df.Player, df.Over))
152
+ under_dict = dict(zip(df.Player, df.Under))
153
+
154
+ total_sims = 5000
155
+
156
+ df.replace("", 0, inplace=True)
157
+
158
+ if prop == 'points':
159
+ df['Median'] = df['Player TP']
160
+ elif prop == 'SOG':
161
+ df['Median'] = df['Player SOG']
162
+ elif prop == 'assists':
163
+ df['Median'] = df['Player Assists']
164
+ elif prop == 'blocked_shots':
165
+ df['Median'] = df['Player Blocks']
166
+
167
+ flex_file = df
168
+ flex_file['Floor'] = (flex_file['Median'] * .15)
169
+ flex_file['Ceiling'] = flex_file['Median'] + (flex_file['Median'] * .85)
170
+ flex_file['STD'] = (flex_file['Median']/3)
171
+ flex_file['Prop'] = flex_file['Player'].map(prop_dict)
172
+ flex_file = flex_file[['Player', 'Prop', 'Floor', 'Median', 'Ceiling', 'STD']]
173
+
174
+ hold_file = flex_file
175
+ overall_file = flex_file
176
+ prop_file = flex_file
177
+
178
+ overall_players = overall_file[['Player']]
179
+
180
+ for x in range(0,total_sims):
181
+ prop_file[x] = prop_file['Prop']
182
+
183
+ prop_file = prop_file.drop(['Player', 'Prop', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
184
+
185
+ for x in range(0,total_sims):
186
+ overall_file[x] = np.random.normal(overall_file['Median'],overall_file['STD'])
187
+
188
+ overall_file=overall_file.drop(['Player', 'Prop', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
189
+
190
+ players_only = hold_file[['Player']]
191
+
192
+ player_outcomes = pd.merge(players_only, overall_file, left_index=True, right_index=True)
193
+
194
+ prop_check = (overall_file - prop_file)
195
+
196
+ players_only['Mean_Outcome'] = overall_file.mean(axis=1)
197
+ players_only['10%'] = overall_file.quantile(0.1, axis=1)
198
+ players_only['90%'] = overall_file.quantile(0.9, axis=1)
199
+ players_only['Over'] = prop_check[prop_check > 0].count(axis=1)/float(total_sims)
200
+ players_only['Imp Over'] = players_only['Player'].map(over_dict)
201
+ players_only['Over%'] = players_only[["Over", "Imp Over"]].mean(axis=1)
202
+ players_only['Under'] = prop_check[prop_check < 0].count(axis=1)/float(total_sims)
203
+ players_only['Imp Under'] = players_only['Player'].map(under_dict)
204
+ players_only['Under%'] = players_only[["Under", "Imp Under"]].mean(axis=1)
205
+ players_only['Prop'] = players_only['Player'].map(prop_dict)
206
+ players_only['Prop_avg'] = players_only['Prop'].mean() / 100
207
+ players_only['prop_threshold'] = .10
208
+ players_only = players_only.loc[players_only['Mean_Outcome'] > 0]
209
+ players_only['Over_diff'] = players_only['Over%'] - players_only['Imp Over']
210
+ players_only['Under_diff'] = players_only['Under%'] - players_only['Imp Under']
211
+ players_only['Bet_check'] = np.where(players_only['Over_diff'] > players_only['Under_diff'], players_only['Over_diff'] , players_only['Under_diff'])
212
+ players_only['Bet_suggest'] = np.where(players_only['Over_diff'] > players_only['Under_diff'], "Over" , "Under")
213
+ players_only['Bet?'] = np.where(players_only['Bet_check'] >= players_only['prop_threshold'], players_only['Bet_suggest'], "No Bet")
214
+ players_only['Edge'] = players_only['Bet_check']
215
+ players_only['Prop type'] = prop
216
+
217
+ players_only['Player'] = hold_file[['Player']]
218
+
219
+ leg_outcomes = players_only[['Player', 'Prop type', 'Prop', 'Mean_Outcome', 'Imp Over', 'Over%', 'Imp Under', 'Under%', 'Bet?', 'Edge']]
220
+
221
+ sim_all_hold = pd.concat([sim_all_hold, leg_outcomes], ignore_index=True)
222
+
223
+ final_outcomes = sim_all_hold
224
+
225
+ elif prop_type_var != 'All Props':
226
+ if prop_type_var == "SOG":
227
+ prop_df = prop_frame[['Player', 'over_prop', 'over_line', 'under_line', 'prop_type']]
228
+ prop_df = prop_df.loc[prop_df['prop_type'] == 'SOG']
229
+ prop_df = prop_df[['Player', 'over_prop', 'over_line', 'under_line']]
230
+ prop_df.rename(columns={"over_prop": "Prop"}, inplace = True)
231
+ prop_df = prop_df.loc[prop_df['Prop'] != 0]
232
+ st.table(prop_df)
233
+ prop_df['Over'] = np.where(prop_df['over_line'] < 0, (-(prop_df['over_line'])/((-(prop_df['over_line']))+101)), 101/(prop_df['over_line']+101))
234
+ prop_df['Under'] = np.where(prop_df['under_line'] < 0, (-(prop_df['under_line'])/((-(prop_df['under_line']))+101)), 101/(prop_df['under_line']+101))
235
+ df = pd.merge(prop_display, prop_df, how='left', left_on=['Player'], right_on = ['Player'])
236
+ elif prop_type_var == "points":
237
+ prop_df = prop_frame[['Player', 'over_prop', 'over_line', 'under_line', 'prop_type']]
238
+ prop_df = prop_df.loc[prop_df['prop_type'] == 'points']
239
+ prop_df = prop_df[['Player', 'over_prop', 'over_line', 'under_line']]
240
+ prop_df.rename(columns={"over_prop": "Prop"}, inplace = True)
241
+ prop_df = prop_df.loc[prop_df['Prop'] != 0]
242
+ st.table(prop_df)
243
+ prop_df['Over'] = np.where(prop_df['over_line'] < 0, (-(prop_df['over_line'])/((-(prop_df['over_line']))+101)), 101/(prop_df['over_line']+101))
244
+ prop_df['Under'] = np.where(prop_df['under_line'] < 0, (-(prop_df['under_line'])/((-(prop_df['under_line']))+101)), 101/(prop_df['under_line']+101))
245
+ df = pd.merge(prop_display, prop_df, how='left', left_on=['Player'], right_on = ['Player'])
246
+ elif prop_type_var == "assists":
247
+ prop_df = prop_frame[['Player', 'over_prop', 'over_line', 'under_line', 'prop_type']]
248
+ prop_df = prop_df.loc[prop_df['prop_type'] == 'assists']
249
+ prop_df = prop_df[['Player', 'over_prop', 'over_line', 'under_line']]
250
+ prop_df.rename(columns={"over_prop": "Prop"}, inplace = True)
251
+ prop_df = prop_df.loc[prop_df['Prop'] != 0]
252
+ st.table(prop_df)
253
+ prop_df['Over'] = np.where(prop_df['over_line'] < 0, (-(prop_df['over_line'])/((-(prop_df['over_line']))+101)), 101/(prop_df['over_line']+101))
254
+ prop_df['Under'] = np.where(prop_df['under_line'] < 0, (-(prop_df['under_line'])/((-(prop_df['under_line']))+101)), 101/(prop_df['under_line']+101))
255
+ df = pd.merge(prop_display, prop_df, how='left', left_on=['Player'], right_on = ['Player'])
256
+ elif prop_type_var == "blocked_shots":
257
+ prop_df = prop_frame[['Player', 'over_prop', 'over_line', 'under_line', 'prop_type']]
258
+ prop_df = prop_df.loc[prop_df['prop_type'] == 'blocked_shots']
259
+ prop_df = prop_df[['Player', 'over_prop', 'over_line', 'under_line']]
260
+ prop_df.rename(columns={"over_prop": "Prop"}, inplace = True)
261
+ prop_df = prop_df.loc[prop_df['Prop'] != 0]
262
+ st.table(prop_df)
263
+ prop_df['Over'] = np.where(prop_df['over_line'] < 0, (-(prop_df['over_line'])/((-(prop_df['over_line']))+101)), 101/(prop_df['over_line']+101))
264
+ prop_df['Under'] = np.where(prop_df['under_line'] < 0, (-(prop_df['under_line'])/((-(prop_df['under_line']))+101)), 101/(prop_df['under_line']+101))
265
+ df = pd.merge(prop_display, prop_df, how='left', left_on=['Player'], right_on = ['Player'])
266
+
267
+ prop_dict = dict(zip(df.Player, df.Prop))
268
+ over_dict = dict(zip(df.Player, df.Over))
269
+ under_dict = dict(zip(df.Player, df.Under))
270
+
271
+ total_sims = 5000
272
+
273
+ df.replace("", 0, inplace=True)
274
+
275
+ if prop == 'points':
276
+ df['Median'] = df['Player TP']
277
+ elif prop == 'SOG':
278
+ df['Median'] = df['Player SOG']
279
+ elif prop == 'assists':
280
+ df['Median'] = df['Player Assists']
281
+ elif prop == 'blocked_shots':
282
+ df['Median'] = df['Player Blocks']
283
+
284
+ flex_file = df
285
+ flex_file['Floor'] = (flex_file['Median'] * .15)
286
+ flex_file['Ceiling'] = flex_file['Median'] + (flex_file['Median'] * .85)
287
+ flex_file['STD'] = (flex_file['Median']/3)
288
+ flex_file['Prop'] = flex_file['Player'].map(prop_dict)
289
+ flex_file = flex_file[['Player', 'Prop', 'Floor', 'Median', 'Ceiling', 'STD']]
290
+
291
+ hold_file = flex_file
292
+ overall_file = flex_file
293
+ prop_file = flex_file
294
+
295
+ overall_players = overall_file[['Player']]
296
+
297
+ for x in range(0,total_sims):
298
+ prop_file[x] = prop_file['Prop']
299
+
300
+ prop_file = prop_file.drop(['Player', 'Prop', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
301
+
302
+ for x in range(0,total_sims):
303
+ overall_file[x] = np.random.normal(overall_file['Median'],overall_file['STD'])
304
+
305
+ overall_file=overall_file.drop(['Player', 'Prop', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
306
+
307
+ players_only = hold_file[['Player']]
308
+
309
+ player_outcomes = pd.merge(players_only, overall_file, left_index=True, right_index=True)
310
+
311
+ prop_check = (overall_file - prop_file)
312
+
313
+ players_only['Mean_Outcome'] = overall_file.mean(axis=1)
314
+ players_only['10%'] = overall_file.quantile(0.1, axis=1)
315
+ players_only['90%'] = overall_file.quantile(0.9, axis=1)
316
+ players_only['Over'] = prop_check[prop_check > 0].count(axis=1)/float(total_sims)
317
+ players_only['Imp Over'] = players_only['Player'].map(over_dict)
318
+ players_only['Over%'] = players_only[["Over", "Imp Over"]].mean(axis=1)
319
+ players_only['Under'] = prop_check[prop_check < 0].count(axis=1)/float(total_sims)
320
+ players_only['Imp Under'] = players_only['Player'].map(under_dict)
321
+ players_only['Under%'] = players_only[["Under", "Imp Under"]].mean(axis=1)
322
+ players_only['Prop'] = players_only['Player'].map(prop_dict)
323
+ players_only['Prop_avg'] = players_only['Prop'].mean() / 100
324
+ players_only['prop_threshold'] = .10
325
+ players_only = players_only.loc[players_only['Mean_Outcome'] > 0]
326
+ players_only['Over_diff'] = players_only['Over%'] - players_only['Imp Over']
327
+ players_only['Under_diff'] = players_only['Under%'] - players_only['Imp Under']
328
+ players_only['Bet_check'] = np.where(players_only['Over_diff'] > players_only['Under_diff'], players_only['Over_diff'] , players_only['Under_diff'])
329
+ players_only['Bet_suggest'] = np.where(players_only['Over_diff'] > players_only['Under_diff'], "Over" , "Under")
330
+ players_only['Bet?'] = np.where(players_only['Bet_check'] >= players_only['prop_threshold'], players_only['Bet_suggest'], "No Bet")
331
+ players_only['Edge'] = players_only['Bet_check']
332
+
333
+ players_only['Player'] = hold_file[['Player']]
334
+
335
+ final_outcomes = players_only[['Player', 'Prop', 'Mean_Outcome', 'Imp Over', 'Over%', 'Imp Under', 'Under%', 'Bet?', 'Edge']]
336
+
337
+ final_outcomes = final_outcomes[final_outcomes['Prop'] > 0]
338
+ final_outcomes = final_outcomes.sort_values(by='Edge', ascending=False)
339
+
340
+ with df_hold_container:
341
+ df_hold_container = st.empty()
342
+ st.dataframe(final_outcomes.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
343
+ with export_container:
344
+ export_container = st.empty()
345
+ st.download_button(
346
+ label="Export Projections",
347
+ data=convert_df_to_csv(final_outcomes),
348
+ file_name='Nba_prop_proj.csv',
349
+ mime='text/csv',
350
+ key='prop_proj',
351
+ )