Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,452 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
st.set_page_config(layout="wide")
|
3 |
+
|
4 |
+
for name in dir():
|
5 |
+
if not name.startswith('_'):
|
6 |
+
del globals()[name]
|
7 |
+
|
8 |
+
import numpy as np
|
9 |
+
import pandas as pd
|
10 |
+
import streamlit as st
|
11 |
+
import gspread
|
12 |
+
import plotly.express as px
|
13 |
+
import random
|
14 |
+
import gc
|
15 |
+
|
16 |
+
@st.cache_resource
|
17 |
+
def init_conn():
|
18 |
+
scope = ['https://www.googleapis.com/auth/spreadsheets',
|
19 |
+
"https://www.googleapis.com/auth/drive"]
|
20 |
+
|
21 |
+
credentials = {
|
22 |
+
"type": "service_account",
|
23 |
+
"project_id": "model-sheets-connect",
|
24 |
+
"private_key_id": "0e0bc2fdef04e771172fe5807392b9d6639d945e",
|
25 |
+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDiu1v/e6KBKOcK\ncx0KQ23nZK3ZVvADYy8u/RUn/EDI82QKxTd/DizRLIV81JiNQxDJXSzgkbwKYEDm\n48E8zGvupU8+Nk76xNPakrQKy2Y8+VJlq5psBtGchJTuUSHcXU5Mg2JhQsB376PJ\nsCw552K6Pw8fpeMDJDZuxpKSkaJR6k9G5Dhf5q8HDXnC5Rh/PRFuKJ2GGRpX7n+2\nhT/sCax0J8jfdTy/MDGiDfJqfQrOPrMKELtsGHR9Iv6F4vKiDqXpKfqH+02E9ptz\nBk+MNcbZ3m90M8ShfRu28ebebsASfarNMzc3dk7tb3utHOGXKCf4tF8yYKo7x8BZ\noO9X4gSfAgMBAAECggEAU8ByyMpSKlTCF32TJhXnVJi/kS+IhC/Qn5JUDMuk4LXr\naAEWsWO6kV/ZRVXArjmuSzuUVrXumISapM9Ps5Ytbl95CJmGDiLDwRL815nvv6k3\nUyAS8EGKjz74RpoIoH6E7EWCAzxlnUgTn+5oP9Flije97epYk3H+e2f1f5e1Nn1d\nYNe8U+1HqJgILcxA1TAUsARBfoD7+K3z/8DVPHI8IpzAh6kTHqhqC23Rram4XoQ6\nzj/ZdVBjvnKuazETfsD+Vl3jGLQA8cKQVV70xdz3xwLcNeHsbPbpGBpZUoF73c65\nkAXOrjYl0JD5yAk+hmYhXr6H9c6z5AieuZGDrhmlFQKBgQDzV6LRXmjn4854DP/J\nI82oX2GcI4eioDZPRukhiQLzYerMQBmyqZIRC+/LTCAhYQSjNgMa+ZKyvLqv48M0\n/x398op/+n3xTs+8L49SPI48/iV+mnH7k0WI/ycd4OOKh8rrmhl/0EWb9iitwJYe\nMjTV/QxNEpPBEXfR1/mvrN/lVQKBgQDuhomOxUhWVRVH6x03slmyRBn0Oiw4MW+r\nrt1hlNgtVmTc5Mu+4G0USMZwYuOB7F8xG4Foc7rIlwS7Ic83jMJxemtqAelwOLdV\nXRLrLWJfX8+O1z/UE15l2q3SUEnQ4esPHbQnZowHLm0mdL14qSVMl1mu1XfsoZ3z\nJZTQb48CIwKBgEWbzQRtKD8lKDupJEYqSrseRbK/ax43DDITS77/DWwHl33D3FYC\nMblUm8ygwxQpR4VUfwDpYXBlklWcJovzamXpSnsfcYVkkQH47NuOXPXPkXQsw+w+\nDYcJzeu7F/vZqk9I7oBkWHUrrik9zPNoUzrfPvSRGtkAoTDSwibhoc5dAoGBAMHE\nK0T/ANeZQLNuzQps6S7G4eqjwz5W8qeeYxsdZkvWThOgDd/ewt3ijMnJm5X05hOn\ni4XF1euTuvUl7wbqYx76Wv3/1ZojiNNgy7ie4rYlyB/6vlBS97F4ZxJdxMlabbCW\n6b3EMWa4EVVXKoA1sCY7IVDE+yoQ1JYsZmq45YzPAoGBANWWHuVueFGZRDZlkNlK\nh5OmySmA0NdNug3G1upaTthyaTZ+CxGliwBqMHAwpkIRPwxUJpUwBTSEGztGTAxs\nWsUOVWlD2/1JaKSmHE8JbNg6sxLilcG6WEDzxjC5dLL1OrGOXj9WhC9KX3sq6qb6\nF/j9eUXfXjAlb042MphoF3ZC\n-----END PRIVATE KEY-----\n",
|
26 |
+
"client_email": "[email protected]",
|
27 |
+
"client_id": "100369174533302798535",
|
28 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
29 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
30 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
31 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gspread-connection%40model-sheets-connect.iam.gserviceaccount.com"
|
32 |
+
}
|
33 |
+
|
34 |
+
gc_con = gspread.service_account_from_dict(credentials)
|
35 |
+
|
36 |
+
return gc_con
|
37 |
+
|
38 |
+
gcservice_account = init_conn()
|
39 |
+
|
40 |
+
master_hold = 'https://docs.google.com/spreadsheets/d/1Yq0vGriWK-bS79e-bD6_u9pqrYE6Yrlbb_wEkmH-ot0/edit#gid=853878325'
|
41 |
+
|
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('Prop_Frame')
|
61 |
+
raw_display = pd.DataFrame(worksheet.get_all_records())
|
62 |
+
raw_display.replace('', np.nan, inplace=True)
|
63 |
+
prop_frame = raw_display.dropna()
|
64 |
+
|
65 |
+
return game_model, player_stats, prop_frame, timestamp
|
66 |
+
|
67 |
+
def convert_df_to_csv(df):
|
68 |
+
return df.to_csv().encode('utf-8')
|
69 |
+
|
70 |
+
game_model, player_stats, prop_frame, timestamp = init_baselines()
|
71 |
+
t_stamp = f"Last Update: " + str(timestamp) + f" CST"
|
72 |
+
|
73 |
+
tab1, tab2, tab3, tab4 = st.tabs(["Game Betting Model", "Player Projections", "Player Prop Simulations", "Stat Specific Simulations"])
|
74 |
+
|
75 |
+
with tab1:
|
76 |
+
st.info(t_stamp)
|
77 |
+
if st.button("Reset Data", key='reset1'):
|
78 |
+
st.cache_data.clear()
|
79 |
+
game_model, player_stats, prop_frame, timestamp = init_baselines()
|
80 |
+
t_stamp = f"Last Update: " + str(timestamp) + f" CST"
|
81 |
+
line_var1 = st.radio('How would you like to display odds?', options = ['Percentage', 'American'], key='line_var1')
|
82 |
+
team_frame = game_model
|
83 |
+
if line_var1 == 'Percentage':
|
84 |
+
team_frame = team_frame[['Team', 'Opp', 'Team Points', 'Opp Points', 'Proj Total', 'Proj Spread', 'Proj Winner', 'Win%']]
|
85 |
+
team_frame = team_frame.set_index('Team')
|
86 |
+
st.dataframe(team_frame.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
|
87 |
+
if line_var1 == 'American':
|
88 |
+
team_frame = team_frame[['Team', 'Opp', 'Team Points', 'Opp Points', 'Proj Total', 'Proj Spread', 'Proj Winner', 'Odds Line']]
|
89 |
+
team_frame = team_frame.set_index('Team')
|
90 |
+
st.dataframe(team_frame.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
|
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 |
+
|