Spaces:
Running
Running
| import streamlit as st | |
| st.set_page_config(layout="wide") | |
| for name in dir(): | |
| if not name.startswith('_'): | |
| del globals()[name] | |
| import numpy as np | |
| from numpy import where as np_where | |
| import pandas as pd | |
| import streamlit as st | |
| import plotly.express as px | |
| import scipy.stats as stats | |
| from datetime import datetime | |
| from database import db, prop_db | |
| game_format = {'Paydirt Win%': '{:.2%}', 'Vegas Win%': '{:.2%}'} | |
| prop_format = {'L5 Success': '{:.2%}', 'L10_Success': '{:.2%}', 'L20_success': '{:.2%}', 'Matchup Boost': '{:.2%}', 'Trending Over': '{:.2%}', 'Trending Under': '{:.2%}', | |
| 'Implied Over': '{:.2%}', 'Implied Under': '{:.2%}', 'Over Edge': '{:.2%}', 'Under Edge': '{:.2%}'} | |
| bet_format = {'Edge': '{:.2%}'} | |
| sim_format = {'Trending Over': '{:.2%}', 'Trending Under': '{:.2%}', 'Imp Over': '{:.2%}', 'Imp Under': '{:.2%}', 'Over%': '{:.2%}', 'Under%': '{:.2%}', 'Edge': '{:.2%}'} | |
| prop_table_options = ['NBA_GAME_PLAYER_POINTS', 'NBA_GAME_PLAYER_REBOUNDS', 'NBA_GAME_PLAYER_ASSISTS', 'NBA_GAME_PLAYER_POINTS_REBOUNDS_ASSISTS', 'NBA_GAME_PLAYER_POINTS_REBOUNDS', 'NBA_GAME_PLAYER_POINTS_ASSISTS', 'NBA_GAME_PLAYER_REBOUNDS_ASSISTS'] | |
| all_sim_vars = ['NBA_GAME_PLAYER_POINTS', 'NBA_GAME_PLAYER_REBOUNDS', 'NBA_GAME_PLAYER_ASSISTS', 'NBA_GAME_PLAYER_POINTS_REBOUNDS_ASSISTS', 'NBA_GAME_PLAYER_POINTS_REBOUNDS', 'NBA_GAME_PLAYER_POINTS_ASSISTS', 'NBA_GAME_PLAYER_REBOUNDS_ASSISTS'] | |
| pick6_sim_vars = ['Points', 'Rebounds', 'Assists', 'Points + Assists + Rebounds', 'Points + Assists', 'Points + Rebounds', 'Assists + Rebounds'] | |
| sim_all_hold = pd.DataFrame(columns=['Player', 'Team', 'Book', 'Prop Type', 'Prop', 'Mean_Outcome', 'Imp Over', 'Trending Over', 'Over%', 'Imp Under', 'Trending Under', 'Under%', 'Bet?', 'Edge']) | |
| st.markdown(""" | |
| <style> | |
| /* Tab styling */ | |
| .stElementContainer [data-baseweb="button-group"] { | |
| gap: 2.000rem; | |
| padding: 4px; | |
| } | |
| .stElementContainer [kind="segmented_control"] { | |
| height: 2.000rem; | |
| white-space: pre-wrap; | |
| background-color: #DAA520; | |
| color: white; | |
| border-radius: 20px; | |
| gap: 1px; | |
| padding: 10px 20px; | |
| font-weight: bold; | |
| transition: all 0.3s ease; | |
| } | |
| .stElementContainer [kind="segmented_controlActive"] { | |
| height: 3.000rem; | |
| background-color: #DAA520; | |
| border: 3px solid #FFD700; | |
| border-radius: 10px; | |
| color: black; | |
| } | |
| .stElementContainer [kind="segmented_control"]:hover { | |
| background-color: #FFD700; | |
| cursor: pointer; | |
| } | |
| div[data-baseweb="select"] > div { | |
| background-color: #DAA520; | |
| color: white; | |
| } | |
| </style>""", unsafe_allow_html=True) | |
| def calculate_poisson(row): | |
| mean_val = row['Mean_Outcome'] | |
| threshold = row['Prop'] | |
| cdf_value = stats.poisson.cdf(threshold, mean_val) | |
| probability = 1 - cdf_value | |
| return probability | |
| def add_column(df): | |
| return_df = df | |
| return_df['2P'] = return_df["Minutes"] * return_df["FG2M"] | |
| return_df['3P'] = return_df["Minutes"] * return_df["Threes"] | |
| return_df['FT'] = return_df["Minutes"] * return_df["FTM"] | |
| return_df['Points'] = (return_df["2P"] * 2) + (return_df["3P"] * 3) + return_df['FT'] | |
| return_df['Rebounds'] = return_df["Minutes"] * return_df["TRB"] | |
| return_df['Assists'] = return_df["Minutes"] * return_df["AST"] | |
| return_df['PRA'] = return_df['Points'] + return_df['Rebounds'] + return_df['Assists'] | |
| return_df['PR'] = return_df['Points'] + return_df['Rebounds'] | |
| return_df['PA'] = return_df['Points'] + return_df['Assists'] | |
| return_df['RA'] = return_df['Rebounds'] + return_df['Assists'] | |
| return_df['Steals'] = return_df["Minutes"] * return_df["STL"] | |
| return_df['Blocks'] = return_df["Minutes"] * return_df["BLK"] | |
| return_df['Turnovers'] = return_df["Minutes"] * return_df["TOV"] | |
| return_df['Fantasy'] = (return_df["2P"] * 3) + (return_df["3P"] * 3.5) + return_df['FT'] + (return_df["Rebounds"] * 1.25) + (return_df["Assists"] * 1.5) + (return_df["Steals"] * 2) + (return_df["Blocks"] * 2) + (return_df["Turnovers"] * -.5) | |
| export_df = return_df[['Player', 'Position', 'Team', 'Opp', 'Minutes', '2P', '3P', 'FT', 'Points', 'Rebounds', 'Assists', 'PRA', 'PR', 'PA', 'RA', 'Steals', 'Blocks', 'Turnovers', 'Fantasy']] | |
| return export_df | |
| def init_baselines(): | |
| collection = db["Game_Betting_Model"] | |
| cursor = collection.find() | |
| raw_display = pd.DataFrame(list(cursor)) | |
| raw_display = raw_display[['Team', 'Opp', 'PD Team Points', 'PD Opp Points', 'VEG Team Points', 'VEG Opp Points', 'PD Proj Total', 'VEG Proj Total', 'PD Over%', 'PD Over Odds', 'PD Under%', 'PD Under Odds', | |
| 'PD Proj Winner', 'PD Proj Spread', 'PD W Spread', 'VEG W Spread', 'PD Win%', 'PD Odds']] | |
| raw_display.replace('#DIV/0!', np.nan, inplace=True) | |
| game_model = raw_display.dropna() | |
| collection = db["Player_Stats"] | |
| cursor = collection.find() | |
| raw_display = pd.DataFrame(list(cursor)) | |
| raw_display.replace('', np.nan, inplace=True) | |
| raw_display = raw_display.rename(columns={"Name": "Player"}) | |
| raw_baselines = raw_display[['Player', 'Position', 'Team', 'Opp', 'Minutes', 'FGM', 'FGA', 'FG2M', 'FG2A', 'Threes', 'FG3A', 'FTM', 'FTA', 'TRB', 'AST', 'STL', 'BLK', 'TOV', 'PRA', 'PR', 'PA', 'RA']] | |
| raw_baselines = raw_baselines[raw_baselines['Minutes'] > 0] | |
| raw_baselines['Player'].replace(['Jaren Jackson', 'Nic Claxton', 'Jabari Smith', 'Lu Dort', 'Moe Wagner', 'Kyle Kuzma', 'Trey Murphy', 'Cameron Thomas'], | |
| ['Jaren Jackson Jr.', 'Nicolas Claxton', 'Jabari Smith Jr.', 'Luguentz Dort', 'Moritz Wagner', 'Kyle Kuzma Jr.', | |
| 'Trey Murphy III', 'Cam Thomas'], inplace=True) | |
| player_stats = raw_display[['Player', 'Position', 'Team', 'Opp', 'Minutes', '3P', 'Points', 'Rebounds', 'Assists', 'Steals', 'Blocks', 'Turnovers', 'Fantasy']] | |
| player_stats = player_stats[player_stats['Minutes'] > 0] | |
| player_stats['Player'].replace(['Jaren Jackson', 'Nic Claxton', 'Jabari Smith', 'Lu Dort', 'Moe Wagner', 'Kyle Kuzma', 'Trey Murphy', 'Cameron Thomas'], | |
| ['Jaren Jackson Jr.', 'Nicolas Claxton', 'Jabari Smith Jr.', 'Luguentz Dort', 'Moritz Wagner', 'Kyle Kuzma Jr.', | |
| 'Trey Murphy III', 'Cam Thomas'], inplace=True) | |
| timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
| collection = db["Prop_Trends"] | |
| cursor = collection.find() | |
| raw_display = pd.DataFrame(list(cursor)) | |
| raw_display.replace('', np.nan, inplace=True) | |
| raw_display = raw_display[['Name', 'over_prop', 'over_line', 'under_prop', 'under_line', 'OddsType', 'PropType', 'No Vig', 'Team', 'L5 Success', 'L10_Success', 'L20_success', 'L10 Avg', 'Projection', | |
| 'Proj Diff', 'Matchup Boost', 'Implied Over', 'Trending Over', 'Over Edge', 'Implied Under', 'Trending Under', 'Under Edge']] | |
| raw_display = raw_display.rename(columns={"Name": "Player", "OddsType": "book", "PropType": "prop_type"}) | |
| prop_frame = raw_display.dropna(subset='Player') | |
| collection = db["Pick6_Trends"] | |
| cursor = collection.find() | |
| raw_display = pd.DataFrame(list(cursor)) | |
| raw_display = raw_display[['Player', 'over_prop', 'over_line', 'under_prop', 'under_line', 'book', 'prop_type', 'No Vig', 'Team', 'L5 Success', 'L10_Success', 'L20_success', 'L10 Avg', 'Projection', | |
| 'Proj Diff', 'Matchup Boost', 'Implied Over', 'Trending Over', 'Over Edge', 'Implied Under', 'Trending Under', 'Under Edge']] | |
| pick_frame = raw_display.drop_duplicates(subset=['Player', 'prop_type'], keep='first') | |
| pick_frame = pick_frame.reset_index(drop=True) | |
| prop_frame['Player'].replace(['Jaren Jackson', 'Nic Claxton', 'Jabari Smith', 'Lu Dort', 'Moe Wagner', 'Kyle Kuzma', 'Trey Murphy', 'Cameron Thomas'], | |
| ['Jaren Jackson Jr.', 'Nicolas Claxton', 'Jabari Smith Jr.', 'Luguentz Dort', 'Moritz Wagner', 'Kyle Kuzma Jr.', | |
| 'Trey Murphy III', 'Cam Thomas'], inplace=True) | |
| pick_frame['Player'].replace(['Jaren Jackson', 'Nic Claxton', 'Jabari Smith', 'Lu Dort', 'Moe Wagner', 'Kyle Kuzma', 'Trey Murphy', 'Cameron Thomas'], | |
| ['Jaren Jackson Jr.', 'Nicolas Claxton', 'Jabari Smith Jr.', 'Luguentz Dort', 'Moritz Wagner', 'Kyle Kuzma Jr.', | |
| 'Trey Murphy III', 'Cam Thomas'], inplace=True) | |
| collection = db["Bet_Sheet"] | |
| cursor = collection.find() | |
| raw_display = pd.DataFrame(list(cursor)) | |
| raw_display.replace('', np.nan, inplace=True) | |
| raw_display = raw_display[['Name', 'PropType', 'No Vig', 'Projection', 'Edge']] | |
| raw_display = raw_display.rename(columns={"Name": "Player", "PropType": "prop_type"}) | |
| raw_display = raw_display.dropna(subset='Player') | |
| raw_display = raw_display.drop_duplicates(subset=['Player', 'prop_type'], keep='first') | |
| raw_display = raw_display.sort_values(by='Edge', ascending=False) | |
| raw_display = raw_display.reset_index(drop=True) | |
| raw_display['O/U'] = np.where(raw_display['No Vig'] > raw_display['Projection'], 'Under', 'Over') | |
| bet_sheet = raw_display[['Player', 'prop_type', 'O/U', 'No Vig', 'Projection', 'Edge']] | |
| bet_sheet = bet_sheet[bet_sheet['prop_type'] != 'NBA_GAME_PLAYER_STEALS'] | |
| bet_sheet = bet_sheet[bet_sheet['prop_type'] != 'NBA_GAME_PLAYER_BLOCKS'] | |
| collection = db["Pick6_Trends"] | |
| cursor = collection.find() | |
| raw_display = pd.DataFrame(list(cursor)) | |
| raw_display = raw_display[['Player', 'over_prop', 'over_line', 'under_prop', 'under_line', 'book', 'prop_type', 'No Vig', 'Team', 'L5 Success', 'L10_Success', 'L20_success', 'L10 Avg', 'Projection', | |
| 'Proj Diff', 'Matchup Boost', 'Implied Over', 'Trending Over', 'Over Edge', 'Implied Under', 'Trending Under', 'Under Edge']] | |
| pick_frame = raw_display.drop_duplicates(subset=['Player', 'prop_type'], keep='first') | |
| pick_frame = pick_frame.reset_index(drop=True) | |
| prop_frame['Player'].replace(['Jaren Jackson', 'Nic Claxton', 'Jabari Smith', 'Lu Dort', 'Moe Wagner', 'Kyle Kuzma', 'Trey Murphy', 'Cameron Thomas'], | |
| ['Jaren Jackson Jr.', 'Nicolas Claxton', 'Jabari Smith Jr.', 'Luguentz Dort', 'Moritz Wagner', 'Kyle Kuzma Jr.', | |
| 'Trey Murphy III', 'Cam Thomas'], inplace=True) | |
| pick_frame['Player'].replace(['Jaren Jackson', 'Nic Claxton', 'Jabari Smith', 'Lu Dort', 'Moe Wagner', 'Kyle Kuzma', 'Trey Murphy', 'Cameron Thomas'], | |
| ['Jaren Jackson Jr.', 'Nicolas Claxton', 'Jabari Smith Jr.', 'Luguentz Dort', 'Moritz Wagner', 'Kyle Kuzma Jr.', | |
| 'Trey Murphy III', 'Cam Thomas'], inplace=True) | |
| collection = prop_db["NBA_Props"] | |
| cursor = collection.find() | |
| raw_display = pd.DataFrame(list(cursor)) | |
| market_props = raw_display[['Name', 'Position', 'Projection', 'PropType', 'OddsType', 'over_pay', 'under_pay']] | |
| market_props['over_prop'] = market_props['Projection'] | |
| market_props['over_line'] = market_props['over_pay'].apply(lambda x: (x - 1) * 100 if x >= 2.0 else -100 / (x - 1)) | |
| market_props['under_prop'] = market_props['Projection'] | |
| market_props['under_line'] = market_props['under_pay'].apply(lambda x: (x - 1) * 100 if x >= 2.0 else -100 / (x - 1)) | |
| return game_model, raw_baselines, player_stats, prop_frame, bet_sheet, pick_frame, market_props, timestamp | |
| def calculate_no_vig(row): | |
| def implied_probability(american_odds): | |
| if american_odds < 0: | |
| return (-american_odds) / ((-american_odds) + 100) | |
| else: | |
| return 100 / (american_odds + 100) | |
| over_line = row['over_line'] | |
| under_line = row['under_line'] | |
| over_prop = row['over_prop'] | |
| over_prob = implied_probability(over_line) | |
| under_prob = implied_probability(under_line) | |
| total_prob = over_prob + under_prob | |
| no_vig_prob = (over_prob / total_prob + 0.5) * over_prop | |
| return no_vig_prob | |
| def convert_df_to_csv(df): | |
| return df.to_csv().encode('utf-8') | |
| game_model, raw_baselines, player_stats, prop_frame, bet_sheet, pick_frame, market_props, timestamp = init_baselines() | |
| t_stamp = f"Last Update: " + str(timestamp) + f" CST" | |
| selected_tab = st.segmented_control( | |
| "Select Tab", | |
| options=["Bet Sheet", "Game Betting Model", 'Prop Market', "Player Projections", "Prop Trend Table", "Player Prop Simulations", "Stat Specific Simulations"], | |
| selection_mode='single', | |
| default='Bet Sheet', | |
| width='stretch', | |
| label_visibility='collapsed', | |
| key='tab_selector' | |
| ) | |
| if selected_tab == 'Bet Sheet': | |
| with st.expander("Info and Filters"): | |
| st.info(t_stamp) | |
| if st.button("Reset Data", key='reset1'): | |
| st.cache_data.clear() | |
| game_model, raw_baselines, player_stats, prop_frame, bet_sheet, pick_frame, market_props, timestamp = init_baselines() | |
| t_stamp = f"Last Update: " + str(timestamp) + f" CST" | |
| prop_selection = st.multiselect('Select prop type to view', default = bet_sheet['prop_type'].unique(), options = bet_sheet['prop_type'].unique(), key='prop_selection') | |
| bet_sheet = bet_sheet[bet_sheet['prop_type'].isin(prop_selection)] | |
| st.dataframe(bet_sheet.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(bet_format, precision=2), use_container_width = True) | |
| st.download_button( | |
| label="Export Bet Sheet", | |
| data=convert_df_to_csv(bet_sheet), | |
| file_name='NBA_bet_sheet_export.csv', | |
| mime='text/csv', | |
| key='bet_sheet_export', | |
| ) | |
| if selected_tab == 'Game Betting Model': | |
| with st.expander("Info and Filters"): | |
| st.info(t_stamp) | |
| if st.button("Reset Data", key='reset1'): | |
| st.cache_data.clear() | |
| game_model, raw_baselines, player_stats, prop_frame, bet_sheet, pick_frame, market_props, timestamp = init_baselines() | |
| t_stamp = f"Last Update: " + str(timestamp) + f" CST" | |
| line_var1 = st.radio('How would you like to display odds?', options = ['Percentage', 'American'], key='line_var1') | |
| team_frame = game_model | |
| if line_var1 == 'Percentage': | |
| team_frame = team_frame[['Team', 'Opp', 'PD Team Points', 'PD Opp Points', 'VEG Team Points', 'VEG Opp Points', 'PD Proj Total', 'VEG Proj Total', 'PD Over%', 'PD Under%', 'PD Proj Winner', 'PD Proj Spread', 'PD W Spread', 'VEG W Spread', 'PD Win%']] | |
| team_frame = team_frame.set_index('Team') | |
| st.dataframe(team_frame.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(game_format, precision=2), use_container_width = True) | |
| if line_var1 == 'American': | |
| team_frame = team_frame[['Team', 'Opp', 'PD Team Points', 'PD Opp Points', 'VEG Team Points', 'VEG Opp Points', 'PD Proj Total', 'VEG Proj Total', 'PD Over Odds', 'PD Under Odds', 'PD Proj Winner', 'PD Proj Spread', 'PD W Spread', 'VEG W Spread', 'PD Odds']] | |
| team_frame = team_frame.set_index('Team') | |
| st.dataframe(team_frame.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(game_format, precision=2), use_container_width = True) | |
| st.download_button( | |
| label="Export Team Model", | |
| data=convert_df_to_csv(team_frame), | |
| file_name='NBA_team_betting_export.csv', | |
| mime='text/csv', | |
| key='team_export', | |
| ) | |
| if selected_tab == 'Prop Market': | |
| with st.expander("Info and Filters"): | |
| st.info(t_stamp) | |
| if st.button("Reset Data", key='reset2'): | |
| st.cache_data.clear() | |
| game_model, raw_baselines, player_stats, prop_frame, bet_sheet, pick_frame, market_props, timestamp = init_baselines() | |
| t_stamp = f"Last Update: " + str(timestamp) + f" CST" | |
| market_type = st.selectbox('Select type of prop are you wanting to view', options = prop_table_options, key = 'market_type_key') | |
| disp_market = market_props.copy() | |
| disp_market = disp_market[disp_market['PropType'] == market_type] | |
| disp_market['No_Vig_Prop'] = disp_market.apply(calculate_no_vig, axis=1) | |
| fanduel_frame = disp_market[disp_market['OddsType'] == 'FANDUEL'] | |
| fanduel_dict = dict(zip(fanduel_frame['Name'], fanduel_frame['No_Vig_Prop'])) | |
| draftkings_frame = disp_market[disp_market['OddsType'] == 'DRAFTKINGS'] | |
| draftkings_dict = dict(zip(draftkings_frame['Name'], draftkings_frame['No_Vig_Prop'])) | |
| mgm_frame = disp_market[disp_market['OddsType'] == 'MGM'] | |
| mgm_dict = dict(zip(mgm_frame['Name'], mgm_frame['No_Vig_Prop'])) | |
| bet365_frame = disp_market[disp_market['OddsType'] == 'BET_365'] | |
| bet365_dict = dict(zip(bet365_frame['Name'], bet365_frame['No_Vig_Prop'])) | |
| disp_market['FANDUEL'] = disp_market['Name'].map(fanduel_dict) | |
| disp_market['DRAFTKINGS'] = disp_market['Name'].map(draftkings_dict) | |
| disp_market['MGM'] = disp_market['Name'].map(mgm_dict) | |
| disp_market['BET365'] = disp_market['Name'].map(bet365_dict) | |
| disp_market = disp_market[['Name', 'Position','FANDUEL', 'DRAFTKINGS', 'MGM', 'BET365']] | |
| disp_market = disp_market.drop_duplicates(subset=['Name'], keep='first', ignore_index=True) | |
| st.dataframe(disp_market.style.background_gradient(axis=1, subset=['FANDUEL', 'DRAFTKINGS', 'MGM', 'BET365'], cmap='RdYlGn').format(prop_format, precision=2), height = 1000, use_container_width = True) | |
| st.download_button( | |
| label="Export Market Props", | |
| data=convert_df_to_csv(disp_market), | |
| file_name='NFL_market_props_export.csv', | |
| mime='text/csv', | |
| ) | |
| if selected_tab == 'Player Projections': | |
| with st.expander("Info and Filters"): | |
| st.info(t_stamp) | |
| if st.button("Reset Data", key='reset3'): | |
| st.cache_data.clear() | |
| game_model, raw_baselines, player_stats, prop_frame, bet_sheet, pick_frame, market_props, timestamp = init_baselines() | |
| t_stamp = f"Last Update: " + str(timestamp) + f" CST" | |
| split_var1 = st.radio("Would you like to view all teams or specific ones?", ('All', 'Specific Teams'), key='split_var1') | |
| if split_var1 == 'Specific Teams': | |
| team_var1 = st.multiselect('Which teams would you like to include in the tables?', options = player_stats['Team'].unique(), key='team_var1') | |
| elif split_var1 == 'All': | |
| team_var1 = player_stats.Team.values.tolist() | |
| player_stats = player_stats[player_stats['Team'].isin(team_var1)] | |
| player_stats_disp = player_stats.set_index('Player') | |
| player_stats_disp = player_stats_disp.sort_values(by='Fantasy', ascending=False) | |
| st.dataframe(player_stats_disp.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True) | |
| st.download_button( | |
| label="Export Prop Model", | |
| data=convert_df_to_csv(player_stats), | |
| file_name='NBA_stats_export.csv', | |
| mime='text/csv', | |
| ) | |
| if selected_tab == 'Prop Trend Table': | |
| with st.expander("Info and Filters"): | |
| st.info(t_stamp) | |
| if st.button("Reset Data", key='reset4'): | |
| st.cache_data.clear() | |
| game_model, raw_baselines, player_stats, prop_frame, bet_sheet, pick_frame, market_props, timestamp = init_baselines() | |
| t_stamp = f"Last Update: " + str(timestamp) + f" CST" | |
| split_var5 = st.radio("Would you like to view all teams or specific ones?", ('All', 'Specific Teams'), key='split_var5') | |
| if split_var5 == 'Specific Teams': | |
| team_var5 = st.multiselect('Which teams would you like to include in the tables?', options = player_stats['Team'].unique(), key='team_var5') | |
| elif split_var5 == 'All': | |
| team_var5 = player_stats.Team.values.tolist() | |
| book_split5 = st.radio("Would you like to view all books or specific ones?", ('All', 'Specific Books'), key='book_split5') | |
| if book_split5 == 'Specific Books': | |
| book_var5 = st.multiselect('Which books would you like to include in the tables?', options = ['BET_365', 'DRAFTKINGS', 'CONSENSUS', 'FANDUEL', 'MGM', 'UNIBET', 'WILLIAM_HILL'], key='book_var5') | |
| elif book_split5 == 'All': | |
| book_var5 = ['BET_365', 'DRAFTKINGS', 'CONSENSUS', 'FANDUEL', 'MGM', 'UNIBET', 'WILLIAM_HILL'] | |
| prop_type_var2 = st.selectbox('Select type of prop are you wanting to view', options = prop_table_options) | |
| prop_frame_disp = prop_frame[prop_frame['Team'].isin(team_var5)] | |
| prop_frame_disp = prop_frame_disp[prop_frame_disp['book'].isin(book_var5)] | |
| prop_frame_disp = prop_frame_disp[prop_frame_disp['prop_type'] == prop_type_var2] | |
| prop_frame_disp = prop_frame_disp.sort_values(by='Trending Over', ascending=False) | |
| st.dataframe(prop_frame_disp.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(prop_format, precision=2), use_container_width = True) | |
| st.download_button( | |
| label="Export Prop Trends Model", | |
| data=convert_df_to_csv(prop_frame), | |
| file_name='NBA_prop_trends_export.csv', | |
| mime='text/csv', | |
| ) | |
| if selected_tab == 'Player Prop Simulations': | |
| st.info(t_stamp) | |
| if st.button("Reset Data", key='reset5'): | |
| st.cache_data.clear() | |
| game_model, raw_baselines, player_stats, prop_frame, bet_sheet, pick_frame, market_props, timestamp = init_baselines() | |
| t_stamp = f"Last Update: " + str(timestamp) + f" CST" | |
| col1, col2 = st.columns([1, 5]) | |
| with col2: | |
| df_hold_container = st.empty() | |
| info_hold_container = st.empty() | |
| plot_hold_container = st.empty() | |
| with col1: | |
| player_check = st.selectbox('Select player to simulate props', options = player_stats['Player'].unique()) | |
| prop_type_var = st.selectbox('Select type of prop to simulate', options = ['points', 'threes', 'rebounds', 'assists', 'blocks', 'steals', | |
| 'PRA', 'points+rebounds', 'points+assists', 'rebounds+assists']) | |
| ou_var = st.selectbox('Select wether it is an over or under', options = ['Over', 'Under']) | |
| if prop_type_var == 'points': | |
| 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) | |
| elif prop_type_var == 'threes': | |
| 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) | |
| elif prop_type_var == 'rebounds': | |
| 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) | |
| elif prop_type_var == 'assists': | |
| 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) | |
| elif prop_type_var == 'blocks': | |
| 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) | |
| elif prop_type_var == 'steals': | |
| 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) | |
| elif prop_type_var == 'PRA': | |
| 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) | |
| elif prop_type_var == 'points+rebounds': | |
| 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) | |
| elif prop_type_var == 'points+assists': | |
| 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) | |
| elif prop_type_var == 'rebounds+assists': | |
| 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) | |
| 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) | |
| line_var = line_var + 1 | |
| if st.button('Simulate Prop'): | |
| with col2: | |
| with df_hold_container.container(): | |
| df = player_stats | |
| st.write("sim started") | |
| total_sims = 1000 | |
| df.replace("", 0, inplace=True) | |
| player_var = df[df['Player'] == player_check] | |
| player_var = player_var.reset_index() | |
| if prop_type_var == 'points': | |
| df['Median'] = pd.to_numeric(df['Points'], errors='coerce') | |
| elif prop_type_var == 'threes': | |
| df['Median'] = pd.to_numeric(df['3P'], errors='coerce') | |
| elif prop_type_var == 'rebounds': | |
| df['Median'] = pd.to_numeric(df['Rebounds'], errors='coerce') | |
| elif prop_type_var == 'assists': | |
| df['Median'] = pd.to_numeric(df['Assists'], errors='coerce') | |
| elif prop_type_var == 'blocks': | |
| df['Median'] = pd.to_numeric(df['Blocks'], errors='coerce') | |
| elif prop_type_var == 'steals': | |
| df['Median'] = pd.to_numeric(df['Steals'], errors='coerce') | |
| elif prop_type_var == 'PRA': | |
| df['Median'] = pd.to_numeric(df['Points'], errors='coerce') + pd.to_numeric(df['Rebounds'], errors='coerce') + pd.to_numeric(df['Assists'], errors='coerce') | |
| elif prop_type_var == 'points+rebounds': | |
| df['Median'] = pd.to_numeric(df['Points'], errors='coerce') + pd.to_numeric(df['Rebounds'], errors='coerce') | |
| elif prop_type_var == 'points+assists': | |
| df['Median'] = pd.to_numeric(df['Points'], errors='coerce') + pd.to_numeric(df['Assists'], errors='coerce') | |
| elif prop_type_var == 'rebounds+assists': | |
| df['Median'] = pd.to_numeric(df['Assists'], errors='coerce') + pd.to_numeric(df['Rebounds'], errors='coerce') | |
| flex_file = df | |
| flex_file['Floor'] = (flex_file['Median'] * .25) + (flex_file['Minutes'] * .25) | |
| flex_file['Ceiling'] = flex_file['Median'] + 10 + (flex_file['Minutes'] * .25) | |
| flex_file['STD'] = (flex_file['Median']/4) | |
| flex_file = flex_file[['Player', 'Floor', 'Median', 'Ceiling', 'STD']] | |
| hold_file = flex_file | |
| overall_file = flex_file | |
| salary_file = flex_file | |
| overall_players = overall_file[['Player']] | |
| for x in range(0,total_sims): | |
| overall_file[x] = np.random.normal(overall_file['Median'],overall_file['STD']) | |
| overall_file=overall_file.drop(['Player', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1) | |
| players_only = hold_file[['Player']] | |
| player_outcomes = pd.merge(players_only, overall_file, left_index=True, right_index=True) | |
| st.write("sim finished, calculating outcomes") | |
| players_only['Mean_Outcome'] = overall_file.mean(axis=1) | |
| players_only['Prop'] = prop_var | |
| players_only['poisson_var'] = players_only.apply(calculate_poisson, axis=1) | |
| players_only['10%'] = overall_file.quantile(0.1, axis=1) | |
| players_only['90%'] = overall_file.quantile(0.9, axis=1) | |
| if ou_var == 'Over': | |
| players_only['beat_prop'] = np.where(players_only['Prop'] <= 3, players_only['poisson_var'], overall_file[overall_file > prop_var].count(axis=1)/float(total_sims)) | |
| elif ou_var == 'Under': | |
| players_only['beat_prop'] = np.where(players_only['Prop'] <= 3, 1 - players_only['poisson_var'], (overall_file[overall_file < prop_var].count(axis=1)/float(total_sims))) | |
| players_only['implied_odds'] = np.where(line_var <= 0, (-(line_var)/((-(line_var))+100)), 100/(line_var+100)) | |
| players_only['Player'] = hold_file[['Player']] | |
| final_outcomes = players_only[['Player', '10%', 'Mean_Outcome', '90%', 'implied_odds', 'beat_prop']] | |
| final_outcomes['Bet?'] = np.where(final_outcomes['beat_prop'] - final_outcomes['implied_odds'] >= .10, "Bet", "No Bet") | |
| final_outcomes = final_outcomes[final_outcomes['Player'] == player_check] | |
| player_outcomes = player_outcomes[player_outcomes['Player'] == player_check] | |
| player_outcomes = player_outcomes.drop(columns=['Player']).transpose() | |
| player_outcomes = player_outcomes.reset_index() | |
| player_outcomes.columns = ['Instance', 'Outcome'] | |
| x1 = player_outcomes.Outcome.to_numpy() | |
| print(x1) | |
| hist_data = [x1] | |
| group_labels = ['player outcomes'] | |
| fig = px.histogram( | |
| player_outcomes, x='Outcome') | |
| fig.add_vline(x=prop_var, line_dash="dash", line_color="green") | |
| with df_hold_container: | |
| df_hold_container = st.empty() | |
| format_dict = {'10%': '{:.2f}', 'Mean_Outcome': '{:.2f}','90%': '{:.2f}', 'beat_prop': '{:.2%}','implied_odds': '{:.2%}'} | |
| st.dataframe(final_outcomes.style.format(format_dict), use_container_width = True) | |
| with info_hold_container: | |
| 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.') | |
| with plot_hold_container: | |
| st.dataframe(player_outcomes, use_container_width = True) | |
| plot_hold_container = st.empty() | |
| st.plotly_chart(fig, use_container_width=True) | |
| if selected_tab == 'Stat Specific Simulations': | |
| st.info(t_stamp) | |
| 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.') | |
| if st.button("Reset Data/Load Data", key='reset6'): | |
| st.cache_data.clear() | |
| game_model, raw_baselines, player_stats, prop_frame, pick_frame, market_props, timestamp = init_baselines() | |
| t_stamp = f"Last Update: " + str(timestamp) + f" CST" | |
| settings_container = st.empty() | |
| df_hold_container = st.empty() | |
| export_container = st.empty() | |
| with settings_container.container(): | |
| col1, col2, col3, col4 = st.columns([3, 3, 3, 3]) | |
| with col1: | |
| game_select_var = st.selectbox('Select prop source', options = ['Aggregate', 'Pick6']) | |
| with col2: | |
| book_select_var = st.selectbox('Select book', options = ['ALL', 'BET_365', 'DRAFTKINGS', 'FANDUEL', 'MGM', 'UNIBET', 'WILLIAM_HILL']) | |
| if book_select_var == 'ALL': | |
| book_selections = ['BET_365', 'DRAFTKINGS', 'FANDUEL', 'MGM', 'UNIBET', 'WILLIAM_HILL'] | |
| else: | |
| book_selections = [book_select_var] | |
| if game_select_var == 'Aggregate': | |
| prop_df = prop_frame[['Player', 'book', 'over_prop', 'over_line', 'under_line', 'prop_type', 'Trending Over', 'Trending Under']] | |
| elif game_select_var == 'Pick6': | |
| prop_df = pick_frame[['Player', 'book', 'over_prop', 'over_line', 'under_line', 'prop_type', 'Trending Over', 'Trending Under']] | |
| book_selections = ['Pick6'] | |
| with col3: | |
| if game_select_var == 'Aggregate': | |
| prop_type_var = st.selectbox('Select prop category', options = ['All Props', 'NBA_GAME_PLAYER_POINTS', 'NBA_GAME_PLAYER_REBOUNDS', 'NBA_GAME_PLAYER_ASSISTS', 'NBA_GAME_PLAYER_POINTS_REBOUNDS_ASSISTS', | |
| 'NBA_GAME_PLAYER_POINTS_REBOUNDS', 'NBA_GAME_PLAYER_POINTS_ASSISTS', 'NBA_GAME_PLAYER_REBOUNDS_ASSISTS', 'NBA_GAME_PLAYER_3_POINTERS_MADE']) | |
| elif game_select_var == 'Pick6': | |
| prop_type_var = st.selectbox('Select prop category', options = ['All Props', 'Points', 'Rebounds', 'Assists', 'Points + Assists + Rebounds', 'Points + Assists', 'Points + Rebounds', 'Assists + Rebounds', '3-Pointers Made']) | |
| with col4: | |
| st.download_button( | |
| label="Download Prop Source", | |
| data=convert_df_to_csv(prop_df), | |
| file_name='Nba_prop_source.csv', | |
| mime='text/csv', | |
| key='prop_source', | |
| ) | |
| if st.button('Simulate Prop Category'): | |
| with df_hold_container.container(): | |
| if prop_type_var == 'All Props': | |
| if game_select_var == 'Aggregate': | |
| prop_df_raw = prop_frame[['Player', 'book', 'over_prop', 'over_line', 'under_line', 'prop_type', 'Trending Over', 'Trending Under']] | |
| sim_vars = ['NBA_GAME_PLAYER_POINTS', 'NBA_GAME_PLAYER_REBOUNDS', 'NBA_GAME_PLAYER_ASSISTS', 'NBA_GAME_PLAYER_POINTS_REBOUNDS_ASSISTS', 'NBA_GAME_PLAYER_POINTS_REBOUNDS', | |
| 'NBA_GAME_PLAYER_POINTS_ASSISTS', 'NBA_GAME_PLAYER_REBOUNDS_ASSISTS', 'NBA_GAME_PLAYER_3_POINTERS_MADE'] | |
| elif game_select_var == 'Pick6': | |
| prop_df_raw = pick_frame[['Player', 'book', 'over_prop', 'over_line', 'under_line', 'prop_type', 'Trending Over', 'Trending Under']] | |
| sim_vars = ['Points', 'Rebounds', 'Assists', 'Points + Assists + Rebounds', 'Points + Assists', 'Points + Rebounds', 'Assists + Rebounds', '3-Pointers Made'] | |
| player_df = player_stats.copy() | |
| for prop in sim_vars: | |
| for books in book_selections: | |
| prop_df = prop_df_raw[prop_df_raw['prop_type'] == prop] | |
| prop_df = prop_df[prop_df['book'] == books] | |
| prop_df = prop_df[['Player', 'book', 'over_prop', 'over_line', 'under_line', 'prop_type', 'Trending Over', 'Trending Under']] | |
| prop_df.rename(columns={"over_prop": "Prop"}, inplace = True) | |
| prop_df['Over'] = 1 / prop_df['over_line'] | |
| prop_df['Under'] = 1 / prop_df['under_line'] | |
| prop_dict = dict(zip(prop_df.Player, prop_df.Prop)) | |
| prop_type_dict = dict(zip(prop_df.Player, prop_df.prop_type)) | |
| book_dict = dict(zip(prop_df.Player, prop_df.book)) | |
| over_dict = dict(zip(prop_df.Player, prop_df.Over)) | |
| under_dict = dict(zip(prop_df.Player, prop_df.Under)) | |
| trending_over_dict = dict(zip(prop_df.Player, prop_df['Trending Over'])) | |
| trending_under_dict = dict(zip(prop_df.Player, prop_df['Trending Under'])) | |
| player_df['book'] = player_df['Player'].map(book_dict) | |
| player_df['Prop'] = player_df['Player'].map(prop_dict) | |
| player_df['prop_type'] = player_df['Player'].map(prop_type_dict) | |
| player_df['Trending Over'] = player_df['Player'].map(trending_over_dict) | |
| player_df['Trending Under'] = player_df['Player'].map(trending_under_dict) | |
| df = player_df.reset_index(drop=True) | |
| team_dict = dict(zip(df.Player, df.Team)) | |
| total_sims = 1000 | |
| df.replace("", 0, inplace=True) | |
| if prop == "NBA_GAME_PLAYER_POINTS" or prop == "Points": | |
| df['Median'] = pd.to_numeric(df['Points'], errors='coerce') | |
| elif prop == "NBA_GAME_PLAYER_REBOUNDS" or prop == "Rebounds": | |
| df['Median'] = pd.to_numeric(df['Rebounds'], errors='coerce') | |
| elif prop == "NBA_GAME_PLAYER_ASSISTS" or prop == "Assists": | |
| df['Median'] = pd.to_numeric(df['Assists'], errors='coerce') | |
| elif prop == "NBA_GAME_PLAYER_3_POINTERS_MADE" or prop == "3-Pointers Made": | |
| df['Median'] = pd.to_numeric(df['3P'], errors='coerce') | |
| elif prop == "NBA_GAME_PLAYER_POINTS_REBOUNDS_ASSISTS" or prop == "Points + Assists + Rebounds": | |
| df['Median'] = pd.to_numeric(df['Points'], errors='coerce') + pd.to_numeric(df['Rebounds'], errors='coerce') + pd.to_numeric(df['Assists'], errors='coerce') | |
| elif prop == "NBA_GAME_PLAYER_POINTS_REBOUNDS" or prop == "Points + Rebounds": | |
| df['Median'] = pd.to_numeric(df['Points'], errors='coerce') + pd.to_numeric(df['Rebounds'], errors='coerce') | |
| elif prop == "NBA_GAME_PLAYER_POINTS_ASSISTS" or prop == "Points + Assists": | |
| df['Median'] = pd.to_numeric(df['Points'], errors='coerce') + pd.to_numeric(df['Assists'], errors='coerce') | |
| elif prop == "NBA_GAME_PLAYER_REBOUNDS_ASSISTS" or prop == "Assists + Rebounds": | |
| df['Median'] = pd.to_numeric(df['Rebounds'], errors='coerce') + pd.to_numeric(df['Assists'], errors='coerce') | |
| flex_file = df.copy() | |
| flex_file['Floor'] = flex_file['Median'] * .25 | |
| flex_file['Ceiling'] = flex_file['Median'] + (flex_file['Median'] * 1.75) | |
| flex_file['STD'] = flex_file['Median'] / 4 | |
| flex_file['Prop'] = flex_file['Player'].map(prop_dict) | |
| flex_file = flex_file[['Player', 'book', 'Prop', 'Floor', 'Median', 'Ceiling', 'STD']] | |
| hold_file = flex_file.copy() | |
| overall_file = flex_file.copy() | |
| prop_file = flex_file.copy() | |
| overall_players = overall_file[['Player']] | |
| for x in range(0,total_sims): | |
| prop_file[x] = prop_file['Prop'] | |
| prop_file = prop_file.drop(['Player', 'book', 'Prop', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1) | |
| for x in range(0,total_sims): | |
| overall_file[x] = np.random.normal(overall_file['Median'],overall_file['STD']) | |
| overall_file=overall_file.drop(['Player', 'book', 'Prop', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1) | |
| players_only = hold_file[['Player']] | |
| player_outcomes = pd.merge(players_only, overall_file, left_index=True, right_index=True) | |
| prop_check = (overall_file - prop_file) | |
| players_only['Mean_Outcome'] = overall_file.mean(axis=1) | |
| players_only['Book'] = players_only['Player'].map(book_dict) | |
| players_only['Prop'] = players_only['Player'].map(prop_dict) | |
| players_only['Trending Over'] = players_only['Player'].map(trending_over_dict) | |
| players_only['Trending Under'] = players_only['Player'].map(trending_under_dict) | |
| players_only['over_adj'] = np_where((players_only['Mean_Outcome'] - players_only['Prop']) > 0, 1, (players_only['Mean_Outcome'] / players_only['Prop'])) | |
| players_only['under_adj'] = np_where((players_only['Prop'] - players_only['Mean_Outcome']) > 0, 1, (players_only['Prop'] / players_only['Mean_Outcome'])) | |
| players_only['poisson_var'] = players_only.apply(calculate_poisson, axis=1) | |
| players_only['10%'] = overall_file.quantile(0.1, axis=1) | |
| players_only['90%'] = overall_file.quantile(0.9, axis=1) | |
| players_only['Over'] = np_where(players_only['Prop'] <= 3, players_only['poisson_var'], prop_check[prop_check > 0].count(axis=1)/float(total_sims)) | |
| players_only['Imp Over'] = players_only['Player'].map(over_dict) | |
| players_only['Over%'] = players_only[["Over", "Imp Over", "Trending Over"]].mean(axis=1) | |
| players_only['Under'] = np_where(players_only['Prop'] <= 3, 1 - players_only['poisson_var'], prop_check[prop_check < 0].count(axis=1)/float(total_sims)) | |
| players_only['Imp Under'] = players_only['Player'].map(under_dict) | |
| players_only['Under%'] = players_only[["Under", "Imp Under", "Trending Under"]].mean(axis=1) | |
| players_only['Prop_avg'] = players_only['Prop'].mean() / 100 | |
| players_only['prop_threshold'] = .10 | |
| players_only = players_only[players_only['Mean_Outcome'] > 0] | |
| players_only['Over_diff'] = players_only['Over%'] - players_only['Imp Over'] | |
| players_only['Under_diff'] = players_only['Under%'] - players_only['Imp Under'] | |
| players_only['Bet_check'] = np.where(players_only['Over_diff'] > players_only['Under_diff'], players_only['Over_diff'] * players_only['over_adj'], players_only['Under_diff'] * players_only['under_adj']) | |
| players_only['Bet_suggest'] = np.where(players_only['Over_diff'] > players_only['Under_diff'], "Over" , "Under") | |
| players_only['Bet?'] = np.where(players_only['Bet_check'] >= players_only['prop_threshold'], players_only['Bet_suggest'], "No Bet") | |
| players_only['Edge'] = players_only['Bet_check'] | |
| players_only['Prop Type'] = prop | |
| players_only['Player'] = hold_file[['Player']] | |
| players_only['Team'] = players_only['Player'].map(team_dict) | |
| leg_outcomes = players_only[['Player', 'Team', 'Book', 'Prop Type', 'Prop', 'Mean_Outcome', 'Imp Over', 'Trending Over', 'Over%', 'Imp Under', 'Trending Under', 'Under%', 'Bet?', 'Edge']] | |
| sim_all_hold = pd.concat([sim_all_hold, leg_outcomes], ignore_index=True) | |
| final_outcomes = sim_all_hold | |
| st.write(f'finished {prop} for {books}') | |
| elif prop_type_var != 'All Props': | |
| player_df = player_stats.copy() | |
| if game_select_var == 'Aggregate': | |
| prop_df_raw = prop_frame[['Player', 'book', 'over_prop', 'over_line', 'under_line', 'prop_type', 'Trending Over', 'Trending Under']] | |
| elif game_select_var == 'Pick6': | |
| prop_df_raw = pick_frame[['Player', 'book', 'over_prop', 'over_line', 'under_line', 'prop_type', 'Trending Over', 'Trending Under']] | |
| for books in book_selections: | |
| prop_df = prop_df_raw[prop_df_raw['book'] == books] | |
| if prop_type_var == "NBA_GAME_PLAYER_POINTS": | |
| prop_df = prop_df[prop_df['prop_type'] == 'NBA_GAME_PLAYER_POINTS'] | |
| elif prop_type_var == "Points": | |
| prop_df = prop_df[prop_df['prop_type'] == 'Points'] | |
| elif prop_type_var == "NBA_GAME_PLAYER_REBOUNDS": | |
| prop_df = prop_df[prop_df['prop_type'] == 'NBA_GAME_PLAYER_REBOUNDS'] | |
| elif prop_type_var == "Rebounds": | |
| prop_df = prop_df[prop_df['prop_type'] == 'Rebounds'] | |
| elif prop_type_var == "NBA_GAME_PLAYER_ASSISTS": | |
| prop_df = prop_df[prop_df['prop_type'] == 'NBA_GAME_PLAYER_ASSISTS'] | |
| elif prop_type_var == "Assists": | |
| prop_df = prop_df[prop_df['prop_type'] == 'Assists'] | |
| elif prop_type_var == "NBA_GAME_PLAYER_3_POINTERS_MADE": | |
| prop_df = prop_df[prop_df['prop_type'] == 'NBA_GAME_PLAYER_3_POINTERS_MADE'] | |
| elif prop_type_var == "3-Pointers Made": | |
| prop_df = prop_df[prop_df['prop_type'] == '3-Pointers Made'] | |
| elif prop_type_var == "NBA_GAME_PLAYER_POINTS_REBOUNDS_ASSISTS": | |
| prop_df = prop_df[prop_df['prop_type'] == 'NBA_GAME_PLAYER_POINTS_REBOUNDS_ASSISTS'] | |
| elif prop_type_var == "Points + Assists + Rebounds": | |
| prop_df = prop_df[prop_df['prop_type'] == 'Points + Assists + Rebounds'] | |
| elif prop_type_var == "NBA_GAME_PLAYER_POINTS_REBOUNDS": | |
| prop_df = prop_df[prop_df['prop_type'] == 'NBA_GAME_PLAYER_POINTS_REBOUNDS'] | |
| elif prop_type_var == "Points + Rebounds": | |
| prop_df = prop_df[prop_df['prop_type'] == 'Points + Rebounds'] | |
| elif prop_type_var == "NBA_GAME_PLAYER_POINTS_ASSISTS": | |
| prop_df = prop_df[prop_df['prop_type'] == 'NBA_GAME_PLAYER_POINTS_ASSISTS'] | |
| elif prop_type_var == "Points + Assists": | |
| prop_df = prop_df[prop_df['prop_type'] == 'Points + Assists'] | |
| elif prop_type_var == "NBA_GAME_PLAYER_REBOUNDS_ASSISTS": | |
| prop_df = prop_df[prop_df['prop_type'] == 'NBA_GAME_PLAYER_REBOUNDS_ASSISTS'] | |
| elif prop_type_var == "Assists + Rebounds": | |
| prop_df = prop_df[prop_df['prop_type'] == 'Assists + Rebounds'] | |
| prop_df = prop_df[['Player', 'book', 'over_prop', 'over_line', 'under_line', 'prop_type', 'Trending Over', 'Trending Under']] | |
| prop_df = prop_df.rename(columns={"over_prop": "Prop"}) | |
| prop_df['Over'] = 1 / prop_df['over_line'] | |
| prop_df['Under'] = 1 / prop_df['under_line'] | |
| prop_dict = dict(zip(prop_df.Player, prop_df.Prop)) | |
| prop_type_dict = dict(zip(prop_df.Player, prop_df.prop_type)) | |
| book_dict = dict(zip(prop_df.Player, prop_df.book)) | |
| over_dict = dict(zip(prop_df.Player, prop_df.Over)) | |
| under_dict = dict(zip(prop_df.Player, prop_df.Under)) | |
| trending_over_dict = dict(zip(prop_df.Player, prop_df['Trending Over'])) | |
| trending_under_dict = dict(zip(prop_df.Player, prop_df['Trending Under'])) | |
| player_df['book'] = player_df['Player'].map(book_dict) | |
| player_df['Prop'] = player_df['Player'].map(prop_dict) | |
| player_df['prop_type'] = player_df['Player'].map(prop_type_dict) | |
| player_df['Trending Over'] = player_df['Player'].map(trending_over_dict) | |
| player_df['Trending Under'] = player_df['Player'].map(trending_under_dict) | |
| df = player_df.reset_index(drop=True) | |
| team_dict = dict(zip(df.Player, df.Team)) | |
| total_sims = 1000 | |
| df.replace("", 0, inplace=True) | |
| if prop_type_var == "NBA_GAME_PLAYER_POINTS" or prop_type_var == "Points": | |
| df['Median'] = pd.to_numeric(df['Points'], errors='coerce') | |
| elif prop_type_var == "NBA_GAME_PLAYER_REBOUNDS" or prop_type_var == "Rebounds": | |
| df['Median'] = pd.to_numeric(df['Rebounds'], errors='coerce') | |
| elif prop_type_var == "NBA_GAME_PLAYER_ASSISTS" or prop_type_var == "Assists": | |
| df['Median'] = pd.to_numeric(df['Assists'], errors='coerce') | |
| elif prop_type_var == "NBA_GAME_PLAYER_3_POINTERS_MADE" or prop_type_var == "3-Pointers Made": | |
| df['Median'] = pd.to_numeric(df['3P'], errors='coerce') | |
| elif prop_type_var == "NBA_GAME_PLAYER_POINTS_REBOUNDS_ASSISTS" or prop_type_var == "Points + Assists + Rebounds": | |
| df['Median'] = pd.to_numeric(df['Points'], errors='coerce') + pd.to_numeric(df['Rebounds'], errors='coerce') + pd.to_numeric(df['Assists'], errors='coerce') | |
| elif prop_type_var == "NBA_GAME_PLAYER_POINTS_REBOUNDS" or prop_type_var == "Points + Rebounds": | |
| df['Median'] = pd.to_numeric(df['Points'], errors='coerce') + pd.to_numeric(df['Rebounds'], errors='coerce') | |
| elif prop_type_var == "NBA_GAME_PLAYER_POINTS_ASSISTS" or prop_type_var == "Points + Assists": | |
| df['Median'] = pd.to_numeric(df['Points'], errors='coerce') + pd.to_numeric(df['Assists'], errors='coerce') | |
| elif prop_type_var == "NBA_GAME_PLAYER_REBOUNDS_ASSISTS" or prop_type_var == "Assists + Rebounds": | |
| df['Median'] = pd.to_numeric(df['Rebounds'], errors='coerce') + pd.to_numeric(df['Assists'], errors='coerce') | |
| flex_file = df.copy() | |
| flex_file['Floor'] = flex_file['Median'] * .25 | |
| flex_file['Ceiling'] = flex_file['Median'] + (flex_file['Median'] * 1.75) | |
| flex_file['STD'] = flex_file['Median'] / 4 | |
| flex_file['Prop'] = flex_file['Player'].map(prop_dict) | |
| flex_file = flex_file[['Player', 'book', 'Prop', 'Floor', 'Median', 'Ceiling', 'STD']] | |
| hold_file = flex_file.copy() | |
| overall_file = flex_file.copy() | |
| prop_file = flex_file.copy() | |
| overall_players = overall_file[['Player']] | |
| for x in range(0,total_sims): | |
| prop_file[x] = prop_file['Prop'] | |
| prop_file = prop_file.drop(['Player', 'book', 'Prop', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1) | |
| for x in range(0,total_sims): | |
| overall_file[x] = np.random.normal(overall_file['Median'],overall_file['STD']) | |
| overall_file=overall_file.drop(['Player', 'book', 'Prop', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1) | |
| players_only = hold_file[['Player']] | |
| player_outcomes = pd.merge(players_only, overall_file, left_index=True, right_index=True) | |
| prop_check = (overall_file - prop_file) | |
| players_only['Mean_Outcome'] = overall_file.mean(axis=1) | |
| players_only['Book'] = players_only['Player'].map(book_dict) | |
| players_only['Prop'] = players_only['Player'].map(prop_dict) | |
| players_only['Trending Over'] = players_only['Player'].map(trending_over_dict) | |
| players_only['Trending Under'] = players_only['Player'].map(trending_under_dict) | |
| players_only['over_adj'] = np_where((players_only['Mean_Outcome'] - players_only['Prop']) > 0, 1, (players_only['Mean_Outcome'] / players_only['Prop'])) | |
| players_only['under_adj'] = np_where((players_only['Prop'] - players_only['Mean_Outcome']) > 0, 1, (players_only['Prop'] / players_only['Mean_Outcome'])) | |
| players_only['poisson_var'] = players_only.apply(calculate_poisson, axis=1) | |
| players_only['10%'] = overall_file.quantile(0.1, axis=1) | |
| players_only['90%'] = overall_file.quantile(0.9, axis=1) | |
| players_only['Over'] = np_where(players_only['Prop'] <= 3, players_only['poisson_var'], prop_check[prop_check > 0].count(axis=1)/float(total_sims)) | |
| players_only['Imp Over'] = players_only['Player'].map(over_dict) | |
| players_only['Over%'] = players_only[["Over", "Imp Over", "Trending Over"]].mean(axis=1) | |
| players_only['Under'] = np_where(players_only['Prop'] <= 3, 1 - players_only['poisson_var'], prop_check[prop_check < 0].count(axis=1)/float(total_sims)) | |
| players_only['Imp Under'] = players_only['Player'].map(under_dict) | |
| players_only['Under%'] = players_only[["Under", "Imp Under", "Trending Under"]].mean(axis=1) | |
| players_only['Prop_avg'] = players_only['Prop'].mean() / 100 | |
| players_only['prop_threshold'] = .10 | |
| players_only = players_only[players_only['Mean_Outcome'] > 0] | |
| players_only['Over_diff'] = players_only['Over%'] - players_only['Imp Over'] | |
| players_only['Under_diff'] = players_only['Under%'] - players_only['Imp Under'] | |
| players_only['Bet_check'] = np.where(players_only['Over_diff'] > players_only['Under_diff'], players_only['Over_diff'] * players_only['over_adj'], players_only['Under_diff'] * players_only['under_adj']) | |
| players_only['Bet_suggest'] = np.where(players_only['Over_diff'] > players_only['Under_diff'], "Over" , "Under") | |
| players_only['Bet?'] = np.where(players_only['Bet_check'] >= players_only['prop_threshold'], players_only['Bet_suggest'], "No Bet") | |
| players_only['Edge'] = players_only['Bet_check'] | |
| players_only['Prop Type'] = prop_type_var | |
| players_only['Player'] = hold_file[['Player']] | |
| players_only['Team'] = players_only['Player'].map(team_dict) | |
| leg_outcomes = players_only[['Player', 'Team', 'Book', 'Prop Type', 'Prop', 'Mean_Outcome', 'Imp Over', 'Trending Over', 'Over%', 'Imp Under', 'Trending Under', 'Under%', 'Bet?', 'Edge']] | |
| sim_all_hold = pd.concat([sim_all_hold, leg_outcomes], ignore_index=True) | |
| final_outcomes = sim_all_hold | |
| st.write(f'finished {prop_type_var} for {books}') | |
| final_outcomes = final_outcomes.dropna() | |
| if game_select_var == 'Pick6': | |
| final_outcomes = final_outcomes.drop_duplicates(subset=['Player', 'Prop Type']) | |
| final_outcomes = final_outcomes.sort_values(by='Edge', ascending=False) | |
| with df_hold_container: | |
| df_hold_container = st.empty() | |
| st.dataframe(final_outcomes.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(sim_format, precision=2), height=500, use_container_width = True) | |
| with export_container: | |
| export_container = st.empty() | |
| st.download_button( | |
| label="Export Projections", | |
| data=convert_df_to_csv(final_outcomes), | |
| file_name='NBA_prop_proj.csv', | |
| mime='text/csv', | |
| key='prop_proj', | |
| ) |