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 pulp | |
| import numpy as np | |
| from numpy import where as np_where | |
| import pandas as pd | |
| import streamlit as st | |
| import gspread | |
| import pymongo | |
| from itertools import combinations | |
| import scipy.stats as stats | |
| from time import sleep as time_sleep | |
| def init_conn(): | |
| uri = st.secrets['mongo_uri'] | |
| client = pymongo.MongoClient(uri, retryWrites=True, serverSelectionTimeoutMS=500000) | |
| db = client["NHL_Database"] | |
| return db | |
| db = init_conn() | |
| prop_table_options = ['NHL_GAME_PLAYER_SHOTS_ON_GOAL', 'NHL_GAME_PLAYER_POINTS', 'NHL_GAME_PLAYER_SHOTS_BLOCKED', 'NHL_GAME_PLAYER_ASSISTS'] | |
| 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%}'} | |
| all_sim_vars = ['NHL_GAME_PLAYER_SHOTS_ON_GOAL', 'NHL_GAME_PLAYER_POINTS', 'NHL_GAME_PLAYER_SHOTS_BLOCKED', 'NHL_GAME_PLAYER_ASSISTS'] | |
| pick6_sim_vars = ['Points', 'Shots on Goal', 'Assists', 'Blocks'] | |
| sim_all_hold = pd.DataFrame(columns=['Player', 'Prop Type', 'Prop', 'Mean_Outcome', 'Imp Over', 'Trending Over', 'Over%', 'Imp Under', 'Trending Under', 'Under%', 'Bet?', 'Edge']) | |
| st.markdown(""" | |
| <style> | |
| /* Tab styling */ | |
| .stTabs [data-baseweb="tab-list"] { | |
| gap: 8px; | |
| padding: 4px; | |
| } | |
| .stTabs [data-baseweb="tab"] { | |
| height: 50px; | |
| white-space: pre-wrap; | |
| background-color: #DAA520; | |
| color: white; | |
| border-radius: 10px; | |
| gap: 1px; | |
| padding: 10px 20px; | |
| font-weight: bold; | |
| transition: all 0.3s ease; | |
| } | |
| .stTabs [aria-selected="true"] { | |
| background-color: #DAA520; | |
| border: 3px solid #FFD700; | |
| color: white; | |
| } | |
| .stTabs [data-baseweb="tab"]:hover { | |
| background-color: #FFD700; | |
| cursor: pointer; | |
| } | |
| </style>""", unsafe_allow_html=True) | |
| def pull_baselines(): | |
| collection = db["Prop_Betting_Table"] | |
| cursor = collection.find() | |
| raw_display = pd.DataFrame(cursor) | |
| prop_display = raw_display[raw_display['Player'] != ""] | |
| prop_display['Player Blocks'].replace("", np.nan, inplace=True) | |
| prop_table = prop_display[['Player', 'Position', 'Team', 'Opp', 'Team_Total', 'Player SOG', 'Player Goals', 'Player Assists', | |
| 'Player TP', 'Player Blocks', 'Player Saves']] | |
| prop_table['Player'].replace(['JJ Peterka', 'Alexander Killorn', 'Matt Boldy', 'Nick Paul', 'Alex Kerfoot'], | |
| ['John-Jason Peterka', 'Alex Killorn', 'Matthew Boldy', 'Nicholas Paul', 'Alexander Kerfoot'], inplace=True) | |
| prop_table['Player'] = prop_table['Player'].str.strip() | |
| stat_columns = ['Team_Total', 'Player SOG', 'Player Goals', 'Player Assists', 'Player TP', 'Player Blocks', 'Player Saves'] | |
| for stat in stat_columns: | |
| prop_table[stat] = prop_table[stat].astype(float) | |
| collection = db["prop_trends"] | |
| cursor = collection.find() | |
| raw_display = pd.DataFrame(cursor) | |
| raw_display.replace('', np.nan, inplace=True) | |
| prop_trends = raw_display.dropna(subset='Player') | |
| prop_trends['Player'].replace(['JJ Peterka', 'Alexander Killorn', 'Matt Boldy', 'Nick Paul', 'Alex Kerfoot'], | |
| ['John-Jason Peterka', 'Alex Killorn', 'Matthew Boldy', 'Nicholas Paul', 'Alexander Kerfoot'], inplace=True) | |
| prop_trends = prop_trends.drop(columns=['_id', 'index']) | |
| collection = db["Pick6_ingest"] | |
| cursor = collection.find() | |
| raw_display = pd.DataFrame(cursor) | |
| raw_display.replace('', np.nan, inplace=True) | |
| pick_frame = raw_display.dropna(subset='Player') | |
| pick_frame['Player'].replace(['JJ Peterka', 'Alexander Killorn', 'Matt Boldy', 'Nick Paul', 'Alex Kerfoot'], | |
| ['John-Jason Peterka', 'Alex Killorn', 'Matthew Boldy', 'Nicholas Paul', 'Alexander Kerfoot'], inplace=True) | |
| pick_frame = pick_frame.drop(columns=['_id', 'index']) | |
| team_dict = dict(zip(prop_table['Player'], prop_table['Team'])) | |
| return prop_table, prop_trends, pick_frame, team_dict | |
| 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 convert_df_to_csv(df): | |
| return df.to_csv().encode('utf-8') | |
| prop_display, prop_trends, pick_frame, team_dict = pull_baselines() | |
| tab1, tab2, tab3 = st.tabs(["Player Stat Table", 'Prop Trend Table', 'Stat Specific Simulations']) | |
| with tab1: | |
| with st.expander("Info and Filters"): | |
| if st.button("Reset Data", key='reset1'): | |
| st.cache_data.clear() | |
| prop_display, prop_trends, pick_frame, team_dict = pull_baselines() | |
| team_var = st.radio("Would you like to view all teams or specific ones?", ('All', 'Specific Teams'), key='team_var1') | |
| if team_var == 'Specific Teams': | |
| team_var = st.multiselect('Which teams would you like to include in the tables?', options = prop_display['Team'].unique(), key='team_var2') | |
| elif team_var == 'All': | |
| team_var = prop_display['Team'].unique() | |
| prop_frame = prop_display[prop_display['Team'].isin(team_var)] | |
| st.dataframe(prop_frame.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True) | |
| st.download_button( | |
| label="Export Table", | |
| data=convert_df_to_csv(prop_frame), | |
| file_name='NHL_prop_stat_export.csv', | |
| mime='text/csv', | |
| key='prop_export', | |
| ) | |
| with tab2: | |
| with st.expander("Info and Filters"): | |
| if st.button("Reset Data", key='reset3'): | |
| st.cache_data.clear() | |
| prop_display, prop_trends, pick_frame, team_dict = pull_baselines() | |
| 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 = prop_trends['Team'].unique(), key='team_var5') | |
| elif split_var5 == 'All': | |
| team_var5 = prop_trends.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_trends[prop_trends['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_disp), | |
| file_name='NHL_prop_trends_export.csv', | |
| mime='text/csv', | |
| ) | |
| with tab3: | |
| st.info('The Over and Under percentages are a composite percentage based on simulations, historical performance, and implied probabilities, and may be different than you would expect based purely on the median projection. Likewise, the Edge of a bet is not the only indicator of if you should make the bet or not as the suggestion is using a base acceptable threshold to determine how much edge you should have for each stat category.') | |
| if st.button("Reset Data/Load Data", key='reset5'): | |
| st.cache_data.clear() | |
| prop_display, prop_trends, pick_frame, team_dict = pull_baselines() | |
| settings_container = st.container() | |
| 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_trends[['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', 'NHL_GAME_PLAYER_POINTS', 'NHL_GAME_PLAYER_SHOTS_ON_GOAL', 'NHL_GAME_PLAYER_ASSISTS', 'NHL_GAME_PLAYER_SHOTS_BLOCKED']) | |
| elif game_select_var == 'Pick6': | |
| prop_type_var = st.selectbox('Select prop category', options = ['All Props', 'Points', 'Shots on Goal', 'Assists', 'Blocks']) | |
| with col4: | |
| st.download_button( | |
| label="Download Prop Source", | |
| data=convert_df_to_csv(prop_df), | |
| file_name='NHL_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_trends[['Player', 'book', 'over_prop', 'over_line', 'under_line', 'prop_type', 'Trending Over', 'Trending Under']] | |
| sim_vars = ['NHL_GAME_PLAYER_POINTS', 'NHL_GAME_PLAYER_SHOTS_ON_GOAL', 'NHL_GAME_PLAYER_ASSISTS', 'NHL_GAME_PLAYER_SHOTS_BLOCKED'] | |
| 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', 'Shots on Goal', 'Assists', 'Blocks'] | |
| player_df = prop_display.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 == 'NHL_GAME_PLAYER_POINTS' or prop == 'Points': | |
| df['Median'] = df['Player TP'] | |
| elif prop == 'NHL_GAME_PLAYER_SHOTS_ON_GOAL' or prop == 'Shots on Goal': | |
| df['Median'] = df['Player SOG'] | |
| elif prop == 'NHL_GAME_PLAYER_ASSISTS' or prop == 'Assists': | |
| df['Median'] = df['Player Assists'] | |
| elif prop == 'NHL_GAME_PLAYER_SHOTS_BLOCKED' or prop == 'Blocks': | |
| df['Median'] = df['Player Blocks'] | |
| flex_file = df.copy() | |
| flex_file['Floor'] = (flex_file['Median'] * .15) | |
| flex_file['Ceiling'] = flex_file['Median'] + (flex_file['Median'] * 1) | |
| flex_file['STD'] = (flex_file['Median']/3) | |
| 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['Prop'] = players_only['Player'].map(prop_dict) | |
| players_only['Book'] = players_only['Player'].map(book_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'] * 0.4) + (players_only['Trending Over'] * 0.4) + (players_only['Imp Over'] * 0.2) | |
| 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'] * 0.4) + (players_only['Trending Under'] * 0.4) + (players_only['Imp Under'] * 0.2) | |
| 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 = prop_display.copy() | |
| if game_select_var == 'Aggregate': | |
| prop_df_raw = prop_trends[['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 == "NHL_GAME_PLAYER_SHOTS_ON_GOAL": | |
| prop_df = prop_df[prop_df['prop_type'] == 'NHL_GAME_PLAYER_SHOTS_ON_GOAL'] | |
| elif prop_type_var == 'Shots on Goal': | |
| prop_df = prop_df[prop_df['prop_type'] == 'Player SOG'] | |
| elif prop_type_var == "NHL_GAME_PLAYER_POINTS": | |
| prop_df = prop_df[prop_df['prop_type'] == 'NHL_GAME_PLAYER_POINTS'] | |
| elif prop_type_var == "Points": | |
| prop_df = prop_df[prop_df['prop_type'] == 'Player TP'] | |
| elif prop_type_var == "NHL_GAME_PLAYER_ASSISTS": | |
| prop_df = prop_df[prop_df['prop_type'] == 'NHL_GAME_PLAYER_ASSISTS'] | |
| elif prop_type_var == "Assists": | |
| prop_df = prop_df[prop_df['prop_type'] == 'Player Assists'] | |
| elif prop_type_var == "NHL_GAME_PLAYER_SHOTS_BLOCKED": | |
| prop_df = prop_df[prop_df['prop_type'] == 'NHL_GAME_PLAYER_SHOTS_BLOCKED'] | |
| elif prop_type_var == "Blocks": | |
| prop_df = prop_df[prop_df['prop_type'] == 'Player Blocks'] | |
| prop_df = prop_df[['Player', 'book', 'over_prop', 'over_line', 'under_line']] | |
| 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_type_var == 'NHL_GAME_PLAYER_POINTS' or prop_type_var == 'Points': | |
| df['Median'] = df['Player TP'] | |
| elif prop_type_var == 'NHL_GAME_PLAYER_SHOTS_ON_GOAL' or prop_type_var == 'Shots on Goal': | |
| df['Median'] = df['Player SOG'] | |
| elif prop_type_var == 'NHL_GAME_PLAYER_ASSISTS' or prop_type_var == 'Assists': | |
| df['Median'] = df['Player Assists'] | |
| elif prop_type_var == 'NHL_GAME_PLAYER_SHOTS_BLOCKED' or prop_type_var == 'Blocks': | |
| df['Median'] = df['Player Blocks'] | |
| flex_file = df.copy() | |
| flex_file['Floor'] = (flex_file['Median'] * .15) | |
| flex_file['Ceiling'] = flex_file['Median'] + (flex_file['Median'] * 1) | |
| flex_file['STD'] = (flex_file['Median']/3) | |
| 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['Prop'] = players_only['Player'].map(prop_dict) | |
| players_only['Book'] = players_only['Player'].map(book_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'] * 0.4) + (players_only['Trending Over'] * 0.4) + (players_only['Imp Over'] * 0.2) | |
| 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'] * 0.4) + (players_only['Trending Under'] * 0.4) + (players_only['Imp Under'] * 0.2) | |
| 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', 'Prop Type', '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[final_outcomes['Prop'] > 0] | |
| 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(precision=2), 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='NHL_prop_proj.csv', | |
| mime='text/csv', | |
| key='prop_proj', | |
| ) |