Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -29,38 +29,37 @@ def init_conn():
|
|
29 |
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gspread-connection%40sheets-api-connect-378620.iam.gserviceaccount.com"
|
30 |
}
|
31 |
|
32 |
-
|
33 |
-
|
|
|
34 |
|
35 |
-
|
36 |
|
37 |
freq_format = {'Proj Own': '{:.2%}', 'Exposure': '{:.2%}', 'Edge': '{:.2%}'}
|
38 |
|
39 |
@st.cache_resource(ttl = 300)
|
40 |
def load_dk_player_projections():
|
41 |
-
sh =
|
42 |
worksheet = sh.worksheet('DK_ROO')
|
43 |
load_display = pd.DataFrame(worksheet.get_all_records())
|
44 |
load_display.replace('', np.nan, inplace=True)
|
45 |
raw_display = load_display.dropna(subset=['Median'])
|
46 |
-
del load_display
|
47 |
|
48 |
return raw_display
|
49 |
|
50 |
@st.cache_resource(ttl = 300)
|
51 |
def load_fd_player_projections():
|
52 |
-
sh =
|
53 |
worksheet = sh.worksheet('FD_ROO')
|
54 |
load_display = pd.DataFrame(worksheet.get_all_records())
|
55 |
load_display.replace('', np.nan, inplace=True)
|
56 |
raw_display = load_display.dropna(subset=['Median'])
|
57 |
-
del load_display
|
58 |
|
59 |
return raw_display
|
60 |
|
61 |
@st.cache_resource(ttl = 300)
|
62 |
def set_export_ids():
|
63 |
-
sh =
|
64 |
worksheet = sh.worksheet('DK_ROO')
|
65 |
load_display = pd.DataFrame(worksheet.get_all_records())
|
66 |
load_display.replace('', np.nan, inplace=True)
|
@@ -72,61 +71,104 @@ def set_export_ids():
|
|
72 |
load_display.replace('', np.nan, inplace=True)
|
73 |
raw_display = load_display.dropna(subset=['Median'])
|
74 |
fd_ids = dict(zip(raw_display['Player'], raw_display['player_id']))
|
75 |
-
|
76 |
-
del load_display
|
77 |
-
del raw_display
|
78 |
|
79 |
return dk_ids, fd_ids
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
84 |
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
RunsVar = 1
|
87 |
seed_depth_def = seed_depth1
|
88 |
Strength_var_def = Strength_var
|
89 |
strength_grow_def = strength_grow
|
90 |
Teams_used_def = Teams_used
|
91 |
Total_Runs_def = Total_Runs
|
|
|
92 |
while RunsVar <= seed_depth_def:
|
93 |
if RunsVar <= 3:
|
94 |
FieldStrength = Strength_var_def
|
95 |
-
FinalPortfolio, maps_dict = get_correlated_portfolio_for_sim(Total_Runs_def * .
|
96 |
-
FinalPortfolio2, maps_dict2 = get_uncorrelated_portfolio_for_sim(Total_Runs_def * .
|
97 |
-
|
98 |
maps_dict.update(maps_dict2)
|
99 |
-
del FinalPortfolio2
|
100 |
-
del maps_dict2
|
101 |
elif RunsVar > 3 and RunsVar <= 4:
|
102 |
FieldStrength += (strength_grow_def + ((30 - len(Teams_used_def)) * .001))
|
103 |
-
FinalPortfolio3, maps_dict3 = get_correlated_portfolio_for_sim(Total_Runs_def * .
|
104 |
-
FinalPortfolio4, maps_dict4 = get_uncorrelated_portfolio_for_sim(Total_Runs_def * .
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
maps_dict.update(maps_dict3)
|
109 |
maps_dict.update(maps_dict4)
|
110 |
-
del FinalPortfolio3
|
111 |
-
del maps_dict3
|
112 |
-
del FinalPortfolio4
|
113 |
-
del maps_dict4
|
114 |
elif RunsVar > 4:
|
115 |
FieldStrength = 1
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
maps_dict.update(
|
122 |
-
maps_dict.update(
|
123 |
-
del FinalPortfolio3
|
124 |
-
del maps_dict3
|
125 |
-
del FinalPortfolio4
|
126 |
-
del maps_dict4
|
127 |
RunsVar += 1
|
128 |
-
|
129 |
-
return
|
130 |
|
131 |
def create_stack_options(player_data, wr_var):
|
132 |
merged_frame = pd.DataFrame(columns = ['QB', 'Player'])
|
@@ -142,9 +184,6 @@ def create_stack_options(player_data, wr_var):
|
|
142 |
merged_frame = merged_frame.reset_index()
|
143 |
correl_dict = dict(zip(merged_frame.QB, merged_frame.Player))
|
144 |
|
145 |
-
del merged_frame
|
146 |
-
del data_raw
|
147 |
-
|
148 |
return correl_dict
|
149 |
|
150 |
def create_overall_dfs(pos_players, table_name, dict_name, pos):
|
@@ -154,17 +193,11 @@ def create_overall_dfs(pos_players, table_name, dict_name, pos):
|
|
154 |
overall_table_name = table_name_raw.head(round(len(table_name_raw)))
|
155 |
overall_table_name = overall_table_name.assign(Var = range(0,len(overall_table_name)))
|
156 |
overall_dict_name = pd.Series(overall_table_name.Player.values, index=overall_table_name.Var).to_dict()
|
157 |
-
|
158 |
-
del pos_players
|
159 |
-
del table_name_raw
|
160 |
elif pos != "FLEX":
|
161 |
table_name_raw = pos_players[pos_players['Position'].str.contains(pos)].reset_index(drop=True)
|
162 |
overall_table_name = table_name_raw.head(round(len(table_name_raw)))
|
163 |
overall_table_name = overall_table_name.assign(Var = range(0,len(overall_table_name)))
|
164 |
overall_dict_name = pd.Series(overall_table_name.Player.values, index=overall_table_name.Var).to_dict()
|
165 |
-
|
166 |
-
del pos_players
|
167 |
-
del table_name_raw
|
168 |
|
169 |
return overall_table_name, overall_dict_name
|
170 |
|
@@ -182,17 +215,20 @@ def get_overall_merged_df():
|
|
182 |
|
183 |
df_out = pd.concat(ref_dict['pos_dfs'], ignore_index=True)
|
184 |
|
185 |
-
return
|
186 |
|
187 |
def calculate_range_var(count, min_val, FieldStrength, field_growth):
|
188 |
var = round(len(count[0]) * FieldStrength)
|
189 |
var = max(var, min_val)
|
190 |
var += round(field_growth)
|
|
|
191 |
return min(var, len(count[0]))
|
192 |
|
193 |
-
def create_random_portfolio(Total_Sample_Size, raw_baselines):
|
|
|
|
|
194 |
|
195 |
-
|
196 |
max_var = len(raw_baselines[raw_baselines['Position'] == 'QB'])
|
197 |
|
198 |
field_growth_rounded = round(field_growth)
|
@@ -211,9 +247,6 @@ def create_random_portfolio(Total_Sample_Size, raw_baselines):
|
|
211 |
elif max_var > 16:
|
212 |
ranges_dict['qb_range'] = round(max_var / 2)
|
213 |
ranges_dict['dst_range'] = round(max_var)
|
214 |
-
# Generate unique ranges
|
215 |
-
# for key, value in ranges_dict.items():
|
216 |
-
# ranges_dict[f"{key}_Uniques"] = list(range(0, value, 1))
|
217 |
|
218 |
# Generate random portfolios
|
219 |
rng = np.random.default_rng()
|
@@ -223,19 +256,14 @@ def create_random_portfolio(Total_Sample_Size, raw_baselines):
|
|
223 |
all_choices = [rng.choice(ranges_dict[f"{key}_range"], size=(Total_Sample_Size, elem)) for key, elem in zip(keys, total_elements)]
|
224 |
RandomPortfolio = pd.DataFrame(np.hstack(all_choices), columns=['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST'])
|
225 |
RandomPortfolio['User/Field'] = 0
|
226 |
-
|
227 |
-
del rng
|
228 |
-
del total_elements
|
229 |
-
del all_choices
|
230 |
-
del O_merge
|
231 |
|
232 |
return RandomPortfolio, maps_dict, ranges_dict, full_pos_player_dict
|
233 |
|
234 |
-
def get_correlated_portfolio_for_sim(Total_Sample_Size, sharp_split):
|
235 |
|
236 |
sizesplit = round(Total_Sample_Size * sharp_split)
|
237 |
|
238 |
-
RandomPortfolio, maps_dict, ranges_dict, full_pos_player_dict = create_random_portfolio(sizesplit, raw_baselines)
|
239 |
stack_num = random.randint(1, 3)
|
240 |
stacking_dict = create_stack_options(raw_baselines, stack_num)
|
241 |
|
@@ -253,12 +281,6 @@ def get_correlated_portfolio_for_sim(Total_Sample_Size, sharp_split):
|
|
253 |
RandomPortfolio = RandomPortfolio[RandomPortfolio['plyr_count'] == 10].drop(columns=['plyr_list','plyr_count']).\
|
254 |
reset_index(drop=True)
|
255 |
|
256 |
-
del sizesplit
|
257 |
-
del full_pos_player_dict
|
258 |
-
del ranges_dict
|
259 |
-
del stack_num
|
260 |
-
del stacking_dict
|
261 |
-
|
262 |
RandomPortfolio['QBs'] = RandomPortfolio['QB'].map(maps_dict['Salary_map']).astype(np.int32)
|
263 |
RandomPortfolio['RB1s'] = RandomPortfolio['RB1'].map(maps_dict['Salary_map']).astype(np.int32)
|
264 |
RandomPortfolio['RB2s'] = RandomPortfolio['RB2'].map(maps_dict['Salary_map']).astype(np.int32)
|
@@ -290,7 +312,6 @@ def get_correlated_portfolio_for_sim(Total_Sample_Size, sharp_split):
|
|
290 |
RandomPortfolio['DSTo'] = RandomPortfolio['DST'].map(maps_dict['Own_map']).astype(np.float16)
|
291 |
|
292 |
RandomPortArray = RandomPortfolio.to_numpy()
|
293 |
-
del RandomPortfolio
|
294 |
|
295 |
RandomPortArray = np.c_[RandomPortArray, np.einsum('ij->i',RandomPortArray[:,10:19].astype(int))]
|
296 |
RandomPortArray = np.c_[RandomPortArray, np.einsum('ij->i',RandomPortArray[:,19:28].astype(np.double))]
|
@@ -299,8 +320,6 @@ def get_correlated_portfolio_for_sim(Total_Sample_Size, sharp_split):
|
|
299 |
RandomPortArrayOut = np.delete(RandomPortArray, np.s_[10:37], axis=1)
|
300 |
RandomPortfolioDF = pd.DataFrame(RandomPortArrayOut, columns = ['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST', 'User/Field', 'Salary', 'Projection', 'Own'])
|
301 |
RandomPortfolioDF = RandomPortfolioDF.sort_values(by=Sim_function, ascending=False)
|
302 |
-
del RandomPortArray
|
303 |
-
del RandomPortArrayOut
|
304 |
|
305 |
if insert_port == 1:
|
306 |
CleanPortfolio['Salary'] = sum([CleanPortfolio['QB'].map(maps_dict['Salary_map']),
|
@@ -345,17 +364,15 @@ def get_correlated_portfolio_for_sim(Total_Sample_Size, sharp_split):
|
|
345 |
|
346 |
RandomPortfolio = RandomPortfolioDF.sort_values(by=Sim_function, ascending=False)
|
347 |
|
348 |
-
del RandomPortfolioDF
|
349 |
-
|
350 |
RandomPortfolio = RandomPortfolio[['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST', 'User/Field', 'Salary', 'Projection', 'Own']]
|
351 |
|
352 |
return RandomPortfolio, maps_dict
|
353 |
|
354 |
-
def get_uncorrelated_portfolio_for_sim(Total_Sample_Size, sharp_split):
|
355 |
|
356 |
sizesplit = round(Total_Sample_Size * (1-sharp_split))
|
357 |
|
358 |
-
RandomPortfolio, maps_dict, ranges_dict, full_pos_player_dict = create_random_portfolio(sizesplit, raw_baselines)
|
359 |
|
360 |
RandomPortfolio['QB'] = pd.Series(list(RandomPortfolio['QB'].map(qb_dict)), dtype="string[pyarrow]")
|
361 |
RandomPortfolio['RB1'] = pd.Series(list(RandomPortfolio['RB1'].map(full_pos_player_dict['pos_dicts'][0])), dtype="string[pyarrow]")
|
@@ -371,10 +388,6 @@ def get_uncorrelated_portfolio_for_sim(Total_Sample_Size, sharp_split):
|
|
371 |
RandomPortfolio = RandomPortfolio[RandomPortfolio['plyr_count'] == 10].drop(columns=['plyr_list','plyr_count']).\
|
372 |
reset_index(drop=True)
|
373 |
|
374 |
-
del sizesplit
|
375 |
-
del full_pos_player_dict
|
376 |
-
del ranges_dict
|
377 |
-
|
378 |
RandomPortfolio['QBs'] = RandomPortfolio['QB'].map(maps_dict['Salary_map']).astype(np.int32)
|
379 |
RandomPortfolio['RB1s'] = RandomPortfolio['RB1'].map(maps_dict['Salary_map']).astype(np.int32)
|
380 |
RandomPortfolio['RB2s'] = RandomPortfolio['RB2'].map(maps_dict['Salary_map']).astype(np.int32)
|
@@ -406,7 +419,6 @@ def get_uncorrelated_portfolio_for_sim(Total_Sample_Size, sharp_split):
|
|
406 |
RandomPortfolio['DSTo'] = RandomPortfolio['DST'].map(maps_dict['Own_map']).astype(np.float16)
|
407 |
|
408 |
RandomPortArray = RandomPortfolio.to_numpy()
|
409 |
-
del RandomPortfolio
|
410 |
|
411 |
RandomPortArray = np.c_[RandomPortArray, np.einsum('ij->i',RandomPortArray[:,10:19].astype(int))]
|
412 |
RandomPortArray = np.c_[RandomPortArray, np.einsum('ij->i',RandomPortArray[:,19:28].astype(np.double))]
|
@@ -415,9 +427,6 @@ def get_uncorrelated_portfolio_for_sim(Total_Sample_Size, sharp_split):
|
|
415 |
RandomPortArrayOut = np.delete(RandomPortArray, np.s_[10:37], axis=1)
|
416 |
RandomPortfolioDF = pd.DataFrame(RandomPortArrayOut, columns = ['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST', 'User/Field', 'Salary', 'Projection', 'Own'])
|
417 |
RandomPortfolioDF = RandomPortfolioDF.sort_values(by=Sim_function, ascending=False)
|
418 |
-
del RandomPortArray
|
419 |
-
del RandomPortArrayOut
|
420 |
-
# st.table(RandomPortfolioDF.head(50))
|
421 |
|
422 |
if insert_port == 1:
|
423 |
CleanPortfolio['Salary'] = sum([CleanPortfolio['QB'].map(maps_dict['Salary_map']),
|
@@ -464,19 +473,8 @@ def get_uncorrelated_portfolio_for_sim(Total_Sample_Size, sharp_split):
|
|
464 |
|
465 |
RandomPortfolio = RandomPortfolio[['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST', 'User/Field', 'Salary', 'Projection', 'Own']]
|
466 |
|
467 |
-
del RandomPortfolioDF
|
468 |
-
|
469 |
return RandomPortfolio, maps_dict
|
470 |
|
471 |
-
|
472 |
-
dk_roo_raw = load_dk_player_projections()
|
473 |
-
fd_roo_raw = load_fd_player_projections()
|
474 |
-
t_stamp = f"Last Update: " + str(dk_roo_raw['timestamp'][0]) + f" CST"
|
475 |
-
dkid_dict, fdid_dict = set_export_ids()
|
476 |
-
|
477 |
-
static_exposure = pd.DataFrame(columns=['Player', 'count'])
|
478 |
-
overall_exposure = pd.DataFrame(columns=['Player', 'count'])
|
479 |
-
|
480 |
tab1, tab2 = st.tabs(['Uploads', 'Contest Sim'])
|
481 |
|
482 |
with tab1:
|
@@ -509,7 +507,6 @@ with tab1:
|
|
509 |
player_salary_dict = dict(zip(proj_dataframe.Player, proj_dataframe.Salary))
|
510 |
player_proj_dict = dict(zip(proj_dataframe.Player, proj_dataframe.Median))
|
511 |
player_own_dict = dict(zip(proj_dataframe.Player, proj_dataframe.Own))
|
512 |
-
player_team_dict = dict(zip(proj_dataframe.Player, proj_dataframe.Team))
|
513 |
|
514 |
with col2:
|
515 |
st.info("The Portfolio file must contain only columns in order and explicitly named: 'QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', and 'DST'. Upload your projections first to avoid an error message.")
|
@@ -577,23 +574,6 @@ with tab1:
|
|
577 |
split_portfolio['TE'].map(player_own_dict),
|
578 |
split_portfolio['FLEX'].map(player_own_dict),
|
579 |
split_portfolio['DST'].map(player_own_dict)])
|
580 |
-
|
581 |
-
split_portfolio['QB_team'] = split_portfolio['QB'].map(player_team_dict)
|
582 |
-
split_portfolio['RB1_team'] = split_portfolio['RB1'].map(player_team_dict)
|
583 |
-
split_portfolio['RB2_team'] = split_portfolio['RB2'].map(player_team_dict)
|
584 |
-
split_portfolio['WR1_team'] = split_portfolio['WR1'].map(player_team_dict)
|
585 |
-
split_portfolio['WR2_team'] = split_portfolio['WR2'].map(player_team_dict)
|
586 |
-
split_portfolio['WR3_team'] = split_portfolio['WR3'].map(player_team_dict)
|
587 |
-
split_portfolio['TE_team'] = split_portfolio['TE'].map(player_team_dict)
|
588 |
-
split_portfolio['FLEX_team'] = split_portfolio['FLEX'].map(player_team_dict)
|
589 |
-
split_portfolio['DST_team'] = split_portfolio['DST'].map(player_team_dict)
|
590 |
-
|
591 |
-
split_portfolio = split_portfolio[['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST', 'Salary', 'Projection', 'Ownership', 'QB_team',
|
592 |
-
'RB1_team', 'RB2_team', 'WR1_team', 'WR2_team', 'WR3_team', 'TE_team', 'FLEX_team', 'DST_team']]
|
593 |
-
|
594 |
-
split_portfolio['Main_Stack'] = split_portfolio.iloc[:, 15:19].apply(lambda row: row.value_counts().nlargest(2).index[0],axis=1)
|
595 |
-
split_portfolio['Main_Stack_Size'] = split_portfolio.iloc[:, 15:19].apply(lambda row: row.value_counts().nlargest(2).values[0],axis=1)
|
596 |
-
split_portfolio['Main_Stack_Size'] = split_portfolio['Main_Stack_Size'] - 1
|
597 |
|
598 |
|
599 |
except:
|
@@ -650,23 +630,6 @@ with tab1:
|
|
650 |
split_portfolio['TE'].map(player_own_dict),
|
651 |
split_portfolio['FLEX'].map(player_own_dict),
|
652 |
split_portfolio['DST'].map(player_own_dict)])
|
653 |
-
|
654 |
-
split_portfolio['QB_team'] = split_portfolio['QB'].map(player_team_dict)
|
655 |
-
split_portfolio['RB1_team'] = split_portfolio['RB1'].map(player_team_dict)
|
656 |
-
split_portfolio['RB2_team'] = split_portfolio['RB2'].map(player_team_dict)
|
657 |
-
split_portfolio['WR1_team'] = split_portfolio['WR1'].map(player_team_dict)
|
658 |
-
split_portfolio['WR2_team'] = split_portfolio['WR2'].map(player_team_dict)
|
659 |
-
split_portfolio['WR3_team'] = split_portfolio['WR3'].map(player_team_dict)
|
660 |
-
split_portfolio['TE_team'] = split_portfolio['TE'].map(player_team_dict)
|
661 |
-
split_portfolio['FLEX_team'] = split_portfolio['FLEX'].map(player_team_dict)
|
662 |
-
split_portfolio['DST_team'] = split_portfolio['DST'].map(player_team_dict)
|
663 |
-
|
664 |
-
split_portfolio = split_portfolio[['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST', 'Salary', 'Projection', 'Ownership', 'QB_team',
|
665 |
-
'RB1_team', 'RB2_team', 'WR1_team', 'WR2_team', 'WR3_team', 'TE_team', 'FLEX_team', 'DST_team']]
|
666 |
-
|
667 |
-
split_portfolio['Main_Stack'] = split_portfolio.iloc[:, 15:19].apply(lambda row: row.value_counts().nlargest(2).index[0],axis=1)
|
668 |
-
split_portfolio['Main_Stack_Size'] = split_portfolio.iloc[:, 15:19].apply(lambda row: row.value_counts().nlargest(2).values[0],axis=1)
|
669 |
-
split_portfolio['Main_Stack_Size'] = split_portfolio['Main_Stack_Size'] - 1
|
670 |
|
671 |
except:
|
672 |
split_portfolio = portfolio_dataframe
|
@@ -700,97 +663,9 @@ with tab1:
|
|
700 |
split_portfolio['TE'].map(player_own_dict),
|
701 |
split_portfolio['FLEX'].map(player_own_dict),
|
702 |
split_portfolio['DST'].map(player_own_dict)])
|
703 |
-
|
704 |
-
|
705 |
-
split_portfolio['RB1_team'] = split_portfolio['RB1'].map(player_team_dict)
|
706 |
-
split_portfolio['RB2_team'] = split_portfolio['RB2'].map(player_team_dict)
|
707 |
-
split_portfolio['WR1_team'] = split_portfolio['WR1'].map(player_team_dict)
|
708 |
-
split_portfolio['WR2_team'] = split_portfolio['WR2'].map(player_team_dict)
|
709 |
-
split_portfolio['WR3_team'] = split_portfolio['WR3'].map(player_team_dict)
|
710 |
-
split_portfolio['TE_team'] = split_portfolio['TE'].map(player_team_dict)
|
711 |
-
split_portfolio['FLEX_team'] = split_portfolio['FLEX'].map(player_team_dict)
|
712 |
-
split_portfolio['DST_team'] = split_portfolio['DST'].map(player_team_dict)
|
713 |
-
|
714 |
-
split_portfolio = split_portfolio[['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST', 'Salary', 'Projection', 'Ownership', 'QB_team',
|
715 |
-
'RB1_team', 'RB2_team', 'WR1_team', 'WR2_team', 'WR3_team', 'TE_team', 'FLEX_team', 'DST_team']]
|
716 |
-
|
717 |
-
split_portfolio['Main_Stack'] = split_portfolio.iloc[:, 15:19].apply(lambda row: row.value_counts().nlargest(2).index[0],axis=1)
|
718 |
-
split_portfolio['Main_Stack_Size'] = split_portfolio.iloc[:, 15:19].apply(lambda row: row.value_counts().nlargest(2).values[0],axis=1)
|
719 |
-
split_portfolio['Main_Stack_Size'] = split_portfolio['Main_Stack_Size'] - 1
|
720 |
-
|
721 |
-
for player_cols in split_portfolio.iloc[:, :9]:
|
722 |
-
static_col_raw = split_portfolio[player_cols].value_counts()
|
723 |
-
static_col = static_col_raw.to_frame()
|
724 |
-
static_col.reset_index(inplace=True)
|
725 |
-
static_col.columns = ['Player', 'count']
|
726 |
-
static_exposure = pd.concat([static_exposure, static_col], ignore_index=True)
|
727 |
-
static_exposure['Exposure'] = static_exposure['count'] / len(split_portfolio)
|
728 |
-
static_exposure = static_exposure[['Player', 'Exposure']]
|
729 |
|
730 |
-
del player_salary_dict
|
731 |
-
del player_proj_dict
|
732 |
-
del player_own_dict
|
733 |
-
del player_team_dict
|
734 |
-
del static_col_raw
|
735 |
-
del static_col
|
736 |
-
with st.container():
|
737 |
-
col1, col2 = st.columns([3, 3])
|
738 |
-
|
739 |
-
if portfolio_file is not None:
|
740 |
-
with col1:
|
741 |
-
team_split_var1 = st.radio("Are you wanting to isolate any lineups with specific main stacks?", ('Full Portfolio', 'Specific Stacks'))
|
742 |
-
if team_split_var1 == 'Specific Stacks':
|
743 |
-
team_var1 = st.multiselect('Which main stacks would you like to include in the Portfolio?', options = split_portfolio['Main_Stack'].unique())
|
744 |
-
elif team_split_var1 == 'Full Portfolio':
|
745 |
-
team_var1 = split_portfolio.Main_Stack.values.tolist()
|
746 |
-
with col2:
|
747 |
-
player_split_var1 = st.radio("Are you wanting to isolate any lineups with specific players?", ('Full Players', 'Specific Players'))
|
748 |
-
if player_split_var1 == 'Specific Players':
|
749 |
-
find_var1 = st.multiselect('Which players must be included in the lineups?', options = static_exposure['Player'].unique())
|
750 |
-
elif player_split_var1 == 'Full Players':
|
751 |
-
find_var1 = static_exposure.Player.values.tolist()
|
752 |
-
|
753 |
-
split_portfolio = split_portfolio[split_portfolio['Main_Stack'].isin(team_var1)]
|
754 |
-
if player_split_var1 == 'Specific Players':
|
755 |
-
split_portfolio = split_portfolio[np.equal.outer(split_portfolio.to_numpy(copy=False), find_var1).any(axis=1).all(axis=1)]
|
756 |
-
elif player_split_var1 == 'Full Players':
|
757 |
-
split_portfolio = split_portfolio
|
758 |
-
|
759 |
-
for player_cols in split_portfolio.iloc[:, :9]:
|
760 |
-
exposure_col_raw = split_portfolio[player_cols].value_counts()
|
761 |
-
exposure_col = exposure_col_raw.to_frame()
|
762 |
-
exposure_col.reset_index(inplace=True)
|
763 |
-
exposure_col.columns = ['Player', 'count']
|
764 |
-
overall_exposure = pd.concat([overall_exposure, exposure_col], ignore_index=True)
|
765 |
-
overall_exposure['Exposure'] = overall_exposure['count'] / len(split_portfolio)
|
766 |
-
overall_exposure = overall_exposure.groupby('Player').sum()
|
767 |
-
overall_exposure.reset_index(inplace=True)
|
768 |
-
overall_exposure = overall_exposure[['Player', 'Exposure']]
|
769 |
-
overall_exposure = overall_exposure.set_index('Player')
|
770 |
-
overall_exposure = overall_exposure.sort_values(by='Exposure', ascending=False)
|
771 |
-
overall_exposure['Exposure'] = overall_exposure['Exposure'].astype(float).map(lambda n: '{:.2%}'.format(n))
|
772 |
-
|
773 |
-
del static_exposure
|
774 |
-
|
775 |
-
with st.container():
|
776 |
-
col1, col2 = st.columns([1, 6])
|
777 |
-
|
778 |
-
with col1:
|
779 |
-
if portfolio_file is not None:
|
780 |
-
st.header('Exposure View')
|
781 |
-
st.dataframe(overall_exposure)
|
782 |
-
|
783 |
-
with col2:
|
784 |
-
if portfolio_file is not None:
|
785 |
-
st.header('Portfolio View')
|
786 |
-
split_portfolio = split_portfolio.reset_index()
|
787 |
-
split_portfolio['Lineup'] = split_portfolio['index'] + 1
|
788 |
-
display_portfolio = split_portfolio[['Lineup', 'QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST', 'Salary', 'Main_Stack', 'Main_Stack_Size', 'Projection', 'Ownership']]
|
789 |
-
display_portfolio = display_portfolio.set_index('Lineup')
|
790 |
-
st.dataframe(display_portfolio.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').background_gradient(cmap='RdYlGn_r', subset=['Ownership']).format(precision=2))
|
791 |
-
del split_portfolio
|
792 |
-
del exposure_col_raw
|
793 |
-
del exposure_col
|
794 |
with tab2:
|
795 |
col1, col2 = st.columns([1, 7])
|
796 |
with col1:
|
@@ -818,8 +693,7 @@ with tab2:
|
|
818 |
elif slate_var1 != 'User':
|
819 |
raw_baselines = fd_roo_raw[fd_roo_raw['slate'] == str(slate_var1)]
|
820 |
raw_baselines = raw_baselines[raw_baselines['version'] == 'overall']
|
821 |
-
|
822 |
-
del fd_roo_raw
|
823 |
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")
|
824 |
insert_port1 = st.selectbox("Are you uploading a portfolio?", ('No', 'Yes'), key='insert_port1')
|
825 |
if insert_port1 == 'Yes':
|
@@ -833,7 +707,6 @@ with tab2:
|
|
833 |
Contest_Size = 5000
|
834 |
elif contest_var1 == 'Large':
|
835 |
Contest_Size = 10000
|
836 |
-
linenum_var1 = 2500
|
837 |
strength_var1 = st.selectbox("How sharp is the field in the contest?", ('Not Very', 'Average', 'Very'))
|
838 |
if strength_var1 == 'Not Very':
|
839 |
sharp_split = .33
|
@@ -847,81 +720,78 @@ with tab2:
|
|
847 |
sharp_split = .75
|
848 |
Strength_var = .01
|
849 |
scaling_var = 15
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
850 |
|
851 |
with col2:
|
852 |
with st.container():
|
853 |
if st.button("Simulate Contest"):
|
854 |
with st.container():
|
855 |
-
st.write('Contest Simulation Starting')
|
856 |
for key in st.session_state.keys():
|
857 |
del st.session_state[key]
|
858 |
-
seed_depth1 = 10
|
859 |
-
Total_Runs = 1000000
|
860 |
-
if Contest_Size <= 1000:
|
861 |
-
strength_grow = .01
|
862 |
-
elif Contest_Size > 1000 and Contest_Size <= 2500:
|
863 |
-
strength_grow = .025
|
864 |
-
elif Contest_Size > 2500 and Contest_Size <= 5000:
|
865 |
-
strength_grow = .05
|
866 |
-
elif Contest_Size > 5000 and Contest_Size <= 20000:
|
867 |
-
strength_grow = .075
|
868 |
-
elif Contest_Size > 20000:
|
869 |
-
strength_grow = .1
|
870 |
-
|
871 |
-
field_growth = 100 * strength_grow
|
872 |
-
|
873 |
-
Sort_function = 'Median'
|
874 |
-
if Sort_function == 'Median':
|
875 |
-
Sim_function = 'Projection'
|
876 |
-
elif Sort_function == 'Own':
|
877 |
-
Sim_function = 'Own'
|
878 |
|
879 |
if slate_var1 == 'User':
|
880 |
-
|
881 |
-
|
882 |
-
|
883 |
-
|
884 |
-
|
885 |
-
|
886 |
-
|
887 |
-
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'])
|
888 |
-
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%'])
|
889 |
-
OwnFrame['Own%'] = np.where(OwnFrame['Own%'] > 75, 75, OwnFrame['Own%'])
|
890 |
-
OwnFrame['Own'] = OwnFrame['Own%'] * (900 / OwnFrame['Own%'].sum())
|
891 |
-
if contest_var1 == 'Large':
|
892 |
-
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'])
|
893 |
-
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%'])
|
894 |
-
OwnFrame['Own%'] = np.where(OwnFrame['Own%'] > 75, 75, OwnFrame['Own%'])
|
895 |
-
OwnFrame['Own'] = OwnFrame['Own%'] * (900 / OwnFrame['Own%'].sum())
|
896 |
-
Overall_Proj = OwnFrame[['Player', 'Team', 'Position', 'Median', 'Own', 'Salary']]
|
897 |
|
898 |
-
|
899 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
900 |
|
901 |
elif slate_var1 != 'User':
|
902 |
-
|
903 |
-
|
904 |
-
|
905 |
-
|
906 |
-
|
907 |
-
|
908 |
-
|
909 |
-
|
910 |
-
if contest_var1 == 'Medium':
|
911 |
-
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'])
|
912 |
-
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%'])
|
913 |
-
OwnFrame['Own%'] = np.where(OwnFrame['Own%'] > 75, 75, OwnFrame['Own%'])
|
914 |
-
OwnFrame['Own'] = OwnFrame['Own%'] * (900 / OwnFrame['Own%'].sum())
|
915 |
-
if contest_var1 == 'Large':
|
916 |
-
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'])
|
917 |
-
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%'])
|
918 |
-
OwnFrame['Own%'] = np.where(OwnFrame['Own%'] > 75, 75, OwnFrame['Own%'])
|
919 |
-
OwnFrame['Own'] = OwnFrame['Own%'] * (900 / OwnFrame['Own%'].sum())
|
920 |
-
Overall_Proj = OwnFrame[['Player', 'Team', 'Position', 'Median', 'Own', 'Salary']]
|
921 |
|
922 |
-
|
923 |
-
|
924 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
925 |
|
926 |
if insert_port == 1:
|
927 |
UserPortfolio = portfolio_dataframe[['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST']]
|
@@ -945,9 +815,6 @@ with tab2:
|
|
945 |
Teams_used['team_item'] = Teams_used['index'] + 1
|
946 |
Teams_used = Teams_used.drop(columns=['index'])
|
947 |
Teams_used_dictraw = Teams_used.drop(columns=['team_item'])
|
948 |
-
# Teams_used_dict = Teams_used_dictraw.to_dict()
|
949 |
-
|
950 |
-
del Teams_used_dictraw
|
951 |
|
952 |
team_list = Teams_used['Team'].to_list()
|
953 |
item_list = Teams_used['team_item'].to_list()
|
@@ -955,8 +822,6 @@ with tab2:
|
|
955 |
FieldStrength_raw = Strength_var + ((30 - len(Teams_used)) * .01)
|
956 |
FieldStrength = FieldStrength_raw - (FieldStrength_raw * (20000 / Contest_Size))
|
957 |
|
958 |
-
del FieldStrength_raw
|
959 |
-
|
960 |
if FieldStrength < 0:
|
961 |
FieldStrength = Strength_var
|
962 |
field_split = Strength_var
|
@@ -1000,12 +865,6 @@ with tab2:
|
|
1000 |
pos_players = pd.concat([rbs_raw, wrs_raw, tes_raw])
|
1001 |
pos_players.dropna(subset=['Median']).reset_index(drop=True)
|
1002 |
pos_players = pos_players.reset_index(drop=True)
|
1003 |
-
|
1004 |
-
del qbs_raw
|
1005 |
-
del defs_raw
|
1006 |
-
del rbs_raw
|
1007 |
-
del wrs_raw
|
1008 |
-
del tes_raw
|
1009 |
|
1010 |
if insert_port == 1:
|
1011 |
try:
|
@@ -1025,8 +884,6 @@ with tab2:
|
|
1025 |
CleanPortfolio['User/Field'] = CleanPortfolio['index'] + 1
|
1026 |
CleanPortfolio.drop(columns=['index'], inplace=True)
|
1027 |
|
1028 |
-
del positions
|
1029 |
-
|
1030 |
CleanPortfolio.replace('', np.nan, inplace=True)
|
1031 |
CleanPortfolio.dropna(subset=['QB'], inplace=True)
|
1032 |
|
@@ -1041,7 +898,6 @@ with tab2:
|
|
1041 |
nerf_frame = pd.merge(cleaport_players, Overall_Proj, on='Player', how='left')
|
1042 |
for col in ['Median', 'Floor', 'Ceiling', 'STDev']:
|
1043 |
nerf_frame[col] *= 0.90
|
1044 |
-
del Raw_Portfolio
|
1045 |
except:
|
1046 |
CleanPortfolio = UserPortfolio.reset_index()
|
1047 |
CleanPortfolio['User/Field'] = CleanPortfolio['index'] + 1
|
@@ -1069,7 +925,7 @@ with tab2:
|
|
1069 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1070 |
cleaport_players['Freq'] = cleaport_players['Freq'].astype(int)
|
1071 |
nerf_frame = Overall_Proj
|
1072 |
-
|
1073 |
ref_dict = {
|
1074 |
'pos':['RB', 'WR', 'TE', 'FLEX'],
|
1075 |
'pos_dfs':['RB_Table', 'WR_Table', 'TE_Table', 'FLEX_Table'],
|
@@ -1100,94 +956,25 @@ with tab2:
|
|
1100 |
'team_check_map':dict(zip(cleaport_players.Player,nerf_frame.Team))
|
1101 |
}
|
1102 |
|
1103 |
-
|
1104 |
-
del Overall_Proj
|
1105 |
-
del nerf_frame
|
1106 |
|
1107 |
-
|
1108 |
-
FinalPortfolio, maps_dict = run_seed_frame(seed_depth1, Strength_var, strength_grow, Teams_used, Total_Runs)
|
1109 |
|
1110 |
-
Sim_size = linenum_var1
|
1111 |
-
SimVar = 1
|
1112 |
-
Sim_Winners = []
|
1113 |
-
fp_array = FinalPortfolio.values
|
1114 |
-
|
1115 |
-
if insert_port == 1:
|
1116 |
-
up_array = CleanPortfolio.values
|
1117 |
-
|
1118 |
-
# Pre-vectorize functions
|
1119 |
-
vec_projection_map = np.vectorize(maps_dict['Projection_map'].__getitem__)
|
1120 |
-
vec_stdev_map = np.vectorize(maps_dict['STDev_map'].__getitem__)
|
1121 |
-
|
1122 |
-
if insert_port == 1:
|
1123 |
-
vec_up_projection_map = np.vectorize(up_dict['Projection_map'].__getitem__)
|
1124 |
-
vec_up_stdev_map = np.vectorize(up_dict['STDev_map'].__getitem__)
|
1125 |
-
|
1126 |
-
st.write('Simulating contest on frames')
|
1127 |
-
|
1128 |
-
while SimVar <= Sim_size:
|
1129 |
-
if insert_port == 1:
|
1130 |
-
fp_random = fp_array[np.random.choice(fp_array.shape[0], Contest_Size-len(CleanPortfolio))]
|
1131 |
-
elif insert_port == 0:
|
1132 |
-
fp_random = fp_array[np.random.choice(fp_array.shape[0], Contest_Size)]
|
1133 |
-
|
1134 |
-
sample_arrays1 = np.c_[
|
1135 |
-
fp_random,
|
1136 |
-
np.sum(np.random.normal(
|
1137 |
-
loc=vec_projection_map(fp_random[:, :-5]),
|
1138 |
-
scale=vec_stdev_map(fp_random[:, :-5])),
|
1139 |
-
axis=1)
|
1140 |
-
]
|
1141 |
-
|
1142 |
-
if insert_port == 1:
|
1143 |
-
sample_arrays2 = np.c_[
|
1144 |
-
up_array,
|
1145 |
-
np.sum(np.random.normal(
|
1146 |
-
loc=vec_up_projection_map(up_array[:, :-5]),
|
1147 |
-
scale=vec_up_stdev_map(up_array[:, :-5])),
|
1148 |
-
axis=1)
|
1149 |
-
]
|
1150 |
-
sample_arrays = np.vstack((sample_arrays1, sample_arrays2))
|
1151 |
-
else:
|
1152 |
-
sample_arrays = sample_arrays1
|
1153 |
-
|
1154 |
-
final_array = sample_arrays[sample_arrays[:, 10].argsort()[::-1]]
|
1155 |
-
best_lineup = final_array[final_array[:, -1].argsort(kind='stable')[::-1][:1]]
|
1156 |
-
Sim_Winners.append(best_lineup)
|
1157 |
-
SimVar += 1
|
1158 |
-
|
1159 |
-
del SimVar
|
1160 |
-
del ref_dict, up_dict
|
1161 |
-
del linenum_var1, UserPortfolio
|
1162 |
-
try:
|
1163 |
-
del up_array
|
1164 |
-
except:
|
1165 |
-
pass
|
1166 |
-
del CleanPortfolio
|
1167 |
-
del vec_projection_map
|
1168 |
-
del vec_stdev_map
|
1169 |
-
del sample_arrays
|
1170 |
-
del final_array
|
1171 |
-
del fp_array
|
1172 |
-
del fp_random
|
1173 |
-
st.write('Contest simulation complete')
|
1174 |
# Initial setup
|
1175 |
Sim_Winner_Frame = pd.DataFrame(np.concatenate(Sim_Winners), columns=FinalPortfolio.columns.tolist() + ['Fantasy'])
|
1176 |
Sim_Winner_Frame['GPP_Proj'] = (Sim_Winner_Frame['Projection'] + Sim_Winner_Frame['Fantasy']) / 2
|
1177 |
|
1178 |
-
del FinalPortfolio
|
1179 |
-
|
1180 |
# Type Casting
|
1181 |
type_cast_dict = {'Salary': int, 'Projection': np.float16, 'Fantasy': np.float16, 'GPP_Proj': np.float16}
|
1182 |
Sim_Winner_Frame = Sim_Winner_Frame.astype(type_cast_dict)
|
1183 |
|
|
|
|
|
1184 |
# Sorting
|
1185 |
st.session_state.Sim_Winner_Frame = Sim_Winner_Frame.sort_values(by='GPP_Proj', ascending=False)
|
1186 |
|
1187 |
# Data Copying
|
1188 |
-
st.session_state.Sim_Winner_Export = Sim_Winner_Frame
|
1189 |
-
|
1190 |
-
del Sim_Winner_Frame
|
1191 |
|
1192 |
# Conditional Replacement
|
1193 |
columns_to_replace = ['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST']
|
@@ -1197,124 +984,96 @@ with tab2:
|
|
1197 |
elif site_var1 == 'Fanduel':
|
1198 |
replace_dict = fdid_dict
|
1199 |
|
1200 |
-
del dkid_dict
|
1201 |
-
del fdid_dict
|
1202 |
-
|
1203 |
for col in columns_to_replace:
|
1204 |
st.session_state.Sim_Winner_Export[col].replace(replace_dict, inplace=True)
|
|
|
|
|
1205 |
|
1206 |
-
player_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,0:9].values, return_counts=True)),
|
1207 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1208 |
-
player_freq['Freq'] = player_freq['Freq'].astype(int)
|
1209 |
-
player_freq['Position'] = player_freq['Player'].map(maps_dict['Pos_map'])
|
1210 |
-
player_freq['Salary'] = player_freq['Player'].map(maps_dict['Salary_map'])
|
1211 |
-
player_freq['Proj Own'] = player_freq['Player'].map(maps_dict['Own_map']) / 100
|
1212 |
-
player_freq['Exposure'] = player_freq['Freq']/(
|
1213 |
-
player_freq['Edge'] = player_freq['Exposure'] - player_freq['Proj Own']
|
1214 |
-
player_freq['Team'] = player_freq['Player'].map(maps_dict['Team_map'])
|
1215 |
for checkVar in range(len(team_list)):
|
1216 |
-
player_freq['Team'] = player_freq['Team'].replace(item_list, team_list)
|
1217 |
-
|
1218 |
-
st.session_state.player_freq = player_freq[['Player', 'Position', 'Team', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
1219 |
-
del player_freq
|
1220 |
|
1221 |
-
qb_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,0:1].values, return_counts=True)),
|
1222 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1223 |
-
qb_freq['Freq'] = qb_freq['Freq'].astype(int)
|
1224 |
-
qb_freq['Position'] = qb_freq['Player'].map(maps_dict['Pos_map'])
|
1225 |
-
qb_freq['Salary'] = qb_freq['Player'].map(maps_dict['Salary_map'])
|
1226 |
-
qb_freq['Proj Own'] = qb_freq['Player'].map(maps_dict['Own_map']) / 100
|
1227 |
-
qb_freq['Exposure'] = qb_freq['Freq']/(
|
1228 |
-
qb_freq['Edge'] = qb_freq['Exposure'] - qb_freq['Proj Own']
|
1229 |
-
qb_freq['Team'] = qb_freq['Player'].map(maps_dict['Team_map'])
|
1230 |
for checkVar in range(len(team_list)):
|
1231 |
-
qb_freq['Team'] = qb_freq['Team'].replace(item_list, team_list)
|
1232 |
|
1233 |
-
st.session_state.
|
1234 |
-
del qb_freq
|
1235 |
-
|
1236 |
-
rb_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,[1, 2]].values, return_counts=True)),
|
1237 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1238 |
-
rb_freq['Freq'] = rb_freq['Freq'].astype(int)
|
1239 |
-
rb_freq['Position'] = rb_freq['Player'].map(maps_dict['Pos_map'])
|
1240 |
-
rb_freq['Salary'] = rb_freq['Player'].map(maps_dict['Salary_map'])
|
1241 |
-
rb_freq['Proj Own'] = rb_freq['Player'].map(maps_dict['Own_map']) / 100
|
1242 |
-
rb_freq['Exposure'] = rb_freq['Freq']/
|
1243 |
-
rb_freq['Edge'] = rb_freq['Exposure'] - rb_freq['Proj Own']
|
1244 |
-
rb_freq['Team'] = rb_freq['Player'].map(maps_dict['Team_map'])
|
1245 |
for checkVar in range(len(team_list)):
|
1246 |
-
rb_freq['Team'] = rb_freq['Team'].replace(item_list, team_list)
|
1247 |
-
|
1248 |
-
st.session_state.rb_freq = rb_freq[['Player', 'Team', 'Position', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
1249 |
-
del rb_freq
|
1250 |
|
1251 |
-
wr_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,[3, 4, 5]].values, return_counts=True)),
|
1252 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1253 |
-
wr_freq['Freq'] = wr_freq['Freq'].astype(int)
|
1254 |
-
wr_freq['Position'] = wr_freq['Player'].map(maps_dict['Pos_map'])
|
1255 |
-
wr_freq['Salary'] = wr_freq['Player'].map(maps_dict['Salary_map'])
|
1256 |
-
wr_freq['Proj Own'] = wr_freq['Player'].map(maps_dict['Own_map']) / 100
|
1257 |
-
wr_freq['Exposure'] = wr_freq['Freq']/
|
1258 |
-
wr_freq['Edge'] = wr_freq['Exposure'] - wr_freq['Proj Own']
|
1259 |
-
wr_freq['Team'] = wr_freq['Player'].map(maps_dict['Team_map'])
|
1260 |
for checkVar in range(len(team_list)):
|
1261 |
-
wr_freq['Team'] = wr_freq['Team'].replace(item_list, team_list)
|
1262 |
-
|
1263 |
-
st.session_state.wr_freq = wr_freq[['Player', 'Team', 'Position', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
1264 |
-
del wr_freq
|
1265 |
|
1266 |
-
te_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,[6]].values, return_counts=True)),
|
1267 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1268 |
-
te_freq['Freq'] = te_freq['Freq'].astype(int)
|
1269 |
-
te_freq['Position'] = te_freq['Player'].map(maps_dict['Pos_map'])
|
1270 |
-
te_freq['Salary'] = te_freq['Player'].map(maps_dict['Salary_map'])
|
1271 |
-
te_freq['Proj Own'] = te_freq['Player'].map(maps_dict['Own_map']) / 100
|
1272 |
-
te_freq['Exposure'] = te_freq['Freq']/
|
1273 |
-
te_freq['Edge'] = te_freq['Exposure'] - te_freq['Proj Own']
|
1274 |
-
te_freq['Team'] = te_freq['Player'].map(maps_dict['Team_map'])
|
1275 |
for checkVar in range(len(team_list)):
|
1276 |
-
te_freq['Team'] = te_freq['Team'].replace(item_list, team_list)
|
1277 |
-
|
1278 |
-
st.session_state.te_freq = te_freq[['Player', 'Team', 'Position', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
1279 |
-
del te_freq
|
1280 |
|
1281 |
-
flex_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,[7]].values, return_counts=True)),
|
1282 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1283 |
-
flex_freq['Freq'] = flex_freq['Freq'].astype(int)
|
1284 |
-
flex_freq['Position'] = flex_freq['Player'].map(maps_dict['Pos_map'])
|
1285 |
-
flex_freq['Salary'] = flex_freq['Player'].map(maps_dict['Salary_map'])
|
1286 |
-
flex_freq['Proj Own'] = flex_freq['Player'].map(maps_dict['Own_map']) / 100
|
1287 |
-
flex_freq['Exposure'] = flex_freq['Freq']/
|
1288 |
-
flex_freq['Edge'] = flex_freq['Exposure'] - flex_freq['Proj Own']
|
1289 |
-
flex_freq['Team'] = flex_freq['Player'].map(maps_dict['Team_map'])
|
1290 |
for checkVar in range(len(team_list)):
|
1291 |
-
flex_freq['Team'] = flex_freq['Team'].replace(item_list, team_list)
|
1292 |
-
|
1293 |
-
st.session_state.flex_freq = flex_freq[['Player', 'Team', 'Position', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
1294 |
-
del flex_freq
|
1295 |
|
1296 |
-
dst_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,8:9].values, return_counts=True)),
|
1297 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1298 |
-
dst_freq['Freq'] = dst_freq['Freq'].astype(int)
|
1299 |
-
dst_freq['Position'] = dst_freq['Player'].map(maps_dict['Pos_map'])
|
1300 |
-
dst_freq['Salary'] = dst_freq['Player'].map(maps_dict['Salary_map'])
|
1301 |
-
dst_freq['Proj Own'] = dst_freq['Player'].map(maps_dict['Own_map']) / 100
|
1302 |
-
dst_freq['Exposure'] = dst_freq['Freq']/
|
1303 |
-
dst_freq['Edge'] = dst_freq['Exposure'] - dst_freq['Proj Own']
|
1304 |
-
dst_freq['Team'] = dst_freq['Player'].map(maps_dict['Team_map'])
|
1305 |
for checkVar in range(len(team_list)):
|
1306 |
-
dst_freq['Team'] = dst_freq['Team'].replace(item_list, team_list)
|
1307 |
-
|
1308 |
-
st.session_state.dst_freq = dst_freq[['Player', 'Team', 'Position', 'Salary', 'Proj Own', 'Exposure', 'Edge']]
|
1309 |
-
del dst_freq
|
1310 |
-
|
1311 |
-
del Sim_size
|
1312 |
-
del maps_dict
|
1313 |
-
del team_list
|
1314 |
-
del item_list
|
1315 |
|
1316 |
with st.container():
|
1317 |
-
simulate_container = st.empty()
|
1318 |
if 'player_freq' in st.session_state:
|
1319 |
player_split_var2 = st.radio("Are you wanting to isolate any lineups with specific players?", ('Full Players', 'Specific Players'), key='player_split_var2')
|
1320 |
if player_split_var2 == 'Specific Players':
|
@@ -1323,7 +1082,7 @@ with tab2:
|
|
1323 |
find_var2 = st.session_state.player_freq.Player.values.tolist()
|
1324 |
|
1325 |
if player_split_var2 == 'Specific Players':
|
1326 |
-
st.session_state.Sim_Winner_Display = st.session_state.Sim_Winner_Frame[np.equal.outer(st.session_state.Sim_Winner_Frame.to_numpy(
|
1327 |
if player_split_var2 == 'Full Players':
|
1328 |
st.session_state.Sim_Winner_Display = st.session_state.Sim_Winner_Frame
|
1329 |
if 'Sim_Winner_Display' in st.session_state:
|
@@ -1331,20 +1090,19 @@ with tab2:
|
|
1331 |
if 'Sim_Winner_Export' in st.session_state:
|
1332 |
st.download_button(
|
1333 |
label="Export Tables",
|
1334 |
-
data=
|
1335 |
file_name='NFL_consim_export.csv',
|
1336 |
mime='text/csv',
|
1337 |
)
|
1338 |
|
1339 |
with st.container():
|
1340 |
-
freq_container = st.empty()
|
1341 |
tab1, tab2, tab3, tab4, tab5, tab6, tab7 = st.tabs(['Overall Exposures', 'QB Exposures', 'RB Exposures', 'WR Exposures', 'TE Exposures', 'FLEX Exposures', 'DST Exposures'])
|
1342 |
with tab1:
|
1343 |
if 'player_freq' in st.session_state:
|
1344 |
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)
|
1345 |
st.download_button(
|
1346 |
label="Export Exposures",
|
1347 |
-
data=
|
1348 |
file_name='player_freq_export.csv',
|
1349 |
mime='text/csv',
|
1350 |
)
|
@@ -1353,7 +1111,7 @@ with tab2:
|
|
1353 |
st.dataframe(st.session_state.qb_freq.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(freq_format, precision=2), use_container_width = True)
|
1354 |
st.download_button(
|
1355 |
label="Export Exposures",
|
1356 |
-
data=
|
1357 |
file_name='qb_freq_export.csv',
|
1358 |
mime='text/csv',
|
1359 |
)
|
@@ -1362,7 +1120,7 @@ with tab2:
|
|
1362 |
st.dataframe(st.session_state.rb_freq.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(freq_format, precision=2), use_container_width = True)
|
1363 |
st.download_button(
|
1364 |
label="Export Exposures",
|
1365 |
-
data=
|
1366 |
file_name='rb_freq_export.csv',
|
1367 |
mime='text/csv',
|
1368 |
)
|
@@ -1371,7 +1129,7 @@ with tab2:
|
|
1371 |
st.dataframe(st.session_state.wr_freq.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(freq_format, precision=2), use_container_width = True)
|
1372 |
st.download_button(
|
1373 |
label="Export Exposures",
|
1374 |
-
data=
|
1375 |
file_name='wr_freq_export.csv',
|
1376 |
mime='text/csv',
|
1377 |
)
|
@@ -1380,7 +1138,7 @@ with tab2:
|
|
1380 |
st.dataframe(st.session_state.te_freq.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(freq_format, precision=2), use_container_width = True)
|
1381 |
st.download_button(
|
1382 |
label="Export Exposures",
|
1383 |
-
data=
|
1384 |
file_name='te_freq_export.csv',
|
1385 |
mime='text/csv',
|
1386 |
)
|
@@ -1389,7 +1147,7 @@ with tab2:
|
|
1389 |
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)
|
1390 |
st.download_button(
|
1391 |
label="Export Exposures",
|
1392 |
-
data=
|
1393 |
file_name='flex_freq_export.csv',
|
1394 |
mime='text/csv',
|
1395 |
)
|
@@ -1398,7 +1156,16 @@ with tab2:
|
|
1398 |
st.dataframe(st.session_state.dst_freq.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(freq_format, precision=2), use_container_width = True)
|
1399 |
st.download_button(
|
1400 |
label="Export Exposures",
|
1401 |
-
data=
|
1402 |
file_name='dst_freq_export.csv',
|
1403 |
mime='text/csv',
|
1404 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gspread-connection%40sheets-api-connect-378620.iam.gserviceaccount.com"
|
30 |
}
|
31 |
|
32 |
+
gc_con = gspread.service_account_from_dict(credentials)
|
33 |
+
|
34 |
+
return gc_con
|
35 |
|
36 |
+
gcservice_account = init_conn()
|
37 |
|
38 |
freq_format = {'Proj Own': '{:.2%}', 'Exposure': '{:.2%}', 'Edge': '{:.2%}'}
|
39 |
|
40 |
@st.cache_resource(ttl = 300)
|
41 |
def load_dk_player_projections():
|
42 |
+
sh = gcservice_account.open_by_url('https://docs.google.com/spreadsheets/d/1I_1Ve3F4tftgfLQQoRKOJ351XfEG48s36OxXUKxmgS8/edit#gid=1391856348')
|
43 |
worksheet = sh.worksheet('DK_ROO')
|
44 |
load_display = pd.DataFrame(worksheet.get_all_records())
|
45 |
load_display.replace('', np.nan, inplace=True)
|
46 |
raw_display = load_display.dropna(subset=['Median'])
|
|
|
47 |
|
48 |
return raw_display
|
49 |
|
50 |
@st.cache_resource(ttl = 300)
|
51 |
def load_fd_player_projections():
|
52 |
+
sh = gcservice_account.open_by_url('https://docs.google.com/spreadsheets/d/1I_1Ve3F4tftgfLQQoRKOJ351XfEG48s36OxXUKxmgS8/edit#gid=1391856348')
|
53 |
worksheet = sh.worksheet('FD_ROO')
|
54 |
load_display = pd.DataFrame(worksheet.get_all_records())
|
55 |
load_display.replace('', np.nan, inplace=True)
|
56 |
raw_display = load_display.dropna(subset=['Median'])
|
|
|
57 |
|
58 |
return raw_display
|
59 |
|
60 |
@st.cache_resource(ttl = 300)
|
61 |
def set_export_ids():
|
62 |
+
sh = gcservice_account.open_by_url('https://docs.google.com/spreadsheets/d/1I_1Ve3F4tftgfLQQoRKOJ351XfEG48s36OxXUKxmgS8/edit#gid=1391856348')
|
63 |
worksheet = sh.worksheet('DK_ROO')
|
64 |
load_display = pd.DataFrame(worksheet.get_all_records())
|
65 |
load_display.replace('', np.nan, inplace=True)
|
|
|
71 |
load_display.replace('', np.nan, inplace=True)
|
72 |
raw_display = load_display.dropna(subset=['Median'])
|
73 |
fd_ids = dict(zip(raw_display['Player'], raw_display['player_id']))
|
|
|
|
|
|
|
74 |
|
75 |
return dk_ids, fd_ids
|
76 |
|
77 |
+
dk_roo_raw = load_dk_player_projections()
|
78 |
+
fd_roo_raw = load_fd_player_projections()
|
79 |
+
t_stamp = f"Last Update: " + str(dk_roo_raw['timestamp'][0]) + f" CST"
|
80 |
+
dkid_dict, fdid_dict = set_export_ids()
|
81 |
|
82 |
+
static_exposure = pd.DataFrame(columns=['Player', 'count'])
|
83 |
+
overall_exposure = pd.DataFrame(columns=['Player', 'count'])
|
84 |
+
|
85 |
+
def sim_contest(Sim_size, FinalPortfolio, CleanPortfolio, maps_dict, up_dict, insert_port):
|
86 |
+
SimVar = 1
|
87 |
+
Sim_Winners = []
|
88 |
+
fp_array = FinalPortfolio.values
|
89 |
+
|
90 |
+
if insert_port == 1:
|
91 |
+
up_array = CleanPortfolio.values
|
92 |
+
|
93 |
+
# Pre-vectorize functions
|
94 |
+
vec_projection_map = np.vectorize(maps_dict['Projection_map'].__getitem__)
|
95 |
+
vec_stdev_map = np.vectorize(maps_dict['STDev_map'].__getitem__)
|
96 |
+
|
97 |
+
if insert_port == 1:
|
98 |
+
vec_up_projection_map = np.vectorize(up_dict['Projection_map'].__getitem__)
|
99 |
+
vec_up_stdev_map = np.vectorize(up_dict['STDev_map'].__getitem__)
|
100 |
+
|
101 |
+
st.write('Simulating contest on frames')
|
102 |
+
|
103 |
+
while SimVar <= Sim_size:
|
104 |
+
if insert_port == 1:
|
105 |
+
fp_random = fp_array[np.random.choice(fp_array.shape[0], Contest_Size-len(CleanPortfolio))]
|
106 |
+
elif insert_port == 0:
|
107 |
+
fp_random = fp_array[np.random.choice(fp_array.shape[0], Contest_Size)]
|
108 |
+
|
109 |
+
sample_arrays1 = np.c_[
|
110 |
+
fp_random,
|
111 |
+
np.sum(np.random.normal(
|
112 |
+
loc=vec_projection_map(fp_random[:, :-5]),
|
113 |
+
scale=vec_stdev_map(fp_random[:, :-5])),
|
114 |
+
axis=1)
|
115 |
+
]
|
116 |
+
|
117 |
+
if insert_port == 1:
|
118 |
+
sample_arrays2 = np.c_[
|
119 |
+
up_array,
|
120 |
+
np.sum(np.random.normal(
|
121 |
+
loc=vec_up_projection_map(up_array[:, :-5]),
|
122 |
+
scale=vec_up_stdev_map(up_array[:, :-5])),
|
123 |
+
axis=1)
|
124 |
+
]
|
125 |
+
sample_arrays = np.vstack((sample_arrays1, sample_arrays2))
|
126 |
+
else:
|
127 |
+
sample_arrays = sample_arrays1
|
128 |
+
|
129 |
+
final_array = sample_arrays[sample_arrays[:, 10].argsort()[::-1]]
|
130 |
+
best_lineup = final_array[final_array[:, -1].argsort(kind='stable')[::-1][:1]]
|
131 |
+
Sim_Winners.append(best_lineup)
|
132 |
+
SimVar += 1
|
133 |
+
|
134 |
+
return Sim_Winners
|
135 |
+
|
136 |
+
def run_seed_frame(seed_depth1, Strength_var, strength_grow, Teams_used, Total_Runs, field_growth):
|
137 |
RunsVar = 1
|
138 |
seed_depth_def = seed_depth1
|
139 |
Strength_var_def = Strength_var
|
140 |
strength_grow_def = strength_grow
|
141 |
Teams_used_def = Teams_used
|
142 |
Total_Runs_def = Total_Runs
|
143 |
+
|
144 |
while RunsVar <= seed_depth_def:
|
145 |
if RunsVar <= 3:
|
146 |
FieldStrength = Strength_var_def
|
147 |
+
FinalPortfolio, maps_dict = get_correlated_portfolio_for_sim(Total_Runs_def * .25, sharp_split, field_growth)
|
148 |
+
FinalPortfolio2, maps_dict2 = get_uncorrelated_portfolio_for_sim(Total_Runs_def * .25, sharp_split, field_growth)
|
149 |
+
FinalPortfolio_init = pd.concat([FinalPortfolio, FinalPortfolio2], axis=0)
|
150 |
maps_dict.update(maps_dict2)
|
|
|
|
|
151 |
elif RunsVar > 3 and RunsVar <= 4:
|
152 |
FieldStrength += (strength_grow_def + ((30 - len(Teams_used_def)) * .001))
|
153 |
+
FinalPortfolio3, maps_dict3 = get_correlated_portfolio_for_sim(Total_Runs_def * .25, sharp_split, field_growth)
|
154 |
+
FinalPortfolio4, maps_dict4 = get_uncorrelated_portfolio_for_sim(Total_Runs_def * .25, sharp_split, field_growth)
|
155 |
+
FinalPortfolio_merge_3 = pd.concat([FinalPortfolio_init, FinalPortfolio3], axis=0)
|
156 |
+
FinalPortfolio_merge_4 = pd.concat([FinalPortfolio_merge_3, FinalPortfolio4], axis=0)
|
157 |
+
FinalPortfolio_step_2 = FinalPortfolio_merge_4.drop_duplicates(subset = ['Projection', 'Own'],keep = 'last').reset_index(drop = True)
|
158 |
maps_dict.update(maps_dict3)
|
159 |
maps_dict.update(maps_dict4)
|
|
|
|
|
|
|
|
|
160 |
elif RunsVar > 4:
|
161 |
FieldStrength = 1
|
162 |
+
FinalPortfolio5, maps_dict5 = get_correlated_portfolio_for_sim(Total_Runs_def * .25, sharp_split, field_growth)
|
163 |
+
FinalPortfolio6, maps_dict6 = get_uncorrelated_portfolio_for_sim(Total_Runs_def * .25, sharp_split, field_growth)
|
164 |
+
FinalPortfolio_merge_5 = pd.concat([FinalPortfolio_step_2, FinalPortfolio5], axis=0)
|
165 |
+
FinalPortfolio_merge_6 = pd.concat([FinalPortfolio_merge_5, FinalPortfolio6], axis=0)
|
166 |
+
FinalPortfolio_export = FinalPortfolio_merge_6.drop_duplicates(subset = ['Projection', 'Own'],keep = 'last').reset_index(drop = True)
|
167 |
+
maps_dict.update(maps_dict5)
|
168 |
+
maps_dict.update(maps_dict6)
|
|
|
|
|
|
|
|
|
169 |
RunsVar += 1
|
170 |
+
|
171 |
+
return FinalPortfolio_export, maps_dict
|
172 |
|
173 |
def create_stack_options(player_data, wr_var):
|
174 |
merged_frame = pd.DataFrame(columns = ['QB', 'Player'])
|
|
|
184 |
merged_frame = merged_frame.reset_index()
|
185 |
correl_dict = dict(zip(merged_frame.QB, merged_frame.Player))
|
186 |
|
|
|
|
|
|
|
187 |
return correl_dict
|
188 |
|
189 |
def create_overall_dfs(pos_players, table_name, dict_name, pos):
|
|
|
193 |
overall_table_name = table_name_raw.head(round(len(table_name_raw)))
|
194 |
overall_table_name = overall_table_name.assign(Var = range(0,len(overall_table_name)))
|
195 |
overall_dict_name = pd.Series(overall_table_name.Player.values, index=overall_table_name.Var).to_dict()
|
|
|
|
|
|
|
196 |
elif pos != "FLEX":
|
197 |
table_name_raw = pos_players[pos_players['Position'].str.contains(pos)].reset_index(drop=True)
|
198 |
overall_table_name = table_name_raw.head(round(len(table_name_raw)))
|
199 |
overall_table_name = overall_table_name.assign(Var = range(0,len(overall_table_name)))
|
200 |
overall_dict_name = pd.Series(overall_table_name.Player.values, index=overall_table_name.Var).to_dict()
|
|
|
|
|
|
|
201 |
|
202 |
return overall_table_name, overall_dict_name
|
203 |
|
|
|
215 |
|
216 |
df_out = pd.concat(ref_dict['pos_dfs'], ignore_index=True)
|
217 |
|
218 |
+
return ref_dict
|
219 |
|
220 |
def calculate_range_var(count, min_val, FieldStrength, field_growth):
|
221 |
var = round(len(count[0]) * FieldStrength)
|
222 |
var = max(var, min_val)
|
223 |
var += round(field_growth)
|
224 |
+
|
225 |
return min(var, len(count[0]))
|
226 |
|
227 |
+
def create_random_portfolio(Total_Sample_Size, raw_baselines, field_growth):
|
228 |
+
|
229 |
+
st.write('Creating Seed Frames')
|
230 |
|
231 |
+
full_pos_player_dict = get_overall_merged_df()
|
232 |
max_var = len(raw_baselines[raw_baselines['Position'] == 'QB'])
|
233 |
|
234 |
field_growth_rounded = round(field_growth)
|
|
|
247 |
elif max_var > 16:
|
248 |
ranges_dict['qb_range'] = round(max_var / 2)
|
249 |
ranges_dict['dst_range'] = round(max_var)
|
|
|
|
|
|
|
250 |
|
251 |
# Generate random portfolios
|
252 |
rng = np.random.default_rng()
|
|
|
256 |
all_choices = [rng.choice(ranges_dict[f"{key}_range"], size=(Total_Sample_Size, elem)) for key, elem in zip(keys, total_elements)]
|
257 |
RandomPortfolio = pd.DataFrame(np.hstack(all_choices), columns=['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST'])
|
258 |
RandomPortfolio['User/Field'] = 0
|
|
|
|
|
|
|
|
|
|
|
259 |
|
260 |
return RandomPortfolio, maps_dict, ranges_dict, full_pos_player_dict
|
261 |
|
262 |
+
def get_correlated_portfolio_for_sim(Total_Sample_Size, sharp_split, field_growth):
|
263 |
|
264 |
sizesplit = round(Total_Sample_Size * sharp_split)
|
265 |
|
266 |
+
RandomPortfolio, maps_dict, ranges_dict, full_pos_player_dict = create_random_portfolio(sizesplit, raw_baselines, field_growth)
|
267 |
stack_num = random.randint(1, 3)
|
268 |
stacking_dict = create_stack_options(raw_baselines, stack_num)
|
269 |
|
|
|
281 |
RandomPortfolio = RandomPortfolio[RandomPortfolio['plyr_count'] == 10].drop(columns=['plyr_list','plyr_count']).\
|
282 |
reset_index(drop=True)
|
283 |
|
|
|
|
|
|
|
|
|
|
|
|
|
284 |
RandomPortfolio['QBs'] = RandomPortfolio['QB'].map(maps_dict['Salary_map']).astype(np.int32)
|
285 |
RandomPortfolio['RB1s'] = RandomPortfolio['RB1'].map(maps_dict['Salary_map']).astype(np.int32)
|
286 |
RandomPortfolio['RB2s'] = RandomPortfolio['RB2'].map(maps_dict['Salary_map']).astype(np.int32)
|
|
|
312 |
RandomPortfolio['DSTo'] = RandomPortfolio['DST'].map(maps_dict['Own_map']).astype(np.float16)
|
313 |
|
314 |
RandomPortArray = RandomPortfolio.to_numpy()
|
|
|
315 |
|
316 |
RandomPortArray = np.c_[RandomPortArray, np.einsum('ij->i',RandomPortArray[:,10:19].astype(int))]
|
317 |
RandomPortArray = np.c_[RandomPortArray, np.einsum('ij->i',RandomPortArray[:,19:28].astype(np.double))]
|
|
|
320 |
RandomPortArrayOut = np.delete(RandomPortArray, np.s_[10:37], axis=1)
|
321 |
RandomPortfolioDF = pd.DataFrame(RandomPortArrayOut, columns = ['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST', 'User/Field', 'Salary', 'Projection', 'Own'])
|
322 |
RandomPortfolioDF = RandomPortfolioDF.sort_values(by=Sim_function, ascending=False)
|
|
|
|
|
323 |
|
324 |
if insert_port == 1:
|
325 |
CleanPortfolio['Salary'] = sum([CleanPortfolio['QB'].map(maps_dict['Salary_map']),
|
|
|
364 |
|
365 |
RandomPortfolio = RandomPortfolioDF.sort_values(by=Sim_function, ascending=False)
|
366 |
|
|
|
|
|
367 |
RandomPortfolio = RandomPortfolio[['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST', 'User/Field', 'Salary', 'Projection', 'Own']]
|
368 |
|
369 |
return RandomPortfolio, maps_dict
|
370 |
|
371 |
+
def get_uncorrelated_portfolio_for_sim(Total_Sample_Size, sharp_split, field_growth):
|
372 |
|
373 |
sizesplit = round(Total_Sample_Size * (1-sharp_split))
|
374 |
|
375 |
+
RandomPortfolio, maps_dict, ranges_dict, full_pos_player_dict = create_random_portfolio(sizesplit, raw_baselines, field_growth)
|
376 |
|
377 |
RandomPortfolio['QB'] = pd.Series(list(RandomPortfolio['QB'].map(qb_dict)), dtype="string[pyarrow]")
|
378 |
RandomPortfolio['RB1'] = pd.Series(list(RandomPortfolio['RB1'].map(full_pos_player_dict['pos_dicts'][0])), dtype="string[pyarrow]")
|
|
|
388 |
RandomPortfolio = RandomPortfolio[RandomPortfolio['plyr_count'] == 10].drop(columns=['plyr_list','plyr_count']).\
|
389 |
reset_index(drop=True)
|
390 |
|
|
|
|
|
|
|
|
|
391 |
RandomPortfolio['QBs'] = RandomPortfolio['QB'].map(maps_dict['Salary_map']).astype(np.int32)
|
392 |
RandomPortfolio['RB1s'] = RandomPortfolio['RB1'].map(maps_dict['Salary_map']).astype(np.int32)
|
393 |
RandomPortfolio['RB2s'] = RandomPortfolio['RB2'].map(maps_dict['Salary_map']).astype(np.int32)
|
|
|
419 |
RandomPortfolio['DSTo'] = RandomPortfolio['DST'].map(maps_dict['Own_map']).astype(np.float16)
|
420 |
|
421 |
RandomPortArray = RandomPortfolio.to_numpy()
|
|
|
422 |
|
423 |
RandomPortArray = np.c_[RandomPortArray, np.einsum('ij->i',RandomPortArray[:,10:19].astype(int))]
|
424 |
RandomPortArray = np.c_[RandomPortArray, np.einsum('ij->i',RandomPortArray[:,19:28].astype(np.double))]
|
|
|
427 |
RandomPortArrayOut = np.delete(RandomPortArray, np.s_[10:37], axis=1)
|
428 |
RandomPortfolioDF = pd.DataFrame(RandomPortArrayOut, columns = ['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST', 'User/Field', 'Salary', 'Projection', 'Own'])
|
429 |
RandomPortfolioDF = RandomPortfolioDF.sort_values(by=Sim_function, ascending=False)
|
|
|
|
|
|
|
430 |
|
431 |
if insert_port == 1:
|
432 |
CleanPortfolio['Salary'] = sum([CleanPortfolio['QB'].map(maps_dict['Salary_map']),
|
|
|
473 |
|
474 |
RandomPortfolio = RandomPortfolio[['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST', 'User/Field', 'Salary', 'Projection', 'Own']]
|
475 |
|
|
|
|
|
476 |
return RandomPortfolio, maps_dict
|
477 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
478 |
tab1, tab2 = st.tabs(['Uploads', 'Contest Sim'])
|
479 |
|
480 |
with tab1:
|
|
|
507 |
player_salary_dict = dict(zip(proj_dataframe.Player, proj_dataframe.Salary))
|
508 |
player_proj_dict = dict(zip(proj_dataframe.Player, proj_dataframe.Median))
|
509 |
player_own_dict = dict(zip(proj_dataframe.Player, proj_dataframe.Own))
|
|
|
510 |
|
511 |
with col2:
|
512 |
st.info("The Portfolio file must contain only columns in order and explicitly named: 'QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', and 'DST'. Upload your projections first to avoid an error message.")
|
|
|
574 |
split_portfolio['TE'].map(player_own_dict),
|
575 |
split_portfolio['FLEX'].map(player_own_dict),
|
576 |
split_portfolio['DST'].map(player_own_dict)])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
577 |
|
578 |
|
579 |
except:
|
|
|
630 |
split_portfolio['TE'].map(player_own_dict),
|
631 |
split_portfolio['FLEX'].map(player_own_dict),
|
632 |
split_portfolio['DST'].map(player_own_dict)])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
633 |
|
634 |
except:
|
635 |
split_portfolio = portfolio_dataframe
|
|
|
663 |
split_portfolio['TE'].map(player_own_dict),
|
664 |
split_portfolio['FLEX'].map(player_own_dict),
|
665 |
split_portfolio['DST'].map(player_own_dict)])
|
666 |
+
|
667 |
+
gc.collect()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
668 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
669 |
with tab2:
|
670 |
col1, col2 = st.columns([1, 7])
|
671 |
with col1:
|
|
|
693 |
elif slate_var1 != 'User':
|
694 |
raw_baselines = fd_roo_raw[fd_roo_raw['slate'] == str(slate_var1)]
|
695 |
raw_baselines = raw_baselines[raw_baselines['version'] == 'overall']
|
696 |
+
|
|
|
697 |
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")
|
698 |
insert_port1 = st.selectbox("Are you uploading a portfolio?", ('No', 'Yes'), key='insert_port1')
|
699 |
if insert_port1 == 'Yes':
|
|
|
707 |
Contest_Size = 5000
|
708 |
elif contest_var1 == 'Large':
|
709 |
Contest_Size = 10000
|
|
|
710 |
strength_var1 = st.selectbox("How sharp is the field in the contest?", ('Not Very', 'Average', 'Very'))
|
711 |
if strength_var1 == 'Not Very':
|
712 |
sharp_split = .33
|
|
|
720 |
sharp_split = .75
|
721 |
Strength_var = .01
|
722 |
scaling_var = 15
|
723 |
+
|
724 |
+
Sort_function = 'Median'
|
725 |
+
Sim_function = 'Projection'
|
726 |
+
|
727 |
+
if Contest_Size <= 1000:
|
728 |
+
strength_grow = .01
|
729 |
+
elif Contest_Size > 1000 and Contest_Size <= 2500:
|
730 |
+
strength_grow = .025
|
731 |
+
elif Contest_Size > 2500 and Contest_Size <= 5000:
|
732 |
+
strength_grow = .05
|
733 |
+
elif Contest_Size > 5000 and Contest_Size <= 20000:
|
734 |
+
strength_grow = .075
|
735 |
+
elif Contest_Size > 20000:
|
736 |
+
strength_grow = .1
|
737 |
+
|
738 |
+
field_growth = 100 * strength_grow
|
739 |
|
740 |
with col2:
|
741 |
with st.container():
|
742 |
if st.button("Simulate Contest"):
|
743 |
with st.container():
|
|
|
744 |
for key in st.session_state.keys():
|
745 |
del st.session_state[key]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
746 |
|
747 |
if slate_var1 == 'User':
|
748 |
+
initial_proj = proj_dataframe[['Player', 'Team', 'Position', 'Median', 'Own', 'Floor', 'Ceiling', 'Salary']]
|
749 |
+
|
750 |
+
# Define the calculation to be applied
|
751 |
+
def calculate_own(position, own, mean_own, factor, max_own=75):
|
752 |
+
return np.where((position == 'QB') & (own - mean_own >= 0),
|
753 |
+
own * (factor * (own - mean_own) / 100) + mean_own,
|
754 |
+
own)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
755 |
|
756 |
+
# Set the factors based on the contest_var1
|
757 |
+
factor_qb, factor_other = {
|
758 |
+
'Small': (10, 5),
|
759 |
+
'Medium': (6, 3),
|
760 |
+
'Large': (3, 1.5),
|
761 |
+
}[contest_var1]
|
762 |
+
|
763 |
+
# Apply the calculation to the DataFrame
|
764 |
+
initial_proj['Own%'] = initial_proj.apply(lambda row: calculate_own(row['Position'], row['Own'], initial_proj.loc[initial_proj['Position'] == row['Position'], 'Own'].mean(), factor_qb if row['Position'] == 'QB' else factor_other), axis=1)
|
765 |
+
initial_proj['Own%'] = initial_proj['Own%'].clip(upper=75)
|
766 |
+
initial_proj['Own'] = initial_proj['Own%'] * (900 / initial_proj['Own%'].sum())
|
767 |
+
|
768 |
+
# Drop unnecessary columns and create the final DataFrame
|
769 |
+
Overall_Proj = initial_proj[['Player', 'Team', 'Position', 'Median', 'Own', 'Salary']]
|
770 |
|
771 |
elif slate_var1 != 'User':
|
772 |
+
# Copy only the necessary columns
|
773 |
+
initial_proj = raw_baselines[['Player', 'Team', 'Position', 'Median', 'Own', 'Floor', 'Ceiling', 'Salary']]
|
774 |
+
|
775 |
+
# Define the calculation to be applied
|
776 |
+
def calculate_own(position, own, mean_own, factor, max_own=75):
|
777 |
+
return np.where((position == 'QB') & (own - mean_own >= 0),
|
778 |
+
own * (factor * (own - mean_own) / 100) + mean_own,
|
779 |
+
own)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
780 |
|
781 |
+
# Set the factors based on the contest_var1
|
782 |
+
factor_qb, factor_other = {
|
783 |
+
'Small': (10, 5),
|
784 |
+
'Medium': (6, 3),
|
785 |
+
'Large': (3, 1.5),
|
786 |
+
}[contest_var1]
|
787 |
+
|
788 |
+
# Apply the calculation to the DataFrame
|
789 |
+
initial_proj['Own%'] = initial_proj.apply(lambda row: calculate_own(row['Position'], row['Own'], initial_proj.loc[initial_proj['Position'] == row['Position'], 'Own'].mean(), factor_qb if row['Position'] == 'QB' else factor_other), axis=1)
|
790 |
+
initial_proj['Own%'] = initial_proj['Own%'].clip(upper=75)
|
791 |
+
initial_proj['Own'] = initial_proj['Own%'] * (900 / initial_proj['Own%'].sum())
|
792 |
+
|
793 |
+
# Drop unnecessary columns and create the final DataFrame
|
794 |
+
Overall_Proj = initial_proj[['Player', 'Team', 'Position', 'Median', 'Own', 'Salary']]
|
795 |
|
796 |
if insert_port == 1:
|
797 |
UserPortfolio = portfolio_dataframe[['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST']]
|
|
|
815 |
Teams_used['team_item'] = Teams_used['index'] + 1
|
816 |
Teams_used = Teams_used.drop(columns=['index'])
|
817 |
Teams_used_dictraw = Teams_used.drop(columns=['team_item'])
|
|
|
|
|
|
|
818 |
|
819 |
team_list = Teams_used['Team'].to_list()
|
820 |
item_list = Teams_used['team_item'].to_list()
|
|
|
822 |
FieldStrength_raw = Strength_var + ((30 - len(Teams_used)) * .01)
|
823 |
FieldStrength = FieldStrength_raw - (FieldStrength_raw * (20000 / Contest_Size))
|
824 |
|
|
|
|
|
825 |
if FieldStrength < 0:
|
826 |
FieldStrength = Strength_var
|
827 |
field_split = Strength_var
|
|
|
865 |
pos_players = pd.concat([rbs_raw, wrs_raw, tes_raw])
|
866 |
pos_players.dropna(subset=['Median']).reset_index(drop=True)
|
867 |
pos_players = pos_players.reset_index(drop=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
868 |
|
869 |
if insert_port == 1:
|
870 |
try:
|
|
|
884 |
CleanPortfolio['User/Field'] = CleanPortfolio['index'] + 1
|
885 |
CleanPortfolio.drop(columns=['index'], inplace=True)
|
886 |
|
|
|
|
|
887 |
CleanPortfolio.replace('', np.nan, inplace=True)
|
888 |
CleanPortfolio.dropna(subset=['QB'], inplace=True)
|
889 |
|
|
|
898 |
nerf_frame = pd.merge(cleaport_players, Overall_Proj, on='Player', how='left')
|
899 |
for col in ['Median', 'Floor', 'Ceiling', 'STDev']:
|
900 |
nerf_frame[col] *= 0.90
|
|
|
901 |
except:
|
902 |
CleanPortfolio = UserPortfolio.reset_index()
|
903 |
CleanPortfolio['User/Field'] = CleanPortfolio['index'] + 1
|
|
|
925 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
926 |
cleaport_players['Freq'] = cleaport_players['Freq'].astype(int)
|
927 |
nerf_frame = Overall_Proj
|
928 |
+
|
929 |
ref_dict = {
|
930 |
'pos':['RB', 'WR', 'TE', 'FLEX'],
|
931 |
'pos_dfs':['RB_Table', 'WR_Table', 'TE_Table', 'FLEX_Table'],
|
|
|
956 |
'team_check_map':dict(zip(cleaport_players.Player,nerf_frame.Team))
|
957 |
}
|
958 |
|
959 |
+
FinalPortfolio, maps_dict = run_seed_frame(10, Strength_var, strength_grow, Teams_used, 1000000, field_growth)
|
|
|
|
|
960 |
|
961 |
+
Sim_Winners = sim_contest(2500, FinalPortfolio, CleanPortfolio, maps_dict, up_dict, insert_port)
|
|
|
962 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
963 |
# Initial setup
|
964 |
Sim_Winner_Frame = pd.DataFrame(np.concatenate(Sim_Winners), columns=FinalPortfolio.columns.tolist() + ['Fantasy'])
|
965 |
Sim_Winner_Frame['GPP_Proj'] = (Sim_Winner_Frame['Projection'] + Sim_Winner_Frame['Fantasy']) / 2
|
966 |
|
|
|
|
|
967 |
# Type Casting
|
968 |
type_cast_dict = {'Salary': int, 'Projection': np.float16, 'Fantasy': np.float16, 'GPP_Proj': np.float16}
|
969 |
Sim_Winner_Frame = Sim_Winner_Frame.astype(type_cast_dict)
|
970 |
|
971 |
+
del FinalPortfolio, insert_port, type_cast_dict
|
972 |
+
|
973 |
# Sorting
|
974 |
st.session_state.Sim_Winner_Frame = Sim_Winner_Frame.sort_values(by='GPP_Proj', ascending=False)
|
975 |
|
976 |
# Data Copying
|
977 |
+
st.session_state.Sim_Winner_Export = Sim_Winner_Frame
|
|
|
|
|
978 |
|
979 |
# Conditional Replacement
|
980 |
columns_to_replace = ['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST']
|
|
|
984 |
elif site_var1 == 'Fanduel':
|
985 |
replace_dict = fdid_dict
|
986 |
|
|
|
|
|
|
|
987 |
for col in columns_to_replace:
|
988 |
st.session_state.Sim_Winner_Export[col].replace(replace_dict, inplace=True)
|
989 |
+
|
990 |
+
del replace_dict, Sim_Winner_Frame, Sim_Winners
|
991 |
|
992 |
+
st.session_state.player_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,0:9].values, return_counts=True)),
|
993 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
994 |
+
st.session_state.player_freq['Freq'] = st.session_state.player_freq['Freq'].astype(int)
|
995 |
+
st.session_state.player_freq['Position'] = st.session_state.player_freq['Player'].map(maps_dict['Pos_map'])
|
996 |
+
st.session_state.player_freq['Salary'] = st.session_state.player_freq['Player'].map(maps_dict['Salary_map'])
|
997 |
+
st.session_state.player_freq['Proj Own'] = st.session_state.player_freq['Player'].map(maps_dict['Own_map']) / 100
|
998 |
+
st.session_state.player_freq['Exposure'] = st.session_state.player_freq['Freq']/(2500)
|
999 |
+
st.session_state.player_freq['Edge'] = st.session_state.player_freq['Exposure'] - st.session_state.player_freq['Proj Own']
|
1000 |
+
st.session_state.player_freq['Team'] = st.session_state.player_freq['Player'].map(maps_dict['Team_map'])
|
1001 |
for checkVar in range(len(team_list)):
|
1002 |
+
st.session_state.player_freq['Team'] = st.session_state.player_freq['Team'].replace(item_list, team_list)
|
|
|
|
|
|
|
1003 |
|
1004 |
+
st.session_state.qb_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,0:1].values, return_counts=True)),
|
1005 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1006 |
+
st.session_state.qb_freq['Freq'] = st.session_state.qb_freq['Freq'].astype(int)
|
1007 |
+
st.session_state.qb_freq['Position'] = st.session_state.qb_freq['Player'].map(maps_dict['Pos_map'])
|
1008 |
+
st.session_state.qb_freq['Salary'] = st.session_state.qb_freq['Player'].map(maps_dict['Salary_map'])
|
1009 |
+
st.session_state.qb_freq['Proj Own'] = st.session_state.qb_freq['Player'].map(maps_dict['Own_map']) / 100
|
1010 |
+
st.session_state.qb_freq['Exposure'] = st.session_state.qb_freq['Freq']/(2500)
|
1011 |
+
st.session_state.qb_freq['Edge'] = st.session_state.qb_freq['Exposure'] - st.session_state.qb_freq['Proj Own']
|
1012 |
+
st.session_state.qb_freq['Team'] = st.session_state.qb_freq['Player'].map(maps_dict['Team_map'])
|
1013 |
for checkVar in range(len(team_list)):
|
1014 |
+
st.session_state.qb_freq['Team'] = st.session_state.qb_freq['Team'].replace(item_list, team_list)
|
1015 |
|
1016 |
+
st.session_state.rb_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,[1, 2]].values, return_counts=True)),
|
|
|
|
|
|
|
1017 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1018 |
+
st.session_state.rb_freq['Freq'] = st.session_state.rb_freq['Freq'].astype(int)
|
1019 |
+
st.session_state.rb_freq['Position'] = st.session_state.rb_freq['Player'].map(maps_dict['Pos_map'])
|
1020 |
+
st.session_state.rb_freq['Salary'] = st.session_state.rb_freq['Player'].map(maps_dict['Salary_map'])
|
1021 |
+
st.session_state.rb_freq['Proj Own'] = st.session_state.rb_freq['Player'].map(maps_dict['Own_map']) / 100
|
1022 |
+
st.session_state.rb_freq['Exposure'] = st.session_state.rb_freq['Freq']/2500
|
1023 |
+
st.session_state.rb_freq['Edge'] = st.session_state.rb_freq['Exposure'] - st.session_state.rb_freq['Proj Own']
|
1024 |
+
st.session_state.rb_freq['Team'] = st.session_state.rb_freq['Player'].map(maps_dict['Team_map'])
|
1025 |
for checkVar in range(len(team_list)):
|
1026 |
+
st.session_state.rb_freq['Team'] = st.session_state.rb_freq['Team'].replace(item_list, team_list)
|
|
|
|
|
|
|
1027 |
|
1028 |
+
st.session_state.wr_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,[3, 4, 5]].values, return_counts=True)),
|
1029 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1030 |
+
st.session_state.wr_freq['Freq'] = st.session_state.wr_freq['Freq'].astype(int)
|
1031 |
+
st.session_state.wr_freq['Position'] = st.session_state.wr_freq['Player'].map(maps_dict['Pos_map'])
|
1032 |
+
st.session_state.wr_freq['Salary'] = st.session_state.wr_freq['Player'].map(maps_dict['Salary_map'])
|
1033 |
+
st.session_state.wr_freq['Proj Own'] = st.session_state.wr_freq['Player'].map(maps_dict['Own_map']) / 100
|
1034 |
+
st.session_state.wr_freq['Exposure'] = st.session_state.wr_freq['Freq']/2500
|
1035 |
+
st.session_state.wr_freq['Edge'] = st.session_state.wr_freq['Exposure'] - st.session_state.wr_freq['Proj Own']
|
1036 |
+
st.session_state.wr_freq['Team'] = st.session_state.wr_freq['Player'].map(maps_dict['Team_map'])
|
1037 |
for checkVar in range(len(team_list)):
|
1038 |
+
st.session_state.wr_freq['Team'] = st.session_state.wr_freq['Team'].replace(item_list, team_list)
|
|
|
|
|
|
|
1039 |
|
1040 |
+
st.session_state.te_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,[6]].values, return_counts=True)),
|
1041 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1042 |
+
st.session_state.te_freq['Freq'] = st.session_state.te_freq['Freq'].astype(int)
|
1043 |
+
st.session_state.te_freq['Position'] = st.session_state.te_freq['Player'].map(maps_dict['Pos_map'])
|
1044 |
+
st.session_state.te_freq['Salary'] = st.session_state.te_freq['Player'].map(maps_dict['Salary_map'])
|
1045 |
+
st.session_state.te_freq['Proj Own'] = st.session_state.te_freq['Player'].map(maps_dict['Own_map']) / 100
|
1046 |
+
st.session_state.te_freq['Exposure'] = st.session_state.te_freq['Freq']/2500
|
1047 |
+
st.session_state.te_freq['Edge'] = st.session_state.te_freq['Exposure'] - st.session_state.te_freq['Proj Own']
|
1048 |
+
st.session_state.te_freq['Team'] = st.session_state.te_freq['Player'].map(maps_dict['Team_map'])
|
1049 |
for checkVar in range(len(team_list)):
|
1050 |
+
st.session_state.te_freq['Team'] = st.session_state.te_freq['Team'].replace(item_list, team_list)
|
|
|
|
|
|
|
1051 |
|
1052 |
+
st.session_state.flex_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,[7]].values, return_counts=True)),
|
1053 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1054 |
+
st.session_state.flex_freq['Freq'] = st.session_state.flex_freq['Freq'].astype(int)
|
1055 |
+
st.session_state.flex_freq['Position'] = st.session_state.flex_freq['Player'].map(maps_dict['Pos_map'])
|
1056 |
+
st.session_state.flex_freq['Salary'] = st.session_state.flex_freq['Player'].map(maps_dict['Salary_map'])
|
1057 |
+
st.session_state.flex_freq['Proj Own'] = st.session_state.flex_freq['Player'].map(maps_dict['Own_map']) / 100
|
1058 |
+
st.session_state.flex_freq['Exposure'] = st.session_state.flex_freq['Freq']/2500
|
1059 |
+
st.session_state.flex_freq['Edge'] = st.session_state.flex_freq['Exposure'] - st.session_state.flex_freq['Proj Own']
|
1060 |
+
st.session_state.flex_freq['Team'] = st.session_state.flex_freq['Player'].map(maps_dict['Team_map'])
|
1061 |
for checkVar in range(len(team_list)):
|
1062 |
+
st.session_state.flex_freq['Team'] = st.session_state.flex_freq['Team'].replace(item_list, team_list)
|
|
|
|
|
|
|
1063 |
|
1064 |
+
st.session_state.dst_freq = pd.DataFrame(np.column_stack(np.unique(st.session_state.Sim_Winner_Frame.iloc[:,8:9].values, return_counts=True)),
|
1065 |
columns=['Player','Freq']).sort_values('Freq', ascending=False).reset_index(drop=True)
|
1066 |
+
st.session_state.dst_freq['Freq'] = st.session_state.dst_freq['Freq'].astype(int)
|
1067 |
+
st.session_state.dst_freq['Position'] = st.session_state.dst_freq['Player'].map(maps_dict['Pos_map'])
|
1068 |
+
st.session_state.dst_freq['Salary'] = st.session_state.dst_freq['Player'].map(maps_dict['Salary_map'])
|
1069 |
+
st.session_state.dst_freq['Proj Own'] = st.session_state.dst_freq['Player'].map(maps_dict['Own_map']) / 100
|
1070 |
+
st.session_state.dst_freq['Exposure'] = st.session_state.dst_freq['Freq']/2500
|
1071 |
+
st.session_state.dst_freq['Edge'] = st.session_state.dst_freq['Exposure'] - st.session_state.dst_freq['Proj Own']
|
1072 |
+
st.session_state.dst_freq['Team'] = st.session_state.dst_freq['Player'].map(maps_dict['Team_map'])
|
1073 |
for checkVar in range(len(team_list)):
|
1074 |
+
st.session_state.dst_freq['Team'] = st.session_state.dst_freq['Team'].replace(item_list, team_list)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1075 |
|
1076 |
with st.container():
|
|
|
1077 |
if 'player_freq' in st.session_state:
|
1078 |
player_split_var2 = st.radio("Are you wanting to isolate any lineups with specific players?", ('Full Players', 'Specific Players'), key='player_split_var2')
|
1079 |
if player_split_var2 == 'Specific Players':
|
|
|
1082 |
find_var2 = st.session_state.player_freq.Player.values.tolist()
|
1083 |
|
1084 |
if player_split_var2 == 'Specific Players':
|
1085 |
+
st.session_state.Sim_Winner_Display = st.session_state.Sim_Winner_Frame[np.equal.outer(st.session_state.Sim_Winner_Frame.to_numpy(), find_var2).any(axis=1).all(axis=1)]
|
1086 |
if player_split_var2 == 'Full Players':
|
1087 |
st.session_state.Sim_Winner_Display = st.session_state.Sim_Winner_Frame
|
1088 |
if 'Sim_Winner_Display' in st.session_state:
|
|
|
1090 |
if 'Sim_Winner_Export' in st.session_state:
|
1091 |
st.download_button(
|
1092 |
label="Export Tables",
|
1093 |
+
data=st.session_state.Sim_Winner_Export.to_csv().encode('utf-8'),
|
1094 |
file_name='NFL_consim_export.csv',
|
1095 |
mime='text/csv',
|
1096 |
)
|
1097 |
|
1098 |
with st.container():
|
|
|
1099 |
tab1, tab2, tab3, tab4, tab5, tab6, tab7 = st.tabs(['Overall Exposures', 'QB Exposures', 'RB Exposures', 'WR Exposures', 'TE Exposures', 'FLEX Exposures', 'DST Exposures'])
|
1100 |
with tab1:
|
1101 |
if 'player_freq' in st.session_state:
|
1102 |
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)
|
1103 |
st.download_button(
|
1104 |
label="Export Exposures",
|
1105 |
+
data=st.session_state.player_freq.to_csv().encode('utf-8'),
|
1106 |
file_name='player_freq_export.csv',
|
1107 |
mime='text/csv',
|
1108 |
)
|
|
|
1111 |
st.dataframe(st.session_state.qb_freq.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(freq_format, precision=2), use_container_width = True)
|
1112 |
st.download_button(
|
1113 |
label="Export Exposures",
|
1114 |
+
data=st.session_state.qb_freq.to_csv().encode('utf-8'),
|
1115 |
file_name='qb_freq_export.csv',
|
1116 |
mime='text/csv',
|
1117 |
)
|
|
|
1120 |
st.dataframe(st.session_state.rb_freq.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(freq_format, precision=2), use_container_width = True)
|
1121 |
st.download_button(
|
1122 |
label="Export Exposures",
|
1123 |
+
data=st.session_state.rb_freq.to_csv().encode('utf-8'),
|
1124 |
file_name='rb_freq_export.csv',
|
1125 |
mime='text/csv',
|
1126 |
)
|
|
|
1129 |
st.dataframe(st.session_state.wr_freq.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(freq_format, precision=2), use_container_width = True)
|
1130 |
st.download_button(
|
1131 |
label="Export Exposures",
|
1132 |
+
data=st.session_state.wr_freq.to_csv().encode('utf-8'),
|
1133 |
file_name='wr_freq_export.csv',
|
1134 |
mime='text/csv',
|
1135 |
)
|
|
|
1138 |
st.dataframe(st.session_state.te_freq.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(freq_format, precision=2), use_container_width = True)
|
1139 |
st.download_button(
|
1140 |
label="Export Exposures",
|
1141 |
+
data=st.session_state.te_freq.to_csv().encode('utf-8'),
|
1142 |
file_name='te_freq_export.csv',
|
1143 |
mime='text/csv',
|
1144 |
)
|
|
|
1147 |
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)
|
1148 |
st.download_button(
|
1149 |
label="Export Exposures",
|
1150 |
+
data=st.session_state.flex_freq.to_csv().encode('utf-8'),
|
1151 |
file_name='flex_freq_export.csv',
|
1152 |
mime='text/csv',
|
1153 |
)
|
|
|
1156 |
st.dataframe(st.session_state.dst_freq.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(freq_format, precision=2), use_container_width = True)
|
1157 |
st.download_button(
|
1158 |
label="Export Exposures",
|
1159 |
+
data=st.session_state.dst_freq.to_csv().encode('utf-8'),
|
1160 |
file_name='dst_freq_export.csv',
|
1161 |
mime='text/csv',
|
1162 |
+
)
|
1163 |
+
|
1164 |
+
del gcservice_account
|
1165 |
+
del dk_roo_raw, fd_roo_raw
|
1166 |
+
del t_stamp
|
1167 |
+
del dkid_dict, fdid_dict
|
1168 |
+
del static_exposure, overall_exposure
|
1169 |
+
del insert_port1, Contest_Size, sharp_split, Strength_var, scaling_var, Sort_function, Sim_function, strength_grow, field_growth
|
1170 |
+
del raw_baselines
|
1171 |
+
del freq_format
|