Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -720,6 +720,10 @@ with tab2:
|
|
720 |
raw_baselines = dk_roo_raw
|
721 |
elif slate_var1 == 'Paydirt (Secondary)':
|
722 |
raw_baselines = dk_roo_raw_2
|
|
|
|
|
|
|
|
|
723 |
st.info("If you are uploading a portfolio, note that there is an adjustments to projections and deviation mapping to prevent 'Projection Bias' and create a fair simulation")
|
724 |
insert_port1 = st.selectbox("Are you uploading a portfolio?", ('No', 'Yes'))
|
725 |
if insert_port1 == 'Yes':
|
@@ -746,383 +750,403 @@ with tab2:
|
|
746 |
scaling_var = 15
|
747 |
|
748 |
with col2:
|
749 |
-
|
750 |
-
|
751 |
-
|
752 |
-
|
753 |
-
|
754 |
-
|
755 |
-
|
756 |
-
|
757 |
-
|
758 |
-
|
759 |
-
|
760 |
-
|
761 |
-
|
762 |
-
|
763 |
-
st.
|
764 |
-
|
765 |
-
|
766 |
-
|
767 |
-
|
768 |
-
|
769 |
-
|
770 |
-
|
771 |
-
|
772 |
-
|
773 |
-
|
774 |
-
|
775 |
-
|
776 |
-
|
777 |
-
|
778 |
-
field_growth = 100 * strength_grow
|
779 |
-
|
780 |
-
Sort_function = 'Median'
|
781 |
-
if Sort_function == 'Median':
|
782 |
-
Sim_function = 'Projection'
|
783 |
-
elif Sort_function == 'Own':
|
784 |
-
Sim_function = 'Own'
|
785 |
-
|
786 |
-
if slate_var1 == 'User':
|
787 |
-
OwnFrame = proj_dataframe
|
788 |
-
if contest_var1 == 'Large':
|
789 |
-
OwnFrame['Own%'] = np.where((OwnFrame['Position'] == 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (10 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean(), OwnFrame['Own'])
|
790 |
-
OwnFrame['Own%'] = np.where((OwnFrame['Position'] != 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (5 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean(), OwnFrame['Own%'])
|
791 |
-
OwnFrame['Own%'] = np.where(OwnFrame['Own%'] > 75, 75, OwnFrame['Own%'])
|
792 |
-
OwnFrame['Own'] = OwnFrame['Own%'] * (500 / OwnFrame['Own%'].sum())
|
793 |
-
if contest_var1 == 'Medium':
|
794 |
-
OwnFrame['Own%'] = np.where((OwnFrame['Position'] == 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (6 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean(), OwnFrame['Own'])
|
795 |
-
OwnFrame['Own%'] = np.where((OwnFrame['Position'] != 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (3 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean(), OwnFrame['Own%'])
|
796 |
-
OwnFrame['Own%'] = np.where(OwnFrame['Own%'] > 75, 75, OwnFrame['Own%'])
|
797 |
-
OwnFrame['Own'] = OwnFrame['Own%'] * (500 / OwnFrame['Own%'].sum())
|
798 |
-
if contest_var1 == 'Small':
|
799 |
-
OwnFrame['Own%'] = np.where((OwnFrame['Position'] == 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (3 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean(), OwnFrame['Own'])
|
800 |
-
OwnFrame['Own%'] = np.where((OwnFrame['Position'] != 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (1.5 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean(), OwnFrame['Own%'])
|
801 |
-
OwnFrame['Own%'] = np.where(OwnFrame['Own%'] > 75, 75, OwnFrame['Own%'])
|
802 |
-
OwnFrame['Own'] = OwnFrame['Own%'] * (500 / OwnFrame['Own%'].sum())
|
803 |
-
Overall_Proj = OwnFrame[['Player', 'Team', 'Position', 'Median', 'Own', 'Salary']]
|
804 |
-
|
805 |
-
del OwnFrame
|
806 |
-
|
807 |
-
elif slate_var1 != 'User':
|
808 |
-
initial_proj = raw_baselines
|
809 |
-
drop_frame = initial_proj.drop_duplicates(subset = 'Player',keep = 'first')
|
810 |
-
OwnFrame = drop_frame[['Player', 'Team', 'Position', 'Median', 'Own', 'Floor', 'Ceiling', 'Salary']]
|
811 |
-
if contest_var1 == 'Large':
|
812 |
-
OwnFrame['Own%'] = np.where((OwnFrame['Position'] == 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (10 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean(), OwnFrame['Own'])
|
813 |
-
OwnFrame['Own%'] = np.where((OwnFrame['Position'] != 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (5 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean(), OwnFrame['Own%'])
|
814 |
-
OwnFrame['Own%'] = np.where(OwnFrame['Own%'] > 75, 75, OwnFrame['Own%'])
|
815 |
-
OwnFrame['Own'] = OwnFrame['Own%'] * (500 / OwnFrame['Own%'].sum())
|
816 |
-
if contest_var1 == 'Medium':
|
817 |
-
OwnFrame['Own%'] = np.where((OwnFrame['Position'] == 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (6 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean(), OwnFrame['Own'])
|
818 |
-
OwnFrame['Own%'] = np.where((OwnFrame['Position'] != 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (3 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean(), OwnFrame['Own%'])
|
819 |
-
OwnFrame['Own%'] = np.where(OwnFrame['Own%'] > 75, 75, OwnFrame['Own%'])
|
820 |
-
OwnFrame['Own'] = OwnFrame['Own%'] * (500 / OwnFrame['Own%'].sum())
|
821 |
-
if contest_var1 == 'Small':
|
822 |
-
OwnFrame['Own%'] = np.where((OwnFrame['Position'] == 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (3 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean(), OwnFrame['Own'])
|
823 |
-
OwnFrame['Own%'] = np.where((OwnFrame['Position'] != 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (1.5 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean(), OwnFrame['Own%'])
|
824 |
-
OwnFrame['Own%'] = np.where(OwnFrame['Own%'] > 75, 75, OwnFrame['Own%'])
|
825 |
-
OwnFrame['Own'] = OwnFrame['Own%'] * (500 / OwnFrame['Own%'].sum())
|
826 |
-
Overall_Proj = OwnFrame[['Player', 'Team', 'Position', 'Median', 'Own', 'Floor', 'Ceiling', 'Salary']]
|
827 |
-
|
828 |
-
del initial_proj
|
829 |
-
del drop_frame
|
830 |
-
del OwnFrame
|
831 |
-
|
832 |
-
if insert_port == 1:
|
833 |
-
UserPortfolio = portfolio_dataframe[['CPT', 'FLEX1', 'FLEX2', 'FLEX3', 'FLEX4', 'FLEX5']]
|
834 |
-
elif insert_port == 0:
|
835 |
-
UserPortfolio = pd.DataFrame(columns = ['CPT', 'FLEX1', 'FLEX2', 'FLEX3', 'FLEX4', 'FLEX5'])
|
836 |
-
|
837 |
-
Overall_Proj.replace('', np.nan, inplace=True)
|
838 |
-
Overall_Proj = Overall_Proj.dropna(subset=['Median'])
|
839 |
-
Overall_Proj = Overall_Proj.assign(Value=lambda x: (x.Median / (x.Salary / 1000)))
|
840 |
-
Overall_Proj['Sort_var'] = (Overall_Proj['Median'].rank(ascending=False) + Overall_Proj['Value'].rank(ascending=False)) / 2
|
841 |
-
Overall_Proj = Overall_Proj.sort_values(by='Sort_var', ascending=False)
|
842 |
-
Overall_Proj['Own'] = np.where((Overall_Proj['Median'] > 0) & (Overall_Proj['Own'] == 0), 1, Overall_Proj['Own'])
|
843 |
-
Overall_Proj = Overall_Proj.loc[Overall_Proj['Own'] > 0]
|
844 |
-
|
845 |
-
Overall_Proj['Floor'] = np.where(Overall_Proj['Position'] == 'QB', Overall_Proj['Median'] * .5, Overall_Proj['Median'] * .25)
|
846 |
-
Overall_Proj['Ceiling'] = np.where(Overall_Proj['Position'] == 'WR', Overall_Proj['Median'] + Overall_Proj['Median'], Overall_Proj['Median'] + Overall_Proj['Floor'])
|
847 |
-
Overall_Proj['STDev'] = Overall_Proj['Median'] / 4
|
848 |
-
|
849 |
-
Teams_used = Overall_Proj['Team'].drop_duplicates().reset_index(drop=True)
|
850 |
-
Teams_used = Teams_used.reset_index()
|
851 |
-
Teams_used['team_item'] = Teams_used['index'] + 1
|
852 |
-
Teams_used = Teams_used.drop(columns=['index'])
|
853 |
-
Teams_used_dictraw = Teams_used.drop(columns=['team_item'])
|
854 |
-
Teams_used_dict = Teams_used_dictraw.to_dict()
|
855 |
-
|
856 |
-
del Teams_used_dictraw
|
857 |
-
|
858 |
-
team_list = Teams_used['Team'].to_list()
|
859 |
-
item_list = Teams_used['team_item'].to_list()
|
860 |
-
|
861 |
-
FieldStrength_raw = Strength_var + ((30 - len(Teams_used)) * .01)
|
862 |
-
FieldStrength = FieldStrength_raw - (FieldStrength_raw * (20000 / Contest_Size))
|
863 |
-
|
864 |
-
del FieldStrength_raw
|
865 |
-
|
866 |
-
if FieldStrength < 0:
|
867 |
-
FieldStrength = Strength_var
|
868 |
-
field_split = Strength_var
|
869 |
-
|
870 |
-
for checkVar in range(len(team_list)):
|
871 |
-
Overall_Proj['Team'] = Overall_Proj['Team'].replace(team_list, item_list)
|
872 |
-
|
873 |
-
flex_raw = Overall_Proj
|
874 |
-
flex_raw.dropna(subset=['Median']).reset_index(drop=True)
|
875 |
-
flex_raw = flex_raw.reset_index(drop=True)
|
876 |
-
flex_raw = flex_raw.sort_values(by='Own', ascending=False)
|
877 |
-
|
878 |
-
pos_players = flex_raw
|
879 |
-
pos_players.dropna(subset=['Median']).reset_index(drop=True)
|
880 |
-
pos_players = pos_players.reset_index(drop=True)
|
881 |
-
|
882 |
-
del flex_raw
|
883 |
-
|
884 |
-
if insert_port == 1:
|
885 |
-
try:
|
886 |
-
# Initialize an empty DataFrame to store raw portfolio data
|
887 |
-
Raw_Portfolio = pd.DataFrame()
|
888 |
-
|
889 |
-
# Split each portfolio column and concatenate to Raw_Portfolio
|
890 |
-
columns_to_process = ['CPT', 'FLEX1', 'FLEX2', 'FLEX3', 'FLEX4', 'FLEX5']
|
891 |
-
for col in columns_to_process:
|
892 |
-
temp_df = UserPortfolio[col].str.split("(", n=1, expand=True)
|
893 |
-
temp_df.columns = [col, 'Drop']
|
894 |
-
Raw_Portfolio = pd.concat([Raw_Portfolio, temp_df], axis=1)
|
895 |
-
|
896 |
-
# Keep only required variables and remove whitespace
|
897 |
-
keep_vars = columns_to_process
|
898 |
-
CleanPortfolio = Raw_Portfolio[keep_vars]
|
899 |
-
CleanPortfolio = CleanPortfolio.apply(lambda x: x.str.strip())
|
900 |
-
|
901 |
-
# Reset index and clean up the DataFrame
|
902 |
-
CleanPortfolio.reset_index(inplace=True)
|
903 |
-
CleanPortfolio['User/Field'] = CleanPortfolio['index'] + 1
|
904 |
-
CleanPortfolio.drop(columns=['index'], inplace=True)
|
905 |
-
CleanPortfolio.replace('', np.nan, inplace=True)
|
906 |
-
CleanPortfolio.dropna(subset=['CPT'], inplace=True)
|
907 |
-
|
908 |
-
# Create cleaport_players DataFrame
|
909 |
-
unique_vals, counts = np.unique(CleanPortfolio.iloc[:, 0:6].values, return_counts=True)
|
910 |
-
cleaport_players = pd.DataFrame(np.column_stack([unique_vals, counts]), columns=['Player', 'Freq']).astype({'Freq': int}).sort_values('Freq', ascending=False).reset_index(drop=True)
|
911 |
-
|
912 |
-
# Merge and update nerf_frame DataFrame
|
913 |
-
nerf_frame = pd.merge(cleaport_players, Overall_Proj, on='Player', how='left')
|
914 |
-
nerf_frame[['Median', 'Floor', 'Ceiling', 'STDev']] *= 1
|
915 |
|
916 |
-
|
917 |
-
|
918 |
-
|
919 |
-
|
920 |
-
|
921 |
-
|
922 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
923 |
|
924 |
-
|
925 |
-
unique_vals, counts = np.unique(CleanPortfolio.iloc[:, 0:6].values, return_counts=True)
|
926 |
-
cleaport_players = pd.DataFrame({'Player': unique_vals, 'Freq': counts}).sort_values('Freq', ascending=False).reset_index(drop=True).astype({'Freq': int})
|
927 |
|
928 |
-
|
929 |
-
|
930 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
931 |
|
932 |
-
|
933 |
-
|
934 |
-
|
935 |
-
CleanPortfolio = UserPortfolio
|
936 |
-
cleaport_players = pd.DataFrame(np.column_stack(np.unique(CleanPortfolio.iloc[:,0:6].values, return_counts=True)),
|
937 |
-
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
938 |
-
cleaport_players['Freq'] = cleaport_players['Freq'].astype(int)
|
939 |
-
nerf_frame = Overall_Proj
|
940 |
|
941 |
-
|
942 |
-
|
943 |
-
|
944 |
-
|
945 |
-
|
946 |
-
|
947 |
-
|
948 |
-
|
949 |
-
'
|
950 |
-
'
|
951 |
-
'
|
952 |
-
|
953 |
-
|
954 |
-
'
|
955 |
-
'
|
956 |
-
'
|
957 |
-
|
958 |
-
|
959 |
-
|
960 |
-
'
|
961 |
-
'
|
962 |
-
'
|
963 |
-
|
964 |
-
'Pos_map':dict(zip(cleaport_players.Player,nerf_frame.Position)),
|
965 |
-
'Own_map':dict(zip(cleaport_players.Player,nerf_frame.Own)),
|
966 |
-
'Team_map':dict(zip(cleaport_players.Player,nerf_frame.Team)),
|
967 |
-
'STDev_map':dict(zip(cleaport_players.Player,nerf_frame.STDev)),
|
968 |
-
'team_check_map':dict(zip(cleaport_players.Player,nerf_frame.Team))
|
969 |
-
}
|
970 |
-
|
971 |
-
del Overall_Proj
|
972 |
-
del nerf_frame
|
973 |
-
|
974 |
-
RunsVar = 1
|
975 |
-
st.write('Seed frame creation')
|
976 |
-
FinalPortfolio, maps_dict = run_seed_frame(seed_depth1, Strength_var, strength_grow, Teams_used, Total_Runs)
|
977 |
-
|
978 |
-
Sim_size = linenum_var1
|
979 |
-
SimVar = 1
|
980 |
-
Sim_Winners = []
|
981 |
-
fp_array = FinalPortfolio.values
|
982 |
-
|
983 |
-
if insert_port == 1:
|
984 |
-
up_array = CleanPortfolio.values
|
985 |
-
|
986 |
-
# Pre-vectorize functions
|
987 |
-
vec_projection_map = np.vectorize(maps_dict['Projection_map'].__getitem__)
|
988 |
-
vec_stdev_map = np.vectorize(maps_dict['STDev_map'].__getitem__)
|
989 |
-
|
990 |
-
if insert_port == 1:
|
991 |
-
vec_up_projection_map = np.vectorize(up_dict['Projection_map'].__getitem__)
|
992 |
-
vec_up_stdev_map = np.vectorize(up_dict['STDev_map'].__getitem__)
|
993 |
-
st.write('Simulating contest on frames')
|
994 |
-
while SimVar <= Sim_size:
|
995 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
996 |
if insert_port == 1:
|
997 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
998 |
elif insert_port == 0:
|
999 |
-
|
|
|
|
|
|
|
|
|
1000 |
|
1001 |
-
|
1002 |
-
|
1003 |
-
|
1004 |
-
|
1005 |
-
|
1006 |
-
axis=1)
|
1007 |
-
]
|
1008 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1009 |
if insert_port == 1:
|
1010 |
-
|
1011 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1012 |
np.sum(np.random.normal(
|
1013 |
-
loc=
|
1014 |
-
scale=
|
1015 |
axis=1)
|
1016 |
]
|
1017 |
-
sample_arrays = np.vstack((sample_arrays1, sample_arrays2))
|
1018 |
-
else:
|
1019 |
-
sample_arrays = sample_arrays1
|
1020 |
-
|
1021 |
-
final_array = sample_arrays[sample_arrays[:, 10].argsort()[::-1]]
|
1022 |
-
best_lineup = final_array[final_array[:, -1].argsort(kind='stable')[::-1][:1]]
|
1023 |
-
Sim_Winners.append(best_lineup)
|
1024 |
-
SimVar += 1
|
1025 |
-
st.write('Contest simulation complete')
|
1026 |
-
|
1027 |
-
Sim_Winner_Frame = pd.DataFrame(np.concatenate(Sim_Winners), columns=FinalPortfolio.columns.tolist() + ['Fantasy'])
|
1028 |
-
Sim_Winner_Frame['GPP_Proj'] = (Sim_Winner_Frame['Projection'] + Sim_Winner_Frame['Fantasy']) / 2
|
1029 |
-
Sim_Winner_Frame['Salary'] = Sim_Winner_Frame['Salary'].astype(int)
|
1030 |
-
Sim_Winner_Frame['Projection'] = Sim_Winner_Frame['Projection'].astype(np.float16)
|
1031 |
-
Sim_Winner_Frame['Fantasy'] = Sim_Winner_Frame['Fantasy'].astype(np.float16)
|
1032 |
-
Sim_Winner_Frame['GPP_Proj'] = Sim_Winner_Frame['GPP_Proj'].astype(np.float16)
|
1033 |
-
Sim_Winner_Frame = Sim_Winner_Frame.sort_values(by='GPP_Proj', ascending=False)
|
1034 |
|
1035 |
-
|
1036 |
-
|
1037 |
-
|
1038 |
-
|
1039 |
-
|
1040 |
-
|
1041 |
-
|
1042 |
-
|
1043 |
-
|
1044 |
-
|
1045 |
-
|
1046 |
-
|
1047 |
-
player_freq = player_freq[['Player', 'Position', 'Team', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
1048 |
|
1049 |
-
|
1050 |
-
|
1051 |
-
|
1052 |
-
|
1053 |
-
|
1054 |
-
|
1055 |
-
|
1056 |
-
|
1057 |
-
|
1058 |
-
|
1059 |
-
|
1060 |
-
|
1061 |
-
|
1062 |
-
|
1063 |
-
|
1064 |
-
|
1065 |
-
|
1066 |
-
|
1067 |
-
|
1068 |
-
|
1069 |
-
|
1070 |
-
|
1071 |
-
|
1072 |
-
|
1073 |
-
|
1074 |
-
|
1075 |
-
|
1076 |
-
|
1077 |
-
|
1078 |
-
|
1079 |
-
|
1080 |
-
|
1081 |
-
|
1082 |
-
|
1083 |
-
|
1084 |
-
|
1085 |
-
|
1086 |
-
|
1087 |
-
|
1088 |
-
|
1089 |
-
|
1090 |
-
|
1091 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1092 |
|
1093 |
-
|
1094 |
-
|
1095 |
-
|
1096 |
-
|
1097 |
-
|
1098 |
-
|
1099 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1100 |
st.download_button(
|
1101 |
label="Export Exposures",
|
1102 |
-
data=convert_df_to_csv(player_freq),
|
1103 |
file_name='player_freq_export.csv',
|
1104 |
mime='text/csv',
|
1105 |
)
|
1106 |
-
|
1107 |
-
|
|
|
1108 |
st.download_button(
|
1109 |
label="Export Exposures",
|
1110 |
-
data=convert_df_to_csv(cpt_freq),
|
1111 |
file_name='cpt_freq_export.csv',
|
1112 |
mime='text/csv',
|
1113 |
)
|
1114 |
-
|
1115 |
-
|
|
|
1116 |
st.download_button(
|
1117 |
label="Export Exposures",
|
1118 |
-
data=convert_df_to_csv(flex_freq),
|
1119 |
file_name='flex_freq_export.csv',
|
1120 |
mime='text/csv',
|
1121 |
-
)
|
1122 |
-
|
1123 |
-
st.download_button(
|
1124 |
-
label="Export Tables",
|
1125 |
-
data=convert_df_to_csv(Sim_Winner_Frame),
|
1126 |
-
file_name='NFL_consim_export.csv',
|
1127 |
-
mime='text/csv',
|
1128 |
-
)
|
|
|
720 |
raw_baselines = dk_roo_raw
|
721 |
elif slate_var1 == 'Paydirt (Secondary)':
|
722 |
raw_baselines = dk_roo_raw_2
|
723 |
+
del dk_roo_raw
|
724 |
+
del dk_roo_raw_2
|
725 |
+
del fd_roo_raw
|
726 |
+
del fd_roo_raw_2
|
727 |
st.info("If you are uploading a portfolio, note that there is an adjustments to projections and deviation mapping to prevent 'Projection Bias' and create a fair simulation")
|
728 |
insert_port1 = st.selectbox("Are you uploading a portfolio?", ('No', 'Yes'))
|
729 |
if insert_port1 == 'Yes':
|
|
|
750 |
scaling_var = 15
|
751 |
|
752 |
with col2:
|
753 |
+
with st.container():
|
754 |
+
if st.button("Simulate Contest", key='sim1'):
|
755 |
+
try:
|
756 |
+
del dst_freq
|
757 |
+
del flex_freq
|
758 |
+
del te_freq
|
759 |
+
del wr_freq
|
760 |
+
del rb_freq
|
761 |
+
del qb_freq
|
762 |
+
del player_freq
|
763 |
+
del Sim_Winner_Export
|
764 |
+
del Sim_Winner_Frame
|
765 |
+
except:
|
766 |
+
pass
|
767 |
+
with st.container():
|
768 |
+
st.write('Contest Simulation Starting')
|
769 |
+
Total_Runs = 1000000
|
770 |
+
seed_depth1 = 5
|
771 |
+
Total_Runs = 2500000
|
772 |
+
if Contest_Size <= 1000:
|
773 |
+
strength_grow = .01
|
774 |
+
elif Contest_Size > 1000 and Contest_Size <= 2500:
|
775 |
+
strength_grow = .025
|
776 |
+
elif Contest_Size > 2500 and Contest_Size <= 5000:
|
777 |
+
strength_grow = .05
|
778 |
+
elif Contest_Size > 5000 and Contest_Size <= 20000:
|
779 |
+
strength_grow = .075
|
780 |
+
elif Contest_Size > 20000:
|
781 |
+
strength_grow = .1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
782 |
|
783 |
+
field_growth = 100 * strength_grow
|
784 |
+
|
785 |
+
Sort_function = 'Median'
|
786 |
+
if Sort_function == 'Median':
|
787 |
+
Sim_function = 'Projection'
|
788 |
+
elif Sort_function == 'Own':
|
789 |
+
Sim_function = 'Own'
|
790 |
+
|
791 |
+
if slate_var1 == 'User':
|
792 |
+
OwnFrame = proj_dataframe
|
793 |
+
if contest_var1 == 'Large':
|
794 |
+
OwnFrame['Own%'] = np.where((OwnFrame['Position'] == 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (10 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean(), OwnFrame['Own'])
|
795 |
+
OwnFrame['Own%'] = np.where((OwnFrame['Position'] != 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (5 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean(), OwnFrame['Own%'])
|
796 |
+
OwnFrame['Own%'] = np.where(OwnFrame['Own%'] > 75, 75, OwnFrame['Own%'])
|
797 |
+
OwnFrame['Own'] = OwnFrame['Own%'] * (500 / OwnFrame['Own%'].sum())
|
798 |
+
if contest_var1 == 'Medium':
|
799 |
+
OwnFrame['Own%'] = np.where((OwnFrame['Position'] == 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (6 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean(), OwnFrame['Own'])
|
800 |
+
OwnFrame['Own%'] = np.where((OwnFrame['Position'] != 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (3 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean(), OwnFrame['Own%'])
|
801 |
+
OwnFrame['Own%'] = np.where(OwnFrame['Own%'] > 75, 75, OwnFrame['Own%'])
|
802 |
+
OwnFrame['Own'] = OwnFrame['Own%'] * (500 / OwnFrame['Own%'].sum())
|
803 |
+
if contest_var1 == 'Small':
|
804 |
+
OwnFrame['Own%'] = np.where((OwnFrame['Position'] == 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (3 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean(), OwnFrame['Own'])
|
805 |
+
OwnFrame['Own%'] = np.where((OwnFrame['Position'] != 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (1.5 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean(), OwnFrame['Own%'])
|
806 |
+
OwnFrame['Own%'] = np.where(OwnFrame['Own%'] > 75, 75, OwnFrame['Own%'])
|
807 |
+
OwnFrame['Own'] = OwnFrame['Own%'] * (500 / OwnFrame['Own%'].sum())
|
808 |
+
Overall_Proj = OwnFrame[['Player', 'Team', 'Position', 'Median', 'Own', 'Salary']]
|
809 |
|
810 |
+
del OwnFrame
|
|
|
|
|
811 |
|
812 |
+
elif slate_var1 != 'User':
|
813 |
+
initial_proj = raw_baselines
|
814 |
+
drop_frame = initial_proj.drop_duplicates(subset = 'Player',keep = 'first')
|
815 |
+
OwnFrame = drop_frame[['Player', 'Team', 'Position', 'Median', 'Own', 'Floor', 'Ceiling', 'Salary']]
|
816 |
+
if contest_var1 == 'Large':
|
817 |
+
OwnFrame['Own%'] = np.where((OwnFrame['Position'] == 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (10 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean(), OwnFrame['Own'])
|
818 |
+
OwnFrame['Own%'] = np.where((OwnFrame['Position'] != 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (5 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean(), OwnFrame['Own%'])
|
819 |
+
OwnFrame['Own%'] = np.where(OwnFrame['Own%'] > 75, 75, OwnFrame['Own%'])
|
820 |
+
OwnFrame['Own'] = OwnFrame['Own%'] * (500 / OwnFrame['Own%'].sum())
|
821 |
+
if contest_var1 == 'Medium':
|
822 |
+
OwnFrame['Own%'] = np.where((OwnFrame['Position'] == 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (6 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean(), OwnFrame['Own'])
|
823 |
+
OwnFrame['Own%'] = np.where((OwnFrame['Position'] != 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (3 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean(), OwnFrame['Own%'])
|
824 |
+
OwnFrame['Own%'] = np.where(OwnFrame['Own%'] > 75, 75, OwnFrame['Own%'])
|
825 |
+
OwnFrame['Own'] = OwnFrame['Own%'] * (500 / OwnFrame['Own%'].sum())
|
826 |
+
if contest_var1 == 'Small':
|
827 |
+
OwnFrame['Own%'] = np.where((OwnFrame['Position'] == 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (3 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] == 'QB', 'Own'].mean(), OwnFrame['Own'])
|
828 |
+
OwnFrame['Own%'] = np.where((OwnFrame['Position'] != 'QB') & (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean() >= 0), OwnFrame['Own'] * (1.5 * (OwnFrame['Own'] - OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean())/100) + OwnFrame.loc[OwnFrame['Position'] != 'QB', 'Own'].mean(), OwnFrame['Own%'])
|
829 |
+
OwnFrame['Own%'] = np.where(OwnFrame['Own%'] > 75, 75, OwnFrame['Own%'])
|
830 |
+
OwnFrame['Own'] = OwnFrame['Own%'] * (500 / OwnFrame['Own%'].sum())
|
831 |
+
Overall_Proj = OwnFrame[['Player', 'Team', 'Position', 'Median', 'Own', 'Floor', 'Ceiling', 'Salary']]
|
832 |
|
833 |
+
del initial_proj
|
834 |
+
del drop_frame
|
835 |
+
del OwnFrame
|
|
|
|
|
|
|
|
|
|
|
836 |
|
837 |
+
if insert_port == 1:
|
838 |
+
UserPortfolio = portfolio_dataframe[['CPT', 'FLEX1', 'FLEX2', 'FLEX3', 'FLEX4', 'FLEX5']]
|
839 |
+
elif insert_port == 0:
|
840 |
+
UserPortfolio = pd.DataFrame(columns = ['CPT', 'FLEX1', 'FLEX2', 'FLEX3', 'FLEX4', 'FLEX5'])
|
841 |
+
|
842 |
+
Overall_Proj.replace('', np.nan, inplace=True)
|
843 |
+
Overall_Proj = Overall_Proj.dropna(subset=['Median'])
|
844 |
+
Overall_Proj = Overall_Proj.assign(Value=lambda x: (x.Median / (x.Salary / 1000)))
|
845 |
+
Overall_Proj['Sort_var'] = (Overall_Proj['Median'].rank(ascending=False) + Overall_Proj['Value'].rank(ascending=False)) / 2
|
846 |
+
Overall_Proj = Overall_Proj.sort_values(by='Sort_var', ascending=False)
|
847 |
+
Overall_Proj['Own'] = np.where((Overall_Proj['Median'] > 0) & (Overall_Proj['Own'] == 0), 1, Overall_Proj['Own'])
|
848 |
+
Overall_Proj = Overall_Proj.loc[Overall_Proj['Own'] > 0]
|
849 |
+
|
850 |
+
Overall_Proj['Floor'] = np.where(Overall_Proj['Position'] == 'QB', Overall_Proj['Median'] * .5, Overall_Proj['Median'] * .25)
|
851 |
+
Overall_Proj['Ceiling'] = np.where(Overall_Proj['Position'] == 'WR', Overall_Proj['Median'] + Overall_Proj['Median'], Overall_Proj['Median'] + Overall_Proj['Floor'])
|
852 |
+
Overall_Proj['STDev'] = Overall_Proj['Median'] / 4
|
853 |
+
|
854 |
+
Teams_used = Overall_Proj['Team'].drop_duplicates().reset_index(drop=True)
|
855 |
+
Teams_used = Teams_used.reset_index()
|
856 |
+
Teams_used['team_item'] = Teams_used['index'] + 1
|
857 |
+
Teams_used = Teams_used.drop(columns=['index'])
|
858 |
+
Teams_used_dictraw = Teams_used.drop(columns=['team_item'])
|
859 |
+
Teams_used_dict = Teams_used_dictraw.to_dict()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
860 |
|
861 |
+
del Teams_used_dictraw
|
862 |
+
|
863 |
+
team_list = Teams_used['Team'].to_list()
|
864 |
+
item_list = Teams_used['team_item'].to_list()
|
865 |
+
|
866 |
+
FieldStrength_raw = Strength_var + ((30 - len(Teams_used)) * .01)
|
867 |
+
FieldStrength = FieldStrength_raw - (FieldStrength_raw * (20000 / Contest_Size))
|
868 |
+
|
869 |
+
del FieldStrength_raw
|
870 |
+
|
871 |
+
if FieldStrength < 0:
|
872 |
+
FieldStrength = Strength_var
|
873 |
+
field_split = Strength_var
|
874 |
+
|
875 |
+
for checkVar in range(len(team_list)):
|
876 |
+
Overall_Proj['Team'] = Overall_Proj['Team'].replace(team_list, item_list)
|
877 |
+
|
878 |
+
flex_raw = Overall_Proj
|
879 |
+
flex_raw.dropna(subset=['Median']).reset_index(drop=True)
|
880 |
+
flex_raw = flex_raw.reset_index(drop=True)
|
881 |
+
flex_raw = flex_raw.sort_values(by='Own', ascending=False)
|
882 |
+
|
883 |
+
pos_players = flex_raw
|
884 |
+
pos_players.dropna(subset=['Median']).reset_index(drop=True)
|
885 |
+
pos_players = pos_players.reset_index(drop=True)
|
886 |
+
|
887 |
+
del flex_raw
|
888 |
+
|
889 |
if insert_port == 1:
|
890 |
+
try:
|
891 |
+
# Initialize an empty DataFrame to store raw portfolio data
|
892 |
+
Raw_Portfolio = pd.DataFrame()
|
893 |
+
|
894 |
+
# Split each portfolio column and concatenate to Raw_Portfolio
|
895 |
+
columns_to_process = ['CPT', 'FLEX1', 'FLEX2', 'FLEX3', 'FLEX4', 'FLEX5']
|
896 |
+
for col in columns_to_process:
|
897 |
+
temp_df = UserPortfolio[col].str.split("(", n=1, expand=True)
|
898 |
+
temp_df.columns = [col, 'Drop']
|
899 |
+
Raw_Portfolio = pd.concat([Raw_Portfolio, temp_df], axis=1)
|
900 |
+
|
901 |
+
# Keep only required variables and remove whitespace
|
902 |
+
keep_vars = columns_to_process
|
903 |
+
CleanPortfolio = Raw_Portfolio[keep_vars]
|
904 |
+
CleanPortfolio = CleanPortfolio.apply(lambda x: x.str.strip())
|
905 |
+
|
906 |
+
# Reset index and clean up the DataFrame
|
907 |
+
CleanPortfolio.reset_index(inplace=True)
|
908 |
+
CleanPortfolio['User/Field'] = CleanPortfolio['index'] + 1
|
909 |
+
CleanPortfolio.drop(columns=['index'], inplace=True)
|
910 |
+
CleanPortfolio.replace('', np.nan, inplace=True)
|
911 |
+
CleanPortfolio.dropna(subset=['CPT'], inplace=True)
|
912 |
+
|
913 |
+
# Create cleaport_players DataFrame
|
914 |
+
unique_vals, counts = np.unique(CleanPortfolio.iloc[:, 0:6].values, return_counts=True)
|
915 |
+
cleaport_players = pd.DataFrame(np.column_stack([unique_vals, counts]), columns=['Player', 'Freq']).astype({'Freq': int}).sort_values('Freq', ascending=False).reset_index(drop=True)
|
916 |
+
|
917 |
+
# Merge and update nerf_frame DataFrame
|
918 |
+
nerf_frame = pd.merge(cleaport_players, Overall_Proj, on='Player', how='left')
|
919 |
+
nerf_frame[['Median', 'Floor', 'Ceiling', 'STDev']] *= 1
|
920 |
+
|
921 |
+
del Raw_Portfolio
|
922 |
+
except:
|
923 |
+
# Reset index and perform column-wise operations
|
924 |
+
CleanPortfolio = UserPortfolio.reset_index(drop=True)
|
925 |
+
CleanPortfolio['User/Field'] = CleanPortfolio.index + 1
|
926 |
+
CleanPortfolio.replace('', np.nan, inplace=True)
|
927 |
+
CleanPortfolio.dropna(subset=['CPT'], inplace=True)
|
928 |
+
|
929 |
+
# Create cleaport_players DataFrame
|
930 |
+
unique_vals, counts = np.unique(CleanPortfolio.iloc[:, 0:6].values, return_counts=True)
|
931 |
+
cleaport_players = pd.DataFrame({'Player': unique_vals, 'Freq': counts}).sort_values('Freq', ascending=False).reset_index(drop=True).astype({'Freq': int})
|
932 |
+
|
933 |
+
# Merge and update nerf_frame DataFrame
|
934 |
+
nerf_frame = pd.merge(cleaport_players, Overall_Proj, on='Player', how='left')
|
935 |
+
nerf_frame[['Median', 'Floor', 'Ceiling', 'STDev']] *= 1
|
936 |
+
|
937 |
+
st.table(nerf_frame)
|
938 |
+
|
939 |
elif insert_port == 0:
|
940 |
+
CleanPortfolio = UserPortfolio
|
941 |
+
cleaport_players = pd.DataFrame(np.column_stack(np.unique(CleanPortfolio.iloc[:,0:6].values, return_counts=True)),
|
942 |
+
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
943 |
+
cleaport_players['Freq'] = cleaport_players['Freq'].astype(int)
|
944 |
+
nerf_frame = Overall_Proj
|
945 |
|
946 |
+
ref_dict = {
|
947 |
+
'pos':['FLEX'],
|
948 |
+
'pos_dfs':['FLEX_Table'],
|
949 |
+
'pos_dicts':['flex_dict']
|
950 |
+
}
|
|
|
|
|
951 |
|
952 |
+
maps_dict = {
|
953 |
+
'Floor_map':dict(zip(Overall_Proj.Player,Overall_Proj.Floor)),
|
954 |
+
'Projection_map':dict(zip(Overall_Proj.Player,Overall_Proj.Median)),
|
955 |
+
'Ceiling_map':dict(zip(Overall_Proj.Player,Overall_Proj.Ceiling)),
|
956 |
+
'Salary_map':dict(zip(Overall_Proj.Player,Overall_Proj.Salary)),
|
957 |
+
'Pos_map':dict(zip(Overall_Proj.Player,Overall_Proj.Position)),
|
958 |
+
'Own_map':dict(zip(Overall_Proj.Player,Overall_Proj.Own)),
|
959 |
+
'Team_map':dict(zip(Overall_Proj.Player,Overall_Proj.Team)),
|
960 |
+
'STDev_map':dict(zip(Overall_Proj.Player,Overall_Proj.STDev)),
|
961 |
+
'team_check_map':dict(zip(Overall_Proj.Player,Overall_Proj.Team))
|
962 |
+
}
|
963 |
+
|
964 |
+
up_dict = {
|
965 |
+
'Floor_map':dict(zip(cleaport_players.Player,nerf_frame.Floor)),
|
966 |
+
'Projection_map':dict(zip(cleaport_players.Player,nerf_frame.Median)),
|
967 |
+
'Ceiling_map':dict(zip(cleaport_players.Player,nerf_frame.Ceiling)),
|
968 |
+
'Salary_map':dict(zip(cleaport_players.Player,nerf_frame.Salary)),
|
969 |
+
'Pos_map':dict(zip(cleaport_players.Player,nerf_frame.Position)),
|
970 |
+
'Own_map':dict(zip(cleaport_players.Player,nerf_frame.Own)),
|
971 |
+
'Team_map':dict(zip(cleaport_players.Player,nerf_frame.Team)),
|
972 |
+
'STDev_map':dict(zip(cleaport_players.Player,nerf_frame.STDev)),
|
973 |
+
'team_check_map':dict(zip(cleaport_players.Player,nerf_frame.Team))
|
974 |
+
}
|
975 |
+
|
976 |
+
del Overall_Proj
|
977 |
+
del nerf_frame
|
978 |
+
|
979 |
+
RunsVar = 1
|
980 |
+
st.write('Seed frame creation')
|
981 |
+
FinalPortfolio, maps_dict = run_seed_frame(seed_depth1, Strength_var, strength_grow, Teams_used, Total_Runs)
|
982 |
+
|
983 |
+
Sim_size = linenum_var1
|
984 |
+
SimVar = 1
|
985 |
+
Sim_Winners = []
|
986 |
+
fp_array = FinalPortfolio.values
|
987 |
+
|
988 |
+
if insert_port == 1:
|
989 |
+
up_array = CleanPortfolio.values
|
990 |
+
|
991 |
+
# Pre-vectorize functions
|
992 |
+
vec_projection_map = np.vectorize(maps_dict['Projection_map'].__getitem__)
|
993 |
+
vec_stdev_map = np.vectorize(maps_dict['STDev_map'].__getitem__)
|
994 |
+
|
995 |
if insert_port == 1:
|
996 |
+
vec_up_projection_map = np.vectorize(up_dict['Projection_map'].__getitem__)
|
997 |
+
vec_up_stdev_map = np.vectorize(up_dict['STDev_map'].__getitem__)
|
998 |
+
st.write('Simulating contest on frames')
|
999 |
+
while SimVar <= Sim_size:
|
1000 |
+
|
1001 |
+
if insert_port == 1:
|
1002 |
+
fp_random = fp_array[np.random.choice(fp_array.shape[0], Contest_Size-len(CleanPortfolio))]
|
1003 |
+
elif insert_port == 0:
|
1004 |
+
fp_random = fp_array[np.random.choice(fp_array.shape[0], Contest_Size)]
|
1005 |
+
|
1006 |
+
sample_arrays1 = np.c_[
|
1007 |
+
fp_random,
|
1008 |
np.sum(np.random.normal(
|
1009 |
+
loc=vec_projection_map(fp_random[:, :-5]),
|
1010 |
+
scale=vec_stdev_map(fp_random[:, :-5])),
|
1011 |
axis=1)
|
1012 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1013 |
|
1014 |
+
if insert_port == 1:
|
1015 |
+
sample_arrays2 = np.c_[
|
1016 |
+
up_array,
|
1017 |
+
np.sum(np.random.normal(
|
1018 |
+
loc=vec_up_projection_map(up_array[:, :-5]),
|
1019 |
+
scale=vec_up_stdev_map(up_array[:, :-5])),
|
1020 |
+
axis=1)
|
1021 |
+
]
|
1022 |
+
sample_arrays = np.vstack((sample_arrays1, sample_arrays2))
|
1023 |
+
else:
|
1024 |
+
sample_arrays = sample_arrays1
|
|
|
|
|
1025 |
|
1026 |
+
final_array = sample_arrays[sample_arrays[:, 10].argsort()[::-1]]
|
1027 |
+
best_lineup = final_array[final_array[:, -1].argsort(kind='stable')[::-1][:1]]
|
1028 |
+
Sim_Winners.append(best_lineup)
|
1029 |
+
SimVar += 1
|
1030 |
+
st.write('Contest simulation complete')
|
1031 |
+
|
1032 |
+
Sim_Winner_Frame = pd.DataFrame(np.concatenate(Sim_Winners), columns=FinalPortfolio.columns.tolist() + ['Fantasy'])
|
1033 |
+
Sim_Winner_Frame['GPP_Proj'] = (Sim_Winner_Frame['Projection'] + Sim_Winner_Frame['Fantasy']) / 2
|
1034 |
+
Sim_Winner_Frame['Salary'] = Sim_Winner_Frame['Salary'].astype(int)
|
1035 |
+
Sim_Winner_Frame['Projection'] = Sim_Winner_Frame['Projection'].astype(np.float16)
|
1036 |
+
Sim_Winner_Frame['Fantasy'] = Sim_Winner_Frame['Fantasy'].astype(np.float16)
|
1037 |
+
Sim_Winner_Frame['GPP_Proj'] = Sim_Winner_Frame['GPP_Proj'].astype(np.float16)
|
1038 |
+
st.session_state.Sim_Winner_Frame = Sim_Winner_Frame.sort_values(by='GPP_Proj', ascending=False)
|
1039 |
+
st.session_state.Sim_Winner_Export = Sim_Winner_Frame.copy()
|
1040 |
+
|
1041 |
+
del Sim_Winner_Frame
|
1042 |
+
|
1043 |
+
player_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,0:6].values, return_counts=True)),
|
1044 |
+
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1045 |
+
player_freq['Freq'] = player_freq['Freq'].astype(int)
|
1046 |
+
player_freq['Position'] = player_freq['Player'].map(maps_dict['Pos_map'])
|
1047 |
+
player_freq['Salary'] = player_freq['Player'].map(maps_dict['Salary_map'])
|
1048 |
+
player_freq['Proj Own'] = (player_freq['Player'].map(maps_dict['Own_map']) / 100)
|
1049 |
+
player_freq['Exposure'] = player_freq['Freq']/(Sim_size)
|
1050 |
+
player_freq['Edge'] = player_freq['Exposure'] - player_freq['Proj Own']
|
1051 |
+
player_freq['Team'] = player_freq['Player'].map(maps_dict['Team_map'])
|
1052 |
+
for checkVar in range(len(team_list)):
|
1053 |
+
player_freq['Team'] = player_freq['Team'].replace(item_list, team_list)
|
1054 |
+
|
1055 |
+
st.session_state.player_freq = player_freq[['Player', 'Position', 'Team', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
1056 |
+
|
1057 |
+
cpt_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,0:1].values, return_counts=True)),
|
1058 |
+
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1059 |
+
cpt_freq['Freq'] = cpt_freq['Freq'].astype(int)
|
1060 |
+
cpt_freq['Position'] = cpt_freq['Player'].map(maps_dict['Pos_map'])
|
1061 |
+
cpt_freq['Salary'] = cpt_freq['Player'].map(maps_dict['Salary_map'])
|
1062 |
+
cpt_freq['Proj Own'] = (cpt_freq['Player'].map(maps_dict['Own_map']) / 4) / 100
|
1063 |
+
cpt_freq['Exposure'] = cpt_freq['Freq']/(Sim_size)
|
1064 |
+
cpt_freq['Edge'] = cpt_freq['Exposure'] - cpt_freq['Proj Own']
|
1065 |
+
cpt_freq['Team'] = cpt_freq['Player'].map(maps_dict['Team_map'])
|
1066 |
+
for checkVar in range(len(team_list)):
|
1067 |
+
cpt_freq['Team'] = cpt_freq['Team'].replace(item_list, team_list)
|
1068 |
+
|
1069 |
+
st.session_state.cpt_freq = cpt_freq[['Player', 'Position', 'Team', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
1070 |
+
|
1071 |
+
flex_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,[1, 2, 3, 4, 5]].values, return_counts=True)),
|
1072 |
+
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1073 |
+
flex_freq['Freq'] = flex_freq['Freq'].astype(int)
|
1074 |
+
flex_freq['Position'] = flex_freq['Player'].map(maps_dict['Pos_map'])
|
1075 |
+
flex_freq['Salary'] = flex_freq['Player'].map(maps_dict['Salary_map'])
|
1076 |
+
flex_freq['Proj Own'] = (flex_freq['Player'].map(maps_dict['Own_map']) / 100) - ((flex_freq['Player'].map(maps_dict['Own_map']) / 4) / 100)
|
1077 |
+
flex_freq['Exposure'] = flex_freq['Freq']/(Sim_size)
|
1078 |
+
flex_freq['Edge'] = flex_freq['Exposure'] - flex_freq['Proj Own']
|
1079 |
+
flex_freq['Team'] = flex_freq['Player'].map(maps_dict['Team_map'])
|
1080 |
+
for checkVar in range(len(team_list)):
|
1081 |
+
flex_freq['Team'] = flex_freq['Team'].replace(item_list, team_list)
|
1082 |
+
|
1083 |
+
st.session_state.flex_freq = flex_freq[['Player', 'Position', 'Team', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
1084 |
+
|
1085 |
+
del fp_random
|
1086 |
+
del sample_arrays
|
1087 |
+
del final_array
|
1088 |
+
del fp_array
|
1089 |
+
try:
|
1090 |
+
del up_array
|
1091 |
+
except:
|
1092 |
+
pass
|
1093 |
+
del best_lineup
|
1094 |
+
del CleanPortfolio
|
1095 |
+
del FinalPortfolio
|
1096 |
+
del maps_dict
|
1097 |
+
del team_list
|
1098 |
+
del item_list
|
1099 |
+
del Sim_size
|
1100 |
|
1101 |
+
with st.container():
|
1102 |
+
simulate_container = st.empty()
|
1103 |
+
if 'player_freq' in st.session_state:
|
1104 |
+
player_split_var2 = st.radio("Are you wanting to isolate any lineups with specific players?", ('Full Players', 'Specific Players'), key='player_split_var2')
|
1105 |
+
if player_split_var2 == 'Specific Players':
|
1106 |
+
find_var2 = st.multiselect('Which players must be included in the lineups?', options = st.session_state.player_freq['Player'].unique())
|
1107 |
+
elif player_split_var2 == 'Full Players':
|
1108 |
+
find_var2 = st.session_state.player_freq.Player.values.tolist()
|
1109 |
+
|
1110 |
+
if player_split_var2 == 'Specific Players':
|
1111 |
+
st.session_state.Sim_Winner_Display = st.session_state.Sim_Winner_Frame[np.equal.outer(st.session_state.Sim_Winner_Frame.to_numpy(copy=False), find_var2).any(axis=1).all(axis=1)]
|
1112 |
+
if player_split_var2 == 'Full Players':
|
1113 |
+
st.session_state.Sim_Winner_Display = st.session_state.Sim_Winner_Frame
|
1114 |
+
if 'Sim_Winner_Display' in st.session_state:
|
1115 |
+
st.dataframe(st.session_state.Sim_Winner_Display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').background_gradient(cmap='RdYlGn_r', subset=['Own']).format(precision=2), use_container_width = True)
|
1116 |
+
if 'Sim_Winner_Export' in st.session_state:
|
1117 |
+
st.download_button(
|
1118 |
+
label="Export Tables",
|
1119 |
+
data=convert_df_to_csv(st.session_state.Sim_Winner_Export),
|
1120 |
+
file_name='NFL_consim_export.csv',
|
1121 |
+
mime='text/csv',
|
1122 |
+
)
|
1123 |
+
|
1124 |
+
with st.container():
|
1125 |
+
tab1, tab2, tab3 = st.tabs(['Overall Exposures', 'CPT Exposures', 'FLEX Exposures'])
|
1126 |
+
with tab1:
|
1127 |
+
if 'player_freq' in st.session_state:
|
1128 |
+
st.dataframe(st.session_state.player_freq.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(freq_format, precision=2), use_container_width = True)
|
1129 |
st.download_button(
|
1130 |
label="Export Exposures",
|
1131 |
+
data=convert_df_to_csv(st.session_state.player_freq),
|
1132 |
file_name='player_freq_export.csv',
|
1133 |
mime='text/csv',
|
1134 |
)
|
1135 |
+
with tab2:
|
1136 |
+
if 'player_freq' in st.session_state:
|
1137 |
+
st.dataframe(st.session_state.cpt_freq.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(freq_format, precision=2), use_container_width = True)
|
1138 |
st.download_button(
|
1139 |
label="Export Exposures",
|
1140 |
+
data=convert_df_to_csv(st.session_state.cpt_freq),
|
1141 |
file_name='cpt_freq_export.csv',
|
1142 |
mime='text/csv',
|
1143 |
)
|
1144 |
+
with tab3:
|
1145 |
+
if 'player_freq' in st.session_state:
|
1146 |
+
st.dataframe(st.session_state.flex_freq.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(freq_format, precision=2), use_container_width = True)
|
1147 |
st.download_button(
|
1148 |
label="Export Exposures",
|
1149 |
+
data=convert_df_to_csv(st.session_state.flex_freq),
|
1150 |
file_name='flex_freq_export.csv',
|
1151 |
mime='text/csv',
|
1152 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|