James McCool commited on
Commit
383a505
·
1 Parent(s): 90b5b34

Add NHL support to ROO build functions and Streamlit display

Browse files
Files changed (2) hide show
  1. app.py +9 -0
  2. function_hold/NHL_functions.py +483 -0
app.py CHANGED
@@ -19,8 +19,10 @@ from pandas import DataFrame
19
  #bring in functions
20
  from function_hold.NBA_functions import DK_NBA_ROO_Build, FD_NBA_ROO_Build
21
  from function_hold.MMA_functions import DK_MMA_ROO_Build, FD_MMA_ROO_Build
 
22
 
23
  nba_percentages_format = {'Top_finish': '{:.2%}', 'Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}', '20+%': '{:.2%}', '4x%': '{:.2%}', '5x%': '{:.2%}', '6x%': '{:.2%}', 'GPP%': '{:.2%}'}
 
24
  mma_percentages_format = {'Top_finish': '{:.2%}', 'Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}', '100+%': '{:.2%}', '10x%': '{:.2%}', '11x%': '{:.2%}', '12x%': '{:.2%}', 'GPP%': '{:.2%}'}
25
 
26
  def load_file(upload):
@@ -110,6 +112,11 @@ with tab2:
110
  disp_file = DK_NBA_ROO_Build(projections, floor_var_sb, ceiling_var_sb, std_var_sb, distribution_type_sb)
111
  elif site_var_sb == "Fanduel":
112
  disp_file = FD_NBA_ROO_Build(projections, floor_var_sb, ceiling_var_sb, std_var_sb, distribution_type_sb)
 
 
 
 
 
113
  elif sport_var == "NFL":
114
  if site_var_sb == "Draftkings":
115
  disp_file = DK_NFL_ROO_Build(projections, floor_var_sb, ceiling_var_sb, std_var_sb, distribution_type_sb)
@@ -130,6 +137,8 @@ with tab2:
130
  if 'disp_file' in locals():
131
  if sport_var == "NBA":
132
  st.dataframe(disp_file.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(nba_percentages_format, precision=2), height=1000, use_container_width = True)
 
 
133
  elif sport_var == "MMA":
134
  st.dataframe(disp_file.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(mma_percentages_format, precision=2), height=1000, use_container_width = True)
135
  except:
 
19
  #bring in functions
20
  from function_hold.NBA_functions import DK_NBA_ROO_Build, FD_NBA_ROO_Build
21
  from function_hold.MMA_functions import DK_MMA_ROO_Build, FD_MMA_ROO_Build
22
+ from function_hold.NHL_functions import DK_NHL_ROO_Build, FD_NHL_ROO_Build
23
 
24
  nba_percentages_format = {'Top_finish': '{:.2%}', 'Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}', '20+%': '{:.2%}', '4x%': '{:.2%}', '5x%': '{:.2%}', '6x%': '{:.2%}', 'GPP%': '{:.2%}'}
25
+ nhl_percentages_format = {'Top_finish': '{:.2%}', 'Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}', '20+%': '{:.2%}', '4x%': '{:.2%}', '5x%': '{:.2%}', '6x%': '{:.2%}', 'GPP%': '{:.2%}'}
26
  mma_percentages_format = {'Top_finish': '{:.2%}', 'Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}', '100+%': '{:.2%}', '10x%': '{:.2%}', '11x%': '{:.2%}', '12x%': '{:.2%}', 'GPP%': '{:.2%}'}
27
 
28
  def load_file(upload):
 
112
  disp_file = DK_NBA_ROO_Build(projections, floor_var_sb, ceiling_var_sb, std_var_sb, distribution_type_sb)
113
  elif site_var_sb == "Fanduel":
114
  disp_file = FD_NBA_ROO_Build(projections, floor_var_sb, ceiling_var_sb, std_var_sb, distribution_type_sb)
115
+ elif sport_var == "NHL":
116
+ if site_var_sb == "Draftkings":
117
+ disp_file = DK_NHL_ROO_Build(projections, floor_var_sb, ceiling_var_sb, std_var_sb, distribution_type_sb)
118
+ elif site_var_sb == "Fanduel":
119
+ disp_file = FD_NHL_ROO_Build(projections, floor_var_sb, ceiling_var_sb, std_var_sb, distribution_type_sb)
120
  elif sport_var == "NFL":
121
  if site_var_sb == "Draftkings":
122
  disp_file = DK_NFL_ROO_Build(projections, floor_var_sb, ceiling_var_sb, std_var_sb, distribution_type_sb)
 
137
  if 'disp_file' in locals():
138
  if sport_var == "NBA":
139
  st.dataframe(disp_file.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(nba_percentages_format, precision=2), height=1000, use_container_width = True)
140
+ elif sport_var == "NHL":
141
+ st.dataframe(disp_file.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(nhl_percentages_format, precision=2), height=1000, use_container_width = True)
142
  elif sport_var == "MMA":
143
  st.dataframe(disp_file.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(mma_percentages_format, precision=2), height=1000, use_container_width = True)
144
  except:
function_hold/NHL_functions.py ADDED
@@ -0,0 +1,483 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from numpy import nan as np_nan
2
+ from numpy import where as np_where
3
+ from numpy import random as np_random
4
+ from numpy import zeros as np_zeros
5
+ from numpy import array as np_array
6
+ from pandas import concat as pd_concat
7
+ from pandas import merge as pd_merge
8
+ from pandas import DataFrame
9
+
10
+ def DK_NHL_ROO_Build(projections_file, floor_var, ceiling_var, std_var, distribution_type):
11
+ total_sims = 1000
12
+
13
+ projects_raw = projections_file.copy()
14
+ projects_raw = projects_raw.replace("", np_nan)
15
+ dk_df = projects_raw.sort_values(by='Median', ascending=False)
16
+
17
+ basic_own_df = dk_df.copy()
18
+ basic_own_df['name_team'] = basic_own_df['Player'] + basic_own_df['Position']
19
+
20
+ def calculate_ownership(df, position):
21
+ # Filter the dataframe based on the position
22
+ frame = df[df['Position'].str.contains(position)]
23
+
24
+ # Calculate Small Field Own%
25
+ frame['Base Own%'] = np_where(
26
+ (frame['Own'] - frame['Own'].mean() >= 0),
27
+ frame['Own'] * (5 * (frame['Own'] - (frame['Own'].mean() / 1.5)) / 100) + frame['Own'].mean(),
28
+ frame['Own']
29
+ )
30
+ frame['Base Own%'] = np_where(
31
+ frame['Base Own%'] > 75,
32
+ 75,
33
+ frame['Base Own%']
34
+ )
35
+
36
+ # Calculate Small Field Own%
37
+ frame['Small Field Own%'] = np_where(
38
+ (frame['Own'] - frame['Own'].mean() >= 0),
39
+ frame['Own'] * (6 * (frame['Own'] - frame['Own'].mean()) / 100) + frame['Own'].mean(),
40
+ frame['Own']
41
+ )
42
+ frame['Small Field Own%'] = np_where(
43
+ frame['Small Field Own%'] > 75,
44
+ 75,
45
+ frame['Small Field Own%']
46
+ )
47
+
48
+ # Calculate Large Field Own%
49
+ frame['Large Field Own%'] = np_where(
50
+ (frame['Own'] - frame['Own'].mean() >= 0),
51
+ frame['Own'] * (2.5 * (frame['Own'] - frame['Own'].mean()) / 100) + frame['Own'].mean(),
52
+ frame['Own']
53
+ )
54
+ frame['Large Field Own%'] = np_where(
55
+ frame['Large Field Own%'] > 75,
56
+ 75,
57
+ frame['Large Field Own%']
58
+ )
59
+
60
+ # Calculate Cash Own%
61
+ frame['Cash Own%'] = np_where(
62
+ (frame['Own'] - frame['Own'].mean() >= 0),
63
+ frame['Own'] * (8 * (frame['Own'] - frame['Own'].mean()) / 100) + frame['Own'].mean(),
64
+ frame['Own']
65
+ )
66
+ frame['Cash Own%'] = np_where(
67
+ frame['Cash Own%'] > 75,
68
+ 75,
69
+ frame['Cash Own%']
70
+ )
71
+
72
+ return frame
73
+
74
+ # Apply the function to each dataframe
75
+ w_frame = calculate_ownership(basic_own_df, 'W')
76
+ c_frame = calculate_ownership(basic_own_df, 'C')
77
+ d_frame = calculate_ownership(basic_own_df, 'D')
78
+ g_frame = calculate_ownership(basic_own_df, 'G')
79
+
80
+ w_reg_norm_var = 330 / w_frame['Base Own%'].sum()
81
+ w_small_norm_var = 330 / w_frame['Small Field Own%'].sum()
82
+ w_large_norm_var = 330 / w_frame['Large Field Own%'].sum()
83
+ w_cash_norm_var = 330 / w_frame['Cash Own%'].sum()
84
+ w_frame['Own'] = w_frame['Base Own%'] * w_reg_norm_var
85
+ w_frame['Small Field Own%'] = w_frame['Small Field Own%'] * w_small_norm_var
86
+ w_frame['Large Field Own%'] = w_frame['Large Field Own%'] * w_large_norm_var
87
+ w_frame['Cash Own%'] = w_frame['Cash Own%'] * w_cash_norm_var
88
+
89
+ c_reg_norm_var = 260 / c_frame['Base Own%'].sum()
90
+ c_small_norm_var = 260 / c_frame['Small Field Own%'].sum()
91
+ c_large_norm_var = 260 / c_frame['Large Field Own%'].sum()
92
+ c_cash_norm_var = 260 / c_frame['Cash Own%'].sum()
93
+ c_frame['Own'] = c_frame['Base Own%'] * c_reg_norm_var
94
+ c_frame['Small Field Own%'] = c_frame['Small Field Own%'] * c_small_norm_var
95
+ c_frame['Large Field Own%'] = c_frame['Large Field Own%'] * c_large_norm_var
96
+ c_frame['Cash Own%'] = c_frame['Cash Own%'] * c_cash_norm_var
97
+
98
+ d_reg_norm_var = 210 / d_frame['Base Own%'].sum()
99
+ d_small_norm_var = 210 / d_frame['Small Field Own%'].sum()
100
+ d_large_norm_var = 210 / d_frame['Large Field Own%'].sum()
101
+ d_cash_norm_var = 210 / d_frame['Cash Own%'].sum()
102
+ d_frame['Own'] = d_frame['Base Own%'] * d_reg_norm_var
103
+ d_frame['Small Field Own%'] = d_frame['Small Field Own%'] * d_small_norm_var
104
+ d_frame['Large Field Own%'] = d_frame['Large Field Own%'] * d_large_norm_var
105
+ d_frame['Cash Own%'] = d_frame['Cash Own%'] * d_cash_norm_var
106
+
107
+ g_reg_norm_var = 100 / g_frame['Base Own%'].sum()
108
+ g_small_norm_var = 100 / g_frame['Small Field Own%'].sum()
109
+ g_large_norm_var = 100 / g_frame['Large Field Own%'].sum()
110
+ g_cash_norm_var = 100 / g_frame['Cash Own%'].sum()
111
+ g_frame['Own'] = g_frame['Base Own%'] * g_reg_norm_var
112
+ g_frame['Small Field Own%'] = g_frame['Small Field Own%'] * g_small_norm_var
113
+ g_frame['Large Field Own%'] = g_frame['Large Field Own%'] * g_large_norm_var
114
+ g_frame['Cash Own%'] = g_frame['Cash Own%'] * g_cash_norm_var
115
+
116
+ basic_own_df = pd_concat([w_frame, c_frame, d_frame, g_frame])
117
+
118
+ basic_own_dict = dict(zip(basic_own_df.Player, basic_own_df.Own))
119
+ small_own_dict = dict(zip(basic_own_df.Player, basic_own_df['Small Field Own%']))
120
+ large_own_dict = dict(zip(basic_own_df.Player, basic_own_df['Large Field Own%']))
121
+ cash_own_dict = dict(zip(basic_own_df.Player, basic_own_df['Cash Own%']))
122
+ basic_team_dict = dict(zip(basic_own_df.name_team, basic_own_df.Team))
123
+ basic_opp_dict = dict(zip(basic_own_df.Player, basic_own_df.Opp))
124
+
125
+ flex_file = basic_own_df.copy()
126
+ flex_file['Floor_raw'] = flex_file['Median'] * .25
127
+ flex_file['Ceiling_raw'] = flex_file['Median'] * 2
128
+ flex_file['Floor'] = np_where(flex_file['Position'] == 'G', flex_file['Median'] * .5, flex_file['Floor_raw'])
129
+ flex_file['Floor'] = np_where(flex_file['Position'] == 'D', flex_file['Median'] * .1, flex_file['Floor_raw'])
130
+ flex_file['Ceiling'] = np_where(flex_file['Position'] == 'G', flex_file['Median'] * 1.75, flex_file['Ceiling_raw'])
131
+ flex_file['Ceiling'] = np_where(flex_file['Position'] == 'D', flex_file['Median'] * 1.75, flex_file['Ceiling_raw'])
132
+ flex_file['STD'] = flex_file['Median'] / 3
133
+ flex_file = flex_file[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD']]
134
+ flex_file = flex_file.reset_index(drop=True)
135
+ hold_file = flex_file.copy()
136
+ overall_file = flex_file.copy()
137
+ salary_file = flex_file.copy()
138
+
139
+ try:
140
+ overall_floor_gpu = np_array(overall_file['Floor'])
141
+ overall_ceiling_gpu = np_array(overall_file['Ceiling'])
142
+ overall_median_gpu = np_array(overall_file['Median'])
143
+ overall_std_gpu = np_array(overall_file['STD'])
144
+ overall_salary_gpu = np_array(overall_file['Salary'])
145
+
146
+ data_shape = (len(overall_file['Player']), total_sims) # Example: 1000 rows
147
+ salary_array = np_zeros(data_shape)
148
+ sim_array = np_zeros(data_shape)
149
+
150
+ for x in range(0, total_sims):
151
+ result_gpu = overall_salary_gpu
152
+ salary_array[:, x] = result_gpu
153
+ cupy_array = salary_array
154
+
155
+ salary_file = salary_file.reset_index(drop=True)
156
+ salary_cupy = DataFrame(cupy_array, columns=list(range(0, total_sims)))
157
+ salary_check_file = pd_concat([salary_file, salary_cupy], axis=1)
158
+ except:
159
+ for x in range(0,total_sims):
160
+ salary_file[x] = salary_file['Salary']
161
+ salary_check_file = salary_file.copy()
162
+
163
+ salary_file=salary_check_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
164
+
165
+ salary_file = salary_file.div(1000)
166
+
167
+ try:
168
+ for x in range(0, total_sims):
169
+ if distribution_type == 'normal':
170
+ # Normal distribution (existing logic)
171
+ result_gpu = np_random.normal(overall_median_gpu, overall_std_gpu)
172
+ elif distribution_type == 'poisson':
173
+ # Poisson distribution - using median as lambda
174
+ result_gpu = np_random.poisson(overall_median_gpu)
175
+ elif distribution_type == 'bimodal':
176
+ # Bimodal distribution - mixture of two normal distributions
177
+ # First peak centered at 80% of median, second at 120% of median
178
+ if np_random.random() < 0.5:
179
+ result_gpu = np_random.normal(overall_floor_gpu, overall_std_gpu)
180
+ else:
181
+ result_gpu = np_random.normal(overall_ceiling_gpu, overall_std_gpu)
182
+ else:
183
+ raise ValueError("Invalid distribution type. Must be 'normal', 'poisson', or 'bimodal'")
184
+
185
+ sim_array[:, x] = result_gpu
186
+ add_array = sim_array
187
+
188
+ overall_file = overall_file.reset_index(drop=True)
189
+ df2 = DataFrame(add_array, columns=list(range(0, total_sims)))
190
+ check_file = pd_concat([overall_file, df2], axis=1)
191
+ except:
192
+ for x in range(0,total_sims):
193
+ if distribution_type == 'normal':
194
+ overall_file[x] = np_random.normal(overall_file['Median'], overall_file['STD'])
195
+ elif distribution_type == 'poisson':
196
+ overall_file[x] = np_random.poisson(overall_file['Median'])
197
+ elif distribution_type == 'bimodal':
198
+ # Bimodal distribution fallback
199
+ if np_random.random() < 0.5:
200
+ overall_file[x] = np_random.normal(overall_file['Median'] * 0.8, overall_file['STD'])
201
+ else:
202
+ overall_file[x] = np_random.normal(overall_file['Median'] * 1.2, overall_file['STD'])
203
+ check_file = overall_file.copy()
204
+
205
+ overall_file=check_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
206
+
207
+ players_only = hold_file[['Player']]
208
+ raw_lineups_file = players_only
209
+
210
+ for x in range(0,total_sims):
211
+ maps_dict = {'proj_map':dict(zip(hold_file.Player,overall_file[x]))}
212
+ raw_lineups_file[x] = sum([raw_lineups_file['Player'].map(maps_dict['proj_map'])])
213
+ players_only[x] = raw_lineups_file[x].rank(ascending=False)
214
+
215
+ players_only=players_only.drop(['Player'], axis=1)
216
+
217
+ salary_2x_check = (overall_file - (salary_file*2))
218
+ salary_3x_check = (overall_file - (salary_file*3))
219
+ salary_4x_check = (overall_file - (salary_file*4))
220
+
221
+ players_only['Average_Rank'] = players_only.mean(axis=1)
222
+ players_only['Top_finish'] = players_only[players_only == 1].count(axis=1)/total_sims
223
+ players_only['Top_5_finish'] = players_only[players_only <= 5].count(axis=1)/total_sims
224
+ players_only['Top_10_finish'] = players_only[players_only <= 10].count(axis=1)/total_sims
225
+ players_only['20+%'] = overall_file[overall_file >= 20].count(axis=1)/float(total_sims)
226
+ players_only['2x%'] = salary_2x_check[salary_2x_check >= 1].count(axis=1)/float(total_sims)
227
+ players_only['3x%'] = salary_3x_check[salary_3x_check >= 1].count(axis=1)/float(total_sims)
228
+ players_only['4x%'] = salary_4x_check[salary_4x_check >= 1].count(axis=1)/float(total_sims)
229
+
230
+ players_only['Player'] = hold_file[['Player']]
231
+
232
+ final_outcomes = players_only[['Player', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%']]
233
+
234
+ final_Proj = pd_merge(hold_file, final_outcomes, on="Player")
235
+ final_Proj = final_Proj[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%']]
236
+ final_Proj['Own'] = final_Proj['Player'].map(basic_own_dict).astype(float)
237
+ final_Proj['Small Field Own%'] = final_Proj['Player'].map(small_own_dict).astype(float)
238
+ final_Proj['Large Field Own%'] = final_Proj['Player'].map(large_own_dict).astype(float)
239
+ final_Proj['Cash Own%'] = final_Proj['Player'].map(cash_own_dict).astype(float)
240
+ final_Proj['name_team'] = final_Proj['Player'] + final_Proj['Position']
241
+ final_Proj['Team'] = final_Proj['name_team'].map(basic_team_dict)
242
+ final_Proj['Opp'] = final_Proj['Player'].map(basic_opp_dict)
243
+ final_Proj = final_Proj[['Player', 'Position', 'Team', 'Opp', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'Own', 'Small Field Own%', 'Large Field Own%', 'Cash Own%']]
244
+ final_Proj = final_Proj.sort_values(by='Median', ascending=False)
245
+
246
+ return final_Proj.copy()
247
+
248
+ def FD_NHL_ROO_Build(projections_file, floor_var, ceiling_var, std_var, distribution_type):
249
+ total_sims = 1000
250
+
251
+ projects_raw = projections_file.copy()
252
+ fd_df = projects_raw.sort_values(by='Median', ascending=False)
253
+
254
+ basic_own_df = fd_df.copy()
255
+ basic_own_df['name_team'] = basic_own_df['Player'] + basic_own_df['Position']
256
+
257
+ def calculate_ownership(df, position):
258
+ # Filter the dataframe based on the position
259
+ frame = df[df['Position'].str.contains(position)]
260
+
261
+ frame['Base Own%'] = np_where(
262
+ (frame['Own'] - frame['Own'].mean() >= 0),
263
+ frame['Own'] * (5 * (frame['Own'] - (frame['Own'].mean() / 1.5)) / 100) + frame['Own'].mean(),
264
+ frame['Own']
265
+ )
266
+ frame['Base Own%'] = np_where(
267
+ frame['Base Own%'] > 75,
268
+ 75,
269
+ frame['Base Own%']
270
+ )
271
+
272
+ # Calculate Small Field Own%
273
+ frame['Small Field Own%'] = np_where(
274
+ (frame['Own'] - frame['Own'].mean() >= 0),
275
+ frame['Own'] * (6 * (frame['Own'] - frame['Own'].mean()) / 100) + frame['Own'].mean(),
276
+ frame['Own']
277
+ )
278
+ frame['Small Field Own%'] = np_where(
279
+ frame['Small Field Own%'] > 75,
280
+ 75,
281
+ frame['Small Field Own%']
282
+ )
283
+
284
+ # Calculate Large Field Own%
285
+ frame['Large Field Own%'] = np_where(
286
+ (frame['Own'] - frame['Own'].mean() >= 0),
287
+ frame['Own'] * (2.5 * (frame['Own'] - frame['Own'].mean()) / 100) + frame['Own'].mean(),
288
+ frame['Own']
289
+ )
290
+ frame['Large Field Own%'] = np_where(
291
+ frame['Large Field Own%'] > 75,
292
+ 75,
293
+ frame['Large Field Own%']
294
+ )
295
+
296
+ # Calculate Cash Own%
297
+ frame['Cash Own%'] = np_where(
298
+ (frame['Own'] - frame['Own'].mean() >= 0),
299
+ frame['Own'] * (8 * (frame['Own'] - frame['Own'].mean()) / 100) + frame['Own'].mean(),
300
+ frame['Own']
301
+ )
302
+ frame['Cash Own%'] = np_where(
303
+ frame['Cash Own%'] > 75,
304
+ 75,
305
+ frame['Cash Own%']
306
+ )
307
+
308
+ return frame
309
+
310
+ # Apply the function to each dataframe
311
+ w_frame = calculate_ownership(basic_own_df, 'W')
312
+ c_frame = calculate_ownership(basic_own_df, 'C')
313
+ d_frame = calculate_ownership(basic_own_df, 'D')
314
+ g_frame = calculate_ownership(basic_own_df, 'G')
315
+
316
+ w_reg_norm_var = 295 / w_frame['Base Own%'].sum()
317
+ w_small_norm_var = 295 / w_frame['Small Field Own%'].sum()
318
+ w_large_norm_var = 295 / w_frame['Large Field Own%'].sum()
319
+ w_cash_norm_var = 295 / w_frame['Cash Own%'].sum()
320
+ w_frame['Own'] = w_frame['Base Own%'] * w_reg_norm_var
321
+ w_frame['Small Field Own%'] = w_frame['Small Field Own%'] * w_small_norm_var
322
+ w_frame['Large Field Own%'] = w_frame['Large Field Own%'] * w_large_norm_var
323
+ w_frame['Cash Own%'] = w_frame['Cash Own%'] * w_cash_norm_var
324
+
325
+ c_reg_norm_var = 295 / c_frame['Base Own%'].sum()
326
+ c_small_norm_var = 295 / c_frame['Small Field Own%'].sum()
327
+ c_large_norm_var = 295 / c_frame['Large Field Own%'].sum()
328
+ c_cash_norm_var = 295 / c_frame['Cash Own%'].sum()
329
+ c_frame['Own'] = c_frame['Base Own%'] * c_reg_norm_var
330
+ c_frame['Small Field Own%'] = c_frame['Small Field Own%'] * c_small_norm_var
331
+ c_frame['Large Field Own%'] = c_frame['Large Field Own%'] * c_large_norm_var
332
+ c_frame['Cash Own%'] = c_frame['Cash Own%'] * c_cash_norm_var
333
+
334
+ d_reg_norm_var = 210 / d_frame['Base Own%'].sum()
335
+ d_small_norm_var = 210 / d_frame['Small Field Own%'].sum()
336
+ d_large_norm_var = 210 / d_frame['Large Field Own%'].sum()
337
+ d_cash_norm_var = 210 / d_frame['Cash Own%'].sum()
338
+ d_frame['Own'] = d_frame['Base Own%'] * d_reg_norm_var
339
+ d_frame['Small Field Own%'] = d_frame['Small Field Own%'] * d_small_norm_var
340
+ d_frame['Large Field Own%'] = d_frame['Large Field Own%'] * d_large_norm_var
341
+ d_frame['Cash Own%'] = d_frame['Cash Own%'] * d_cash_norm_var
342
+
343
+ g_reg_norm_var = 100 / g_frame['Base Own%'].sum()
344
+ g_small_norm_var = 100 / g_frame['Small Field Own%'].sum()
345
+ g_large_norm_var = 100 / g_frame['Large Field Own%'].sum()
346
+ g_cash_norm_var = 100 / g_frame['Cash Own%'].sum()
347
+ g_frame['Own'] = g_frame['Base Own%'] * g_reg_norm_var
348
+ g_frame['Small Field Own%'] = g_frame['Small Field Own%'] * g_small_norm_var
349
+ g_frame['Large Field Own%'] = g_frame['Large Field Own%'] * g_large_norm_var
350
+ g_frame['Cash Own%'] = g_frame['Cash Own%'] * g_cash_norm_var
351
+
352
+ basic_own_df = pd_concat([w_frame, c_frame, d_frame, g_frame])
353
+
354
+ basic_own_dict = dict(zip(basic_own_df.Player, basic_own_df.Own))
355
+ small_own_dict = dict(zip(basic_own_df.Player, basic_own_df['Small Field Own%']))
356
+ large_own_dict = dict(zip(basic_own_df.Player, basic_own_df['Large Field Own%']))
357
+ cash_own_dict = dict(zip(basic_own_df.Player, basic_own_df['Cash Own%']))
358
+ basic_team_dict = dict(zip(basic_own_df.name_team, basic_own_df.Team))
359
+ basic_opp_dict = dict(zip(basic_own_df.Player, basic_own_df.Opp))
360
+
361
+ flex_file = basic_own_df.copy()
362
+ flex_file['Floor_raw'] = flex_file['Median'] * .25
363
+ flex_file['Ceiling_raw'] = flex_file['Median'] * 2
364
+ flex_file['Floor'] = np_where(flex_file['Position'] == 'G', flex_file['Median'] * .5, flex_file['Floor_raw'])
365
+ flex_file['Floor'] = np_where(flex_file['Position'] == 'D', flex_file['Median'] * .1, flex_file['Floor_raw'])
366
+ flex_file['Ceiling'] = np_where(flex_file['Position'] == 'G', flex_file['Median'] * 1.75, flex_file['Ceiling_raw'])
367
+ flex_file['Ceiling'] = np_where(flex_file['Position'] == 'D', flex_file['Median'] * 1.75, flex_file['Ceiling_raw'])
368
+ flex_file['STD'] = flex_file['Median'] / 3
369
+ flex_file = flex_file[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD']]
370
+ flex_file = flex_file.reset_index(drop=True)
371
+ hold_file = flex_file.copy()
372
+ overall_file = flex_file.copy()
373
+ salary_file = flex_file.copy()
374
+
375
+ try:
376
+ overall_floor_gpu = np_array(overall_file['Floor'])
377
+ overall_ceiling_gpu = np_array(overall_file['Ceiling'])
378
+ overall_median_gpu = np_array(overall_file['Median'])
379
+ overall_std_gpu = np_array(overall_file['STD'])
380
+ overall_salary_gpu = np_array(overall_file['Salary'])
381
+
382
+ data_shape = (len(overall_file['Player']), total_sims) # Example: 1000 rows
383
+ salary_array = np_zeros(data_shape)
384
+ sim_array = np_zeros(data_shape)
385
+
386
+ for x in range(0, total_sims):
387
+ result_gpu = overall_salary_gpu
388
+ salary_array[:, x] = result_gpu
389
+ cupy_array = salary_array
390
+
391
+ salary_file = salary_file.reset_index(drop=True)
392
+ salary_cupy = DataFrame(cupy_array, columns=list(range(0, total_sims)))
393
+ salary_check_file = pd_concat([salary_file, salary_cupy], axis=1)
394
+ except:
395
+ for x in range(0,total_sims):
396
+ salary_file[x] = salary_file['Salary']
397
+ salary_check_file = salary_file.copy()
398
+
399
+ salary_file=salary_check_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
400
+
401
+ salary_file = salary_file.div(1000)
402
+
403
+ try:
404
+ for x in range(0, total_sims):
405
+ if distribution_type == 'normal':
406
+ # Normal distribution (existing logic)
407
+ result_gpu = np_random.normal(overall_median_gpu, overall_std_gpu)
408
+ elif distribution_type == 'poisson':
409
+ # Poisson distribution - using median as lambda
410
+ result_gpu = np_random.poisson(overall_median_gpu)
411
+ elif distribution_type == 'bimodal':
412
+ # Bimodal distribution - mixture of two normal distributions
413
+ # First peak centered at 80% of median, second at 120% of median
414
+ if np_random.random() < 0.5:
415
+ result_gpu = np_random.normal(overall_floor_gpu, overall_std_gpu)
416
+ else:
417
+ result_gpu = np_random.normal(overall_ceiling_gpu, overall_std_gpu)
418
+ else:
419
+ raise ValueError("Invalid distribution type. Must be 'normal', 'poisson', or 'bimodal'")
420
+
421
+ sim_array[:, x] = result_gpu
422
+ add_array = sim_array
423
+
424
+ overall_file = overall_file.reset_index(drop=True)
425
+ df2 = DataFrame(add_array, columns=list(range(0, total_sims)))
426
+ check_file = pd_concat([overall_file, df2], axis=1)
427
+ except:
428
+ for x in range(0,total_sims):
429
+ if distribution_type == 'normal':
430
+ overall_file[x] = np_random.normal(overall_file['Median'], overall_file['STD'])
431
+ elif distribution_type == 'poisson':
432
+ overall_file[x] = np_random.poisson(overall_file['Median'])
433
+ elif distribution_type == 'bimodal':
434
+ # Bimodal distribution fallback
435
+ if np_random.random() < 0.5:
436
+ overall_file[x] = np_random.normal(overall_file['Median'] * 0.8, overall_file['STD'])
437
+ else:
438
+ overall_file[x] = np_random.normal(overall_file['Median'] * 1.2, overall_file['STD'])
439
+ check_file = overall_file.copy()
440
+
441
+ overall_file=check_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
442
+
443
+ players_only = hold_file[['Player']]
444
+ raw_lineups_file = players_only
445
+
446
+ for x in range(0,total_sims):
447
+ maps_dict = {'proj_map':dict(zip(hold_file.Player,overall_file[x]))}
448
+ raw_lineups_file[x] = sum([raw_lineups_file['Player'].map(maps_dict['proj_map'])])
449
+ players_only[x] = raw_lineups_file[x].rank(ascending=False)
450
+
451
+ players_only=players_only.drop(['Player'], axis=1)
452
+
453
+ salary_2x_check = (overall_file - (salary_file*2))
454
+ salary_3x_check = (overall_file - (salary_file*3))
455
+ salary_4x_check = (overall_file - (salary_file*4))
456
+
457
+ players_only['Average_Rank'] = players_only.mean(axis=1)
458
+ players_only['Top_finish'] = players_only[players_only == 1].count(axis=1)/total_sims
459
+ players_only['Top_5_finish'] = players_only[players_only <= 5].count(axis=1)/total_sims
460
+ players_only['Top_10_finish'] = players_only[players_only <= 10].count(axis=1)/total_sims
461
+ players_only['20+%'] = overall_file[overall_file >= 20].count(axis=1)/float(total_sims)
462
+ players_only['2x%'] = salary_2x_check[salary_2x_check >= 1].count(axis=1)/float(total_sims)
463
+ players_only['3x%'] = salary_3x_check[salary_3x_check >= 1].count(axis=1)/float(total_sims)
464
+ players_only['4x%'] = salary_4x_check[salary_4x_check >= 1].count(axis=1)/float(total_sims)
465
+
466
+ players_only['Player'] = hold_file[['Player']]
467
+
468
+ final_outcomes = players_only[['Player', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%']]
469
+
470
+ final_Proj = merge(hold_file, final_outcomes, on="Player")
471
+ final_Proj = final_Proj[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%']]
472
+ final_Proj['Own'] = final_Proj['Player'].map(basic_own_dict).astype(float)
473
+ final_Proj['Small Field Own%'] = final_Proj['Player'].map(small_own_dict).astype(float)
474
+ final_Proj['Large Field Own%'] = final_Proj['Player'].map(large_own_dict).astype(float)
475
+ final_Proj['Cash Own%'] = final_Proj['Player'].map(cash_own_dict).astype(float)
476
+ final_Proj['name_team'] = final_Proj['Player'] + final_Proj['Position']
477
+ final_Proj['Team'] = final_Proj['name_team'].map(basic_team_dict)
478
+ final_Proj['Opp'] = final_Proj['Player'].map(basic_opp_dict)
479
+ final_Proj = final_Proj[['Player', 'Position', 'Team', 'Opp', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'Own', 'Small Field Own%', 'Large Field Own%', 'Cash Own%']]
480
+ final_Proj['Salary'] = final_Proj['Salary'].astype(int)
481
+ final_Proj = final_Proj.sort_values(by='Median', ascending=False)
482
+
483
+ return final_Proj.copy()