Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -42,411 +42,37 @@ master_hold = 'https://docs.google.com/spreadsheets/d/1Yq0vGriWK-bS79e-bD6_u9pqr
|
|
| 42 |
@st.cache_resource(ttl = 300)
|
| 43 |
def init_baselines():
|
| 44 |
sh = gcservice_account.open_by_url(master_hold)
|
| 45 |
-
worksheet = sh.worksheet('Betting Model Clean')
|
| 46 |
-
raw_display = pd.DataFrame(worksheet.get_all_records())
|
| 47 |
-
raw_display.replace('#DIV/0!', np.nan, inplace=True)
|
| 48 |
-
game_model = raw_display.dropna()
|
| 49 |
-
|
| 50 |
-
worksheet = sh.worksheet('DK_Build_Up')
|
| 51 |
-
raw_display = pd.DataFrame(worksheet.get_all_records())
|
| 52 |
-
raw_display.replace('', np.nan, inplace=True)
|
| 53 |
-
raw_display.rename(columns={"Name": "Player"}, inplace = True)
|
| 54 |
-
raw_display = raw_display[['Player', 'Position', 'Team', 'Opp', 'Minutes', '3P', 'Points', 'Rebounds', 'Assists', 'Steals', 'Blocks', 'Turnovers', 'Fantasy']]
|
| 55 |
-
player_stats = raw_display[raw_display['Minutes'] > 0]
|
| 56 |
-
|
| 57 |
-
worksheet = sh.worksheet('Timestamp')
|
| 58 |
-
timestamp = worksheet.acell('A1').value
|
| 59 |
|
| 60 |
-
worksheet = sh.worksheet('
|
| 61 |
raw_display = pd.DataFrame(worksheet.get_all_records())
|
| 62 |
raw_display.replace('', np.nan, inplace=True)
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
return
|
| 66 |
|
| 67 |
def convert_df_to_csv(df):
|
| 68 |
return df.to_csv().encode('utf-8')
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
st.download_button(
|
| 93 |
-
label="Export Team Model",
|
| 94 |
-
data=convert_df_to_csv(team_frame),
|
| 95 |
-
file_name='NBA_team_betting_export.csv',
|
| 96 |
-
mime='text/csv',
|
| 97 |
-
key='team_export',
|
| 98 |
-
)
|
| 99 |
-
|
| 100 |
-
with tab2:
|
| 101 |
-
st.info(t_stamp)
|
| 102 |
-
if st.button("Reset Data", key='reset2'):
|
| 103 |
-
st.cache_data.clear()
|
| 104 |
-
game_model, player_stats, prop_frame, timestamp = init_baselines()
|
| 105 |
-
t_stamp = f"Last Update: " + str(timestamp) + f" CST"
|
| 106 |
-
split_var1 = st.radio("Would you like to view all teams or specific ones?", ('All', 'Specific Teams'), key='split_var1')
|
| 107 |
-
if split_var1 == 'Specific Teams':
|
| 108 |
-
team_var1 = st.multiselect('Which teams would you like to include in the tables?', options = player_stats['Team'].unique(), key='team_var1')
|
| 109 |
-
elif split_var1 == 'All':
|
| 110 |
-
team_var1 = player_stats.Team.values.tolist()
|
| 111 |
-
player_stats = player_stats[player_stats['Team'].isin(team_var1)]
|
| 112 |
-
player_stats_disp = player_stats.set_index('Player')
|
| 113 |
-
player_stats_disp = player_stats_disp.sort_values(by='Fantasy', ascending=False)
|
| 114 |
-
st.dataframe(player_stats_disp.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
|
| 115 |
-
st.download_button(
|
| 116 |
-
label="Export Prop Model",
|
| 117 |
-
data=convert_df_to_csv(player_stats),
|
| 118 |
-
file_name='NBA_stats_export.csv',
|
| 119 |
-
mime='text/csv',
|
| 120 |
-
)
|
| 121 |
-
|
| 122 |
-
with tab3:
|
| 123 |
-
st.info(t_stamp)
|
| 124 |
-
if st.button("Reset Data", key='reset3'):
|
| 125 |
-
st.cache_data.clear()
|
| 126 |
-
game_model, player_stats, prop_frame, timestamp = init_baselines()
|
| 127 |
-
t_stamp = f"Last Update: " + str(timestamp) + f" CST"
|
| 128 |
-
col1, col2 = st.columns([1, 5])
|
| 129 |
-
|
| 130 |
-
with col2:
|
| 131 |
-
df_hold_container = st.empty()
|
| 132 |
-
info_hold_container = st.empty()
|
| 133 |
-
plot_hold_container = st.empty()
|
| 134 |
-
|
| 135 |
-
with col1:
|
| 136 |
-
player_check = st.selectbox('Select player to simulate props', options = player_stats['Player'].unique())
|
| 137 |
-
prop_type_var = st.selectbox('Select type of prop to simulate', options = ['points', 'threes', 'rebounds', 'assists', 'blocks', 'steals',
|
| 138 |
-
'PRA', 'points+rebounds', 'points+assists', 'rebounds+assists'])
|
| 139 |
-
|
| 140 |
-
ou_var = st.selectbox('Select wether it is an over or under', options = ['Over', 'Under'])
|
| 141 |
-
if prop_type_var == 'points':
|
| 142 |
-
prop_var = st.number_input('Type in the prop offered (i.e 5.5)', min_value = 0.0, max_value = 50.5, value = 15.5, step = .5)
|
| 143 |
-
elif prop_type_var == 'threes':
|
| 144 |
-
prop_var = st.number_input('Type in the prop offered (i.e 5.5)', min_value = 0.0, max_value = 5.5, value = 1.5, step = .5)
|
| 145 |
-
elif prop_type_var == 'rebounds':
|
| 146 |
-
prop_var = st.number_input('Type in the prop offered (i.e 5.5)', min_value = 0.0, max_value = 25.5, value = 5.5, step = .5)
|
| 147 |
-
elif prop_type_var == 'assists':
|
| 148 |
-
prop_var = st.number_input('Type in the prop offered (i.e 5.5)', min_value = 0.0, max_value = 25.5, value = 5.5, step = .5)
|
| 149 |
-
elif prop_type_var == 'blocks':
|
| 150 |
-
prop_var = st.number_input('Type in the prop offered (i.e 5.5)', min_value = 0.0, max_value = 5.5, value = 1.5, step = .5)
|
| 151 |
-
elif prop_type_var == 'steals':
|
| 152 |
-
prop_var = st.number_input('Type in the prop offered (i.e 5.5)', min_value = 0.0, max_value = 5.5, value = 1.5, step = .5)
|
| 153 |
-
elif prop_type_var == 'PRA':
|
| 154 |
-
prop_var = st.number_input('Type in the prop offered (i.e 5.5)', min_value = 0.0, max_value = 65.5, value = 20.5, step = .5)
|
| 155 |
-
elif prop_type_var == 'points+rebounds':
|
| 156 |
-
prop_var = st.number_input('Type in the prop offered (i.e 5.5)', min_value = 0.0, max_value = 45.5, value = 10.5, step = .5)
|
| 157 |
-
elif prop_type_var == 'points+assists':
|
| 158 |
-
prop_var = st.number_input('Type in the prop offered (i.e 5.5)', min_value = 0.0, max_value = 45.5, value = 10.5, step = .5)
|
| 159 |
-
elif prop_type_var == 'rebounds+assists':
|
| 160 |
-
prop_var = st.number_input('Type in the prop offered (i.e 5.5)', min_value = 0.0, max_value = 45.5, value = 10.5, step = .5)
|
| 161 |
-
line_var = st.number_input('Type in the line on the prop (i.e. -120)', min_value = -1500, max_value = 1500, value = -150, step = 1)
|
| 162 |
-
line_var = line_var + 1
|
| 163 |
-
|
| 164 |
-
if st.button('Simulate Prop'):
|
| 165 |
-
with col2:
|
| 166 |
-
|
| 167 |
-
with df_hold_container.container():
|
| 168 |
-
|
| 169 |
-
df = player_stats
|
| 170 |
-
|
| 171 |
-
total_sims = 5000
|
| 172 |
-
|
| 173 |
-
df.replace("", 0, inplace=True)
|
| 174 |
-
|
| 175 |
-
player_var = df.loc[df['Player'] == player_check]
|
| 176 |
-
player_var = player_var.reset_index()
|
| 177 |
-
|
| 178 |
-
if prop_type_var == 'points':
|
| 179 |
-
df['Median'] = df['Points']
|
| 180 |
-
elif prop_type_var == 'threes':
|
| 181 |
-
df['Median'] = df['3P']
|
| 182 |
-
elif prop_type_var == 'rebounds':
|
| 183 |
-
df['Median'] = df['Rebounds']
|
| 184 |
-
elif prop_type_var == 'assists':
|
| 185 |
-
df['Median'] = df['Assists']
|
| 186 |
-
elif prop_type_var == 'blocks':
|
| 187 |
-
df['Median'] = df['Blocks']
|
| 188 |
-
elif prop_type_var == 'steals':
|
| 189 |
-
df['Median'] = df['Steals']
|
| 190 |
-
elif prop_type_var == 'PRA':
|
| 191 |
-
df['Median'] = df['Points'] + df['Rebounds'] + df['Assists']
|
| 192 |
-
elif prop_type_var == 'points+rebounds':
|
| 193 |
-
df['Median'] = df['Points'] + df['Rebounds']
|
| 194 |
-
elif prop_type_var == 'points+assists':
|
| 195 |
-
df['Median'] = df['Points'] + df['Assists']
|
| 196 |
-
elif prop_type_var == 'rebounds+assists':
|
| 197 |
-
df['Median'] = df['Assists'] + df['Rebounds']
|
| 198 |
-
|
| 199 |
-
flex_file = df
|
| 200 |
-
flex_file['Floor'] = (flex_file['Median'] * .25) + (flex_file['Minutes'] * .25)
|
| 201 |
-
flex_file['Ceiling'] = flex_file['Median'] + 10 + (flex_file['Minutes'] * .25)
|
| 202 |
-
flex_file['STD'] = (flex_file['Median']/4)
|
| 203 |
-
flex_file = flex_file[['Player', 'Floor', 'Median', 'Ceiling', 'STD']]
|
| 204 |
-
|
| 205 |
-
hold_file = flex_file
|
| 206 |
-
overall_file = flex_file
|
| 207 |
-
salary_file = flex_file
|
| 208 |
-
|
| 209 |
-
overall_players = overall_file[['Player']]
|
| 210 |
-
|
| 211 |
-
for x in range(0,total_sims):
|
| 212 |
-
overall_file[x] = np.random.normal(overall_file['Median'],overall_file['STD'])
|
| 213 |
-
|
| 214 |
-
overall_file=overall_file.drop(['Player', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
|
| 215 |
-
overall_file.astype('int').dtypes
|
| 216 |
-
|
| 217 |
-
players_only = hold_file[['Player']]
|
| 218 |
-
|
| 219 |
-
player_outcomes = pd.merge(players_only, overall_file, left_index=True, right_index=True)
|
| 220 |
-
|
| 221 |
-
players_only['Mean_Outcome'] = overall_file.mean(axis=1)
|
| 222 |
-
players_only['10%'] = overall_file.quantile(0.1, axis=1)
|
| 223 |
-
players_only['90%'] = overall_file.quantile(0.9, axis=1)
|
| 224 |
-
if ou_var == 'Over':
|
| 225 |
-
players_only['beat_prop'] = overall_file[overall_file > prop_var].count(axis=1)/float(total_sims)
|
| 226 |
-
elif ou_var == 'Under':
|
| 227 |
-
players_only['beat_prop'] = (overall_file[overall_file < prop_var].count(axis=1)/float(total_sims))
|
| 228 |
-
|
| 229 |
-
players_only['implied_odds'] = np.where(line_var <= 0, (-(line_var)/((-(line_var))+100)), 100/(line_var+100))
|
| 230 |
-
|
| 231 |
-
players_only['Player'] = hold_file[['Player']]
|
| 232 |
-
|
| 233 |
-
final_outcomes = players_only[['Player', '10%', 'Mean_Outcome', '90%', 'implied_odds', 'beat_prop']]
|
| 234 |
-
final_outcomes['Bet?'] = np.where(final_outcomes['beat_prop'] - final_outcomes['implied_odds'] >= .10, "Bet", "No Bet")
|
| 235 |
-
final_outcomes = final_outcomes.loc[final_outcomes['Player'] == player_check]
|
| 236 |
-
player_outcomes = player_outcomes.loc[player_outcomes['Player'] == player_check]
|
| 237 |
-
player_outcomes = player_outcomes.drop(columns=['Player']).transpose()
|
| 238 |
-
player_outcomes = player_outcomes.reset_index()
|
| 239 |
-
player_outcomes.columns = ['Instance', 'Outcome']
|
| 240 |
-
|
| 241 |
-
x1 = player_outcomes.Outcome.to_numpy()
|
| 242 |
-
|
| 243 |
-
print(x1)
|
| 244 |
-
|
| 245 |
-
hist_data = [x1]
|
| 246 |
-
|
| 247 |
-
group_labels = ['player outcomes']
|
| 248 |
-
|
| 249 |
-
fig = px.histogram(
|
| 250 |
-
player_outcomes, x='Outcome')
|
| 251 |
-
fig.add_vline(x=prop_var, line_dash="dash", line_color="green")
|
| 252 |
-
|
| 253 |
-
with df_hold_container:
|
| 254 |
-
df_hold_container = st.empty()
|
| 255 |
-
format_dict = {'10%': '{:.2f}', 'Mean_Outcome': '{:.2f}','90%': '{:.2f}', 'beat_prop': '{:.2%}','implied_odds': '{:.2%}'}
|
| 256 |
-
st.dataframe(final_outcomes.style.format(format_dict), use_container_width = True)
|
| 257 |
-
|
| 258 |
-
with info_hold_container:
|
| 259 |
-
st.info('The Y-axis is the percent of times in simulations that the player reaches certain thresholds, while the X-axis is the threshold to be met. The Green dotted line is the prop you entered. You can hover over any spot and see the percent to reach that mark.')
|
| 260 |
-
|
| 261 |
-
with plot_hold_container:
|
| 262 |
-
st.dataframe(player_outcomes, use_container_width = True)
|
| 263 |
-
plot_hold_container = st.empty()
|
| 264 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 265 |
-
|
| 266 |
-
with tab4:
|
| 267 |
-
st.info(t_stamp)
|
| 268 |
-
st.info('The Over and Under percentages are a compositve 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.')
|
| 269 |
-
if st.button("Reset Data/Load Data", key='reset5'):
|
| 270 |
-
st.cache_data.clear()
|
| 271 |
-
game_model, player_stats, prop_frame, timestamp = init_baselines()
|
| 272 |
-
t_stamp = f"Last Update: " + str(timestamp) + f" CST"
|
| 273 |
-
col1, col2 = st.columns([1, 5])
|
| 274 |
-
|
| 275 |
-
with col2:
|
| 276 |
-
df_hold_container = st.empty()
|
| 277 |
-
info_hold_container = st.empty()
|
| 278 |
-
plot_hold_container = st.empty()
|
| 279 |
-
export_container = st.empty()
|
| 280 |
-
|
| 281 |
-
with col1:
|
| 282 |
-
prop_type_var = st.selectbox('Select prop category', options = ['points', 'rebounds', 'assists', 'PRA', 'points+rebounds', 'points+assists', 'rebounds+assists'])
|
| 283 |
-
|
| 284 |
-
if st.button('Simulate Prop Category'):
|
| 285 |
-
with col2:
|
| 286 |
-
|
| 287 |
-
with df_hold_container.container():
|
| 288 |
-
|
| 289 |
-
if prop_type_var == "points":
|
| 290 |
-
prop_df = prop_frame[['Player', 'over_prop', 'over_line', 'under_line', 'prop_type']]
|
| 291 |
-
prop_df = prop_df.loc[prop_df['prop_type'] == 'points']
|
| 292 |
-
prop_df = prop_df[['Player', 'over_prop', 'over_line', 'under_line']]
|
| 293 |
-
prop_df.rename(columns={"over_prop": "Prop"}, inplace = True)
|
| 294 |
-
prop_df = prop_df.loc[prop_df['Prop'] != 0]
|
| 295 |
-
st.table(prop_df)
|
| 296 |
-
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))
|
| 297 |
-
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))
|
| 298 |
-
df = pd.merge(player_stats, prop_df, how='left', left_on=['Player'], right_on = ['Player'])
|
| 299 |
-
elif prop_type_var == "rebounds":
|
| 300 |
-
prop_df = prop_frame[['Player', 'over_prop', 'over_line', 'under_line', 'prop_type']]
|
| 301 |
-
prop_df = prop_df.loc[prop_df['prop_type'] == 'rebounds']
|
| 302 |
-
prop_df = prop_df[['Player', 'over_prop', 'over_line', 'under_line']]
|
| 303 |
-
prop_df.rename(columns={"over_prop": "Prop"}, inplace = True)
|
| 304 |
-
prop_df = prop_df.loc[prop_df['Prop'] != 0]
|
| 305 |
-
st.table(prop_df)
|
| 306 |
-
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))
|
| 307 |
-
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))
|
| 308 |
-
df = pd.merge(player_stats, prop_df, how='left', left_on=['Player'], right_on = ['Player'])
|
| 309 |
-
elif prop_type_var == "assists":
|
| 310 |
-
prop_df = prop_frame[['Player', 'over_prop', 'over_line', 'under_line', 'prop_type']]
|
| 311 |
-
prop_df = prop_df.loc[prop_df['prop_type'] == 'assists']
|
| 312 |
-
prop_df = prop_df[['Player', 'over_prop', 'over_line', 'under_line']]
|
| 313 |
-
prop_df.rename(columns={"over_prop": "Prop"}, inplace = True)
|
| 314 |
-
prop_df = prop_df.loc[prop_df['Prop'] != 0]
|
| 315 |
-
st.table(prop_df)
|
| 316 |
-
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))
|
| 317 |
-
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))
|
| 318 |
-
df = pd.merge(player_stats, prop_df, how='left', left_on=['Player'], right_on = ['Player'])
|
| 319 |
-
elif prop_type_var == "PRA":
|
| 320 |
-
prop_df = prop_frame[['Player', 'over_prop', 'over_line', 'under_line', 'prop_type']]
|
| 321 |
-
prop_df = prop_df.loc[prop_df['prop_type'] == 'PRA']
|
| 322 |
-
prop_df = prop_df[['Player', 'over_prop', 'over_line', 'under_line']]
|
| 323 |
-
prop_df.rename(columns={"over_prop": "Prop"}, inplace = True)
|
| 324 |
-
prop_df = prop_df.loc[prop_df['Prop'] != 0]
|
| 325 |
-
st.table(prop_df)
|
| 326 |
-
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))
|
| 327 |
-
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))
|
| 328 |
-
df = pd.merge(player_stats, prop_df, how='left', left_on=['Player'], right_on = ['Player'])
|
| 329 |
-
elif prop_type_var == "points+rebounds":
|
| 330 |
-
prop_df = prop_frame[['Player', 'over_prop', 'over_line', 'under_line', 'prop_type']]
|
| 331 |
-
prop_df = prop_df.loc[prop_df['prop_type'] == 'points+rebounds']
|
| 332 |
-
prop_df = prop_df[['Player', 'over_prop', 'over_line', 'under_line']]
|
| 333 |
-
prop_df.rename(columns={"over_prop": "Prop"}, inplace = True)
|
| 334 |
-
prop_df = prop_df.loc[prop_df['Prop'] != 0]
|
| 335 |
-
st.table(prop_df)
|
| 336 |
-
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))
|
| 337 |
-
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))
|
| 338 |
-
df = pd.merge(player_stats, prop_df, how='left', left_on=['Player'], right_on = ['Player'])
|
| 339 |
-
elif prop_type_var == "points+assists":
|
| 340 |
-
prop_df = prop_frame[['Player', 'over_prop', 'over_line', 'under_line', 'prop_type']]
|
| 341 |
-
prop_df = prop_df.loc[prop_df['prop_type'] == 'points+assists']
|
| 342 |
-
prop_df = prop_df[['Player', 'over_prop', 'over_line', 'under_line']]
|
| 343 |
-
prop_df.rename(columns={"over_prop": "Prop"}, inplace = True)
|
| 344 |
-
prop_df = prop_df.loc[prop_df['Prop'] != 0]
|
| 345 |
-
st.table(prop_df)
|
| 346 |
-
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))
|
| 347 |
-
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))
|
| 348 |
-
df = pd.merge(player_stats, prop_df, how='left', left_on=['Player'], right_on = ['Player'])
|
| 349 |
-
elif prop_type_var == "rebounds+assists":
|
| 350 |
-
prop_df = prop_frame[['Player', 'over_prop', 'over_line', 'under_line', 'prop_type']]
|
| 351 |
-
prop_df = prop_df.loc[prop_df['prop_type'] == 'rebounds+assists']
|
| 352 |
-
prop_df = prop_df[['Player', 'over_prop', 'over_line', 'under_line']]
|
| 353 |
-
prop_df.rename(columns={"over_prop": "Prop"}, inplace = True)
|
| 354 |
-
prop_df = prop_df.loc[prop_df['Prop'] != 0]
|
| 355 |
-
st.table(prop_df)
|
| 356 |
-
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))
|
| 357 |
-
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))
|
| 358 |
-
df = pd.merge(player_stats, prop_df, how='left', left_on=['Player'], right_on = ['Player'])
|
| 359 |
-
|
| 360 |
-
prop_dict = dict(zip(df.Player, df.Prop))
|
| 361 |
-
over_dict = dict(zip(df.Player, df.Over))
|
| 362 |
-
under_dict = dict(zip(df.Player, df.Under))
|
| 363 |
-
|
| 364 |
-
total_sims = 5000
|
| 365 |
-
|
| 366 |
-
df.replace("", 0, inplace=True)
|
| 367 |
-
|
| 368 |
-
if prop_type_var == 'points':
|
| 369 |
-
df['Median'] = df['Points']
|
| 370 |
-
elif prop_type_var == 'rebounds':
|
| 371 |
-
df['Median'] = df['Rebounds']
|
| 372 |
-
elif prop_type_var == 'assists':
|
| 373 |
-
df['Median'] = df['Assists']
|
| 374 |
-
elif prop_type_var == 'PRA':
|
| 375 |
-
df['Median'] = df['Points'] + df['Rebounds'] + df['Assists']
|
| 376 |
-
elif prop_type_var == 'points+rebounds':
|
| 377 |
-
df['Median'] = df['Points'] + df['Rebounds']
|
| 378 |
-
elif prop_type_var == 'points+assists':
|
| 379 |
-
df['Median'] = df['Points'] + df['Assists']
|
| 380 |
-
elif prop_type_var == 'rebounds+assists':
|
| 381 |
-
df['Median'] = df['Assists'] + df['Rebounds']
|
| 382 |
-
|
| 383 |
-
flex_file = df
|
| 384 |
-
flex_file['Floor'] = (flex_file['Median'] * .25) + (flex_file['Minutes'] * .25)
|
| 385 |
-
flex_file['Ceiling'] = flex_file['Median'] + 10 + (flex_file['Minutes'] * .25)
|
| 386 |
-
flex_file['STD'] = (flex_file['Median']/4)
|
| 387 |
-
flex_file['Prop'] = flex_file['Player'].map(prop_dict)
|
| 388 |
-
flex_file = flex_file[['Player', 'Prop', 'Floor', 'Median', 'Ceiling', 'STD']]
|
| 389 |
-
|
| 390 |
-
hold_file = flex_file
|
| 391 |
-
overall_file = flex_file
|
| 392 |
-
prop_file = flex_file
|
| 393 |
-
|
| 394 |
-
overall_players = overall_file[['Player']]
|
| 395 |
-
|
| 396 |
-
for x in range(0,total_sims):
|
| 397 |
-
prop_file[x] = prop_file['Prop']
|
| 398 |
-
|
| 399 |
-
prop_file = prop_file.drop(['Player', 'Prop', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
|
| 400 |
-
|
| 401 |
-
for x in range(0,total_sims):
|
| 402 |
-
overall_file[x] = np.random.normal(overall_file['Median'],overall_file['STD'])
|
| 403 |
-
|
| 404 |
-
overall_file=overall_file.drop(['Player', 'Prop', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
|
| 405 |
-
|
| 406 |
-
players_only = hold_file[['Player']]
|
| 407 |
-
|
| 408 |
-
player_outcomes = pd.merge(players_only, overall_file, left_index=True, right_index=True)
|
| 409 |
-
|
| 410 |
-
prop_check = (overall_file - prop_file)
|
| 411 |
-
|
| 412 |
-
players_only['Mean_Outcome'] = overall_file.mean(axis=1)
|
| 413 |
-
players_only['10%'] = overall_file.quantile(0.1, axis=1)
|
| 414 |
-
players_only['90%'] = overall_file.quantile(0.9, axis=1)
|
| 415 |
-
players_only['Over'] = prop_check[prop_check > 0].count(axis=1)/float(total_sims)
|
| 416 |
-
players_only['Imp Over'] = players_only['Player'].map(over_dict)
|
| 417 |
-
players_only['Over%'] = players_only[["Over", "Imp Over"]].mean(axis=1)
|
| 418 |
-
players_only['Under'] = prop_check[prop_check < 0].count(axis=1)/float(total_sims)
|
| 419 |
-
players_only['Imp Under'] = players_only['Player'].map(under_dict)
|
| 420 |
-
players_only['Under%'] = players_only[["Under", "Imp Under"]].mean(axis=1)
|
| 421 |
-
players_only['Prop'] = players_only['Player'].map(prop_dict)
|
| 422 |
-
players_only['Prop_avg'] = players_only['Prop'].mean() / 100
|
| 423 |
-
players_only['prop_threshold'] = .10
|
| 424 |
-
players_only = players_only.loc[players_only['Mean_Outcome'] > 0]
|
| 425 |
-
players_only['Over_diff'] = players_only['Over%'] - players_only['Imp Over']
|
| 426 |
-
players_only['Under_diff'] = players_only['Under%'] - players_only['Imp Under']
|
| 427 |
-
players_only['Bet_check'] = np.where(players_only['Over_diff'] > players_only['Under_diff'], players_only['Over_diff'] , players_only['Under_diff'])
|
| 428 |
-
players_only['Bet_suggest'] = np.where(players_only['Over_diff'] > players_only['Under_diff'], "Over" , "Under")
|
| 429 |
-
players_only['Bet?'] = np.where(players_only['Bet_check'] >= players_only['prop_threshold'], players_only['Bet_suggest'], "No Bet")
|
| 430 |
-
players_only['Edge'] = players_only['Bet_check']
|
| 431 |
-
|
| 432 |
-
players_only['Player'] = hold_file[['Player']]
|
| 433 |
-
|
| 434 |
-
final_outcomes = players_only[['Player', 'Prop', 'Mean_Outcome', 'Imp Over', 'Over%', 'Imp Under', 'Under%', 'Bet?', 'Edge']]
|
| 435 |
-
|
| 436 |
-
final_outcomes = final_outcomes.sort_values(by='Edge', ascending=False)
|
| 437 |
-
|
| 438 |
-
final_outcomes = final_outcomes.set_index('Player')
|
| 439 |
-
|
| 440 |
-
with df_hold_container:
|
| 441 |
-
df_hold_container = st.empty()
|
| 442 |
-
st.dataframe(final_outcomes.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
|
| 443 |
-
with export_container:
|
| 444 |
-
export_container = st.empty()
|
| 445 |
-
st.download_button(
|
| 446 |
-
label="Export Projections",
|
| 447 |
-
data=convert_df_to_csv(final_outcomes),
|
| 448 |
-
file_name='Nba_prop_proj.csv',
|
| 449 |
-
mime='text/csv',
|
| 450 |
-
key='prop_proj',
|
| 451 |
-
)
|
| 452 |
-
|
|
|
|
| 42 |
@st.cache_resource(ttl = 300)
|
| 43 |
def init_baselines():
|
| 44 |
sh = gcservice_account.open_by_url(master_hold)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
worksheet = sh.worksheet('Arturo Props')
|
| 47 |
raw_display = pd.DataFrame(worksheet.get_all_records())
|
| 48 |
raw_display.replace('', np.nan, inplace=True)
|
| 49 |
+
raw_display = raw_display[['Player', 'Pos', 'Team', 'Opponent', 'Min', 'mpgL3', 'Diff', 'Status', 'Pts', 'Rbs', 'Asst', 'TOs', '3PM',
|
| 50 |
+
'Steals', 'Blk', 'FD', 'DK']]
|
| 51 |
+
player_stats = raw_display[raw_display['Min'] > 0]
|
| 52 |
|
| 53 |
+
return player_stats
|
| 54 |
|
| 55 |
def convert_df_to_csv(df):
|
| 56 |
return df.to_csv().encode('utf-8')
|
| 57 |
|
| 58 |
+
player_stats = init_baselines()
|
| 59 |
+
|
| 60 |
+
st.info(t_stamp)
|
| 61 |
+
if st.button("Reset Data", key='reset2'):
|
| 62 |
+
st.cache_data.clear()
|
| 63 |
+
player_stats = init_baselines()
|
| 64 |
+
split_var1 = st.radio("Would you like to view all teams or specific ones?", ('All', 'Specific Teams'), key='split_var1')
|
| 65 |
+
if split_var1 == 'Specific Teams':
|
| 66 |
+
team_var1 = st.multiselect('Which teams would you like to include in the tables?', options = player_stats['Team'].unique(), key='team_var1')
|
| 67 |
+
elif split_var1 == 'All':
|
| 68 |
+
team_var1 = player_stats.Team.values.tolist()
|
| 69 |
+
player_stats = player_stats[player_stats['Team'].isin(team_var1)]
|
| 70 |
+
player_stats_disp = player_stats.set_index('Player')
|
| 71 |
+
player_stats_disp = player_stats_disp.sort_values(by='Team', ascending=False)
|
| 72 |
+
st.dataframe(player_stats_disp.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
|
| 73 |
+
st.download_button(
|
| 74 |
+
label="Export Prop Model",
|
| 75 |
+
data=convert_df_to_csv(player_stats),
|
| 76 |
+
file_name='AmericanNumbers_stats_export.csv',
|
| 77 |
+
mime='text/csv',
|
| 78 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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