Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -170,23 +170,37 @@ def run_seed_frame(seed_depth1, Strength_var, strength_grow, Teams_used, Total_R
|
|
170 |
return FinalPortfolio, maps_dict
|
171 |
|
172 |
def create_stack_options(player_data, wr_var):
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
|
|
185 |
|
186 |
-
|
187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
|
189 |
-
|
190 |
|
191 |
@st.cache_data
|
192 |
def apply_range(s: pl.Series) -> pl.Series:
|
@@ -275,257 +289,246 @@ def create_random_portfolio(Total_Sample_Size, raw_baselines):
|
|
275 |
def get_correlated_portfolio_for_sim(Total_Sample_Size):
|
276 |
|
277 |
sizesplit = round(Total_Sample_Size * sharp_split)
|
278 |
-
|
279 |
RandomPortfolio, maps_dict, ranges_dict, full_pos_player_dict = create_random_portfolio(sizesplit, raw_baselines)
|
280 |
stack_num = random.randint(1, 3)
|
281 |
stacking_dict = create_stack_options(raw_baselines, stack_num)
|
282 |
|
283 |
-
#
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
RandomPortfolio['WR2'] = pd.Series(list(RandomPortfolio['WR2'].map(full_pos_player_dict['pos_dicts'][1])), dtype="string[pyarrow]")
|
310 |
-
RandomPortfolio['WR3'] = pd.Series(list(RandomPortfolio['WR3'].map(full_pos_player_dict['pos_dicts'][1])), dtype="string[pyarrow]")
|
311 |
-
RandomPortfolio['TE'] = pd.Series(list(RandomPortfolio['TE'].map(full_pos_player_dict['pos_dicts'][2])), dtype="string[pyarrow]")
|
312 |
-
RandomPortfolio['FLEX'] = pd.Series(list(RandomPortfolio['FLEX'].map(full_pos_player_dict['pos_dicts'][3])), dtype="string[pyarrow]")
|
313 |
-
RandomPortfolio['DST'] = pd.Series(list(RandomPortfolio['DST'].map(def_dict)), dtype="string[pyarrow]")
|
314 |
-
RandomPortfolio['plyr_list'] = RandomPortfolio[RandomPortfolio.columns.values.tolist()].values.tolist()
|
315 |
-
RandomPortfolio['plyr_count'] = RandomPortfolio['plyr_list'].apply(lambda x: len(set(x)))
|
316 |
-
RandomPortfolio = RandomPortfolio[RandomPortfolio['plyr_count'] == 10].drop(columns=['plyr_list','plyr_count']).\
|
317 |
-
reset_index(drop=True)
|
318 |
|
319 |
-
|
320 |
-
|
321 |
-
del ranges_dict
|
322 |
-
del stack_num
|
323 |
-
del stacking_dict
|
324 |
|
|
|
|
|
325 |
|
|
|
|
|
326 |
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
|
|
|
|
|
|
336 |
|
337 |
-
|
338 |
-
RandomPortfolio
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
RandomPortfolio['TEp'] = RandomPortfolio['TE'].map(maps_dict['Projection_map']).astype(np.float16)
|
344 |
-
RandomPortfolio['FLEXp'] = RandomPortfolio['FLEX'].map(maps_dict['Projection_map']).astype(np.float16)
|
345 |
-
RandomPortfolio['DSTp'] = RandomPortfolio['DST'].map(maps_dict['Projection_map']).astype(np.float16)
|
346 |
|
347 |
-
|
348 |
-
RandomPortfolio
|
349 |
-
|
350 |
-
|
351 |
-
RandomPortfolio['WR2o'] = RandomPortfolio['WR2'].map(maps_dict['Own_map']).astype(np.float16)
|
352 |
-
RandomPortfolio['WR3o'] = RandomPortfolio['WR3'].map(maps_dict['Own_map']).astype(np.float16)
|
353 |
-
RandomPortfolio['TEo'] = RandomPortfolio['TE'].map(maps_dict['Own_map']).astype(np.float16)
|
354 |
-
RandomPortfolio['FLEXo'] = RandomPortfolio['FLEX'].map(maps_dict['Own_map']).astype(np.float16)
|
355 |
-
RandomPortfolio['DSTo'] = RandomPortfolio['DST'].map(maps_dict['Own_map']).astype(np.float16)
|
356 |
|
357 |
-
|
358 |
-
|
359 |
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
|
364 |
-
|
365 |
-
RandomPortfolioDF = pd.DataFrame(RandomPortArrayOut, columns = ['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST', 'User/Field', 'Salary', 'Projection', 'Own'])
|
366 |
-
RandomPortfolioDF = RandomPortfolioDF.sort_values(by=Sim_function, ascending=False)
|
367 |
-
del RandomPortArray
|
368 |
-
del RandomPortArrayOut
|
369 |
|
370 |
if insert_port == 1:
|
371 |
-
CleanPortfolio
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
]).astype(np.float16)
|
392 |
-
if insert_port == 1:
|
393 |
-
CleanPortfolio['Own'] = sum([CleanPortfolio['QB'].map(maps_dict['Own_map']),
|
394 |
-
CleanPortfolio['RB1'].map(maps_dict['Own_map']),
|
395 |
-
CleanPortfolio['RB2'].map(maps_dict['Own_map']),
|
396 |
-
CleanPortfolio['WR1'].map(maps_dict['Own_map']),
|
397 |
-
CleanPortfolio['WR2'].map(maps_dict['Own_map']),
|
398 |
-
CleanPortfolio['WR3'].map(maps_dict['Own_map']),
|
399 |
-
CleanPortfolio['TE'].map(maps_dict['Own_map']),
|
400 |
-
CleanPortfolio['FLEX'].map(maps_dict['Own_map']),
|
401 |
-
CleanPortfolio['DST'].map(maps_dict['Own_map'])
|
402 |
-
]).astype(np.float16)
|
403 |
|
404 |
if site_var1 == 'Draftkings':
|
405 |
-
RandomPortfolioDF = RandomPortfolioDF
|
406 |
-
RandomPortfolioDF = RandomPortfolioDF
|
|
|
407 |
elif site_var1 == 'Fanduel':
|
408 |
-
RandomPortfolioDF = RandomPortfolioDF
|
409 |
-
RandomPortfolioDF = RandomPortfolioDF
|
410 |
|
411 |
-
|
|
|
|
|
412 |
|
413 |
-
|
|
|
414 |
|
415 |
return RandomPortfolio, maps_dict
|
416 |
|
417 |
def get_uncorrelated_portfolio_for_sim(Total_Sample_Size):
|
418 |
|
419 |
-
sizesplit = round(Total_Sample_Size *
|
420 |
-
|
421 |
RandomPortfolio, maps_dict, ranges_dict, full_pos_player_dict = create_random_portfolio(sizesplit, raw_baselines)
|
422 |
-
|
423 |
-
|
424 |
-
RandomPortfolio['RB1'] = pd.Series(list(RandomPortfolio['RB1'].map(full_pos_player_dict['pos_dicts'][0])), dtype="string[pyarrow]")
|
425 |
-
RandomPortfolio['RB2'] = pd.Series(list(RandomPortfolio['RB2'].map(full_pos_player_dict['pos_dicts'][0])), dtype="string[pyarrow]")
|
426 |
-
RandomPortfolio['WR1'] = pd.Series(list(RandomPortfolio['WR1'].map(full_pos_player_dict['pos_dicts'][1])), dtype="string[pyarrow]")
|
427 |
-
RandomPortfolio['WR2'] = pd.Series(list(RandomPortfolio['WR2'].map(full_pos_player_dict['pos_dicts'][1])), dtype="string[pyarrow]")
|
428 |
-
RandomPortfolio['WR3'] = pd.Series(list(RandomPortfolio['WR3'].map(full_pos_player_dict['pos_dicts'][1])), dtype="string[pyarrow]")
|
429 |
-
RandomPortfolio['TE'] = pd.Series(list(RandomPortfolio['TE'].map(full_pos_player_dict['pos_dicts'][2])), dtype="string[pyarrow]")
|
430 |
-
RandomPortfolio['FLEX'] = pd.Series(list(RandomPortfolio['FLEX'].map(full_pos_player_dict['pos_dicts'][3])), dtype="string[pyarrow]")
|
431 |
-
RandomPortfolio['DST'] = pd.Series(list(RandomPortfolio['DST'].map(def_dict)), dtype="string[pyarrow]")
|
432 |
-
RandomPortfolio['plyr_list'] = RandomPortfolio[RandomPortfolio.columns.values.tolist()].values.tolist()
|
433 |
-
RandomPortfolio['plyr_count'] = RandomPortfolio['plyr_list'].apply(lambda x: len(set(x)))
|
434 |
-
RandomPortfolio = RandomPortfolio[RandomPortfolio['plyr_count'] == 10].drop(columns=['plyr_list','plyr_count']).\
|
435 |
-
reset_index(drop=True)
|
436 |
|
437 |
-
|
438 |
-
|
439 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
440 |
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
RandomPortfolio['WR2s'] = RandomPortfolio['WR2'].map(maps_dict['Salary_map']).astype(np.int32)
|
446 |
-
RandomPortfolio['WR3s'] = RandomPortfolio['WR3'].map(maps_dict['Salary_map']).astype(np.int32)
|
447 |
-
RandomPortfolio['TEs'] = RandomPortfolio['TE'].map(maps_dict['Salary_map']).astype(np.int32)
|
448 |
-
RandomPortfolio['FLEXs'] = RandomPortfolio['FLEX'].map(maps_dict['Salary_map']).astype(np.int32)
|
449 |
-
RandomPortfolio['DSTs'] = RandomPortfolio['DST'].map(maps_dict['Salary_map']).astype(np.int32)
|
450 |
|
451 |
-
|
452 |
-
RandomPortfolio
|
453 |
-
RandomPortfolio['RB2p'] = RandomPortfolio['RB2'].map(maps_dict['Projection_map']).astype(np.float16)
|
454 |
-
RandomPortfolio['WR1p'] = RandomPortfolio['WR1'].map(maps_dict['Projection_map']).astype(np.float16)
|
455 |
-
RandomPortfolio['WR2p'] = RandomPortfolio['WR2'].map(maps_dict['Projection_map']).astype(np.float16)
|
456 |
-
RandomPortfolio['WR3p'] = RandomPortfolio['WR3'].map(maps_dict['Projection_map']).astype(np.float16)
|
457 |
-
RandomPortfolio['TEp'] = RandomPortfolio['TE'].map(maps_dict['Projection_map']).astype(np.float16)
|
458 |
-
RandomPortfolio['FLEXp'] = RandomPortfolio['FLEX'].map(maps_dict['Projection_map']).astype(np.float16)
|
459 |
-
RandomPortfolio['DSTp'] = RandomPortfolio['DST'].map(maps_dict['Projection_map']).astype(np.float16)
|
460 |
|
461 |
-
|
462 |
-
RandomPortfolio
|
463 |
-
RandomPortfolio['RB2o'] = RandomPortfolio['RB2'].map(maps_dict['Own_map']).astype(np.float16)
|
464 |
-
RandomPortfolio['WR1o'] = RandomPortfolio['WR1'].map(maps_dict['Own_map']).astype(np.float16)
|
465 |
-
RandomPortfolio['WR2o'] = RandomPortfolio['WR2'].map(maps_dict['Own_map']).astype(np.float16)
|
466 |
-
RandomPortfolio['WR3o'] = RandomPortfolio['WR3'].map(maps_dict['Own_map']).astype(np.float16)
|
467 |
-
RandomPortfolio['TEo'] = RandomPortfolio['TE'].map(maps_dict['Own_map']).astype(np.float16)
|
468 |
-
RandomPortfolio['FLEXo'] = RandomPortfolio['FLEX'].map(maps_dict['Own_map']).astype(np.float16)
|
469 |
-
RandomPortfolio['DSTo'] = RandomPortfolio['DST'].map(maps_dict['Own_map']).astype(np.float16)
|
470 |
|
471 |
-
|
472 |
-
|
473 |
|
474 |
-
|
475 |
-
|
476 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
477 |
|
478 |
-
RandomPortArrayOut = np.delete(RandomPortArray, np.s_[10:37], axis=1)
|
479 |
-
RandomPortfolioDF = pd.DataFrame(RandomPortArrayOut, columns = ['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST', 'User/Field', 'Salary', 'Projection', 'Own'])
|
480 |
-
RandomPortfolioDF = RandomPortfolioDF.sort_values(by=Sim_function, ascending=False)
|
481 |
-
del RandomPortArray
|
482 |
-
del RandomPortArrayOut
|
483 |
-
# st.table(RandomPortfolioDF.head(50))
|
484 |
-
|
485 |
-
if insert_port == 1:
|
486 |
-
CleanPortfolio['Salary'] = sum([CleanPortfolio['QB'].map(maps_dict['Salary_map']),
|
487 |
-
CleanPortfolio['RB1'].map(maps_dict['Salary_map']),
|
488 |
-
CleanPortfolio['RB2'].map(maps_dict['Salary_map']),
|
489 |
-
CleanPortfolio['WR1'].map(maps_dict['Salary_map']),
|
490 |
-
CleanPortfolio['WR2'].map(maps_dict['Salary_map']),
|
491 |
-
CleanPortfolio['WR3'].map(maps_dict['Salary_map']),
|
492 |
-
CleanPortfolio['TE'].map(maps_dict['Salary_map']),
|
493 |
-
CleanPortfolio['FLEX'].map(maps_dict['Salary_map']),
|
494 |
-
CleanPortfolio['DST'].map(maps_dict['Salary_map'])
|
495 |
-
]).astype(np.int16)
|
496 |
-
if insert_port == 1:
|
497 |
-
CleanPortfolio['Projection'] = sum([CleanPortfolio['QB'].map(up_dict['Projection_map']),
|
498 |
-
CleanPortfolio['RB1'].map(up_dict['Projection_map']),
|
499 |
-
CleanPortfolio['RB2'].map(up_dict['Projection_map']),
|
500 |
-
CleanPortfolio['WR1'].map(up_dict['Projection_map']),
|
501 |
-
CleanPortfolio['WR2'].map(up_dict['Projection_map']),
|
502 |
-
CleanPortfolio['WR3'].map(up_dict['Projection_map']),
|
503 |
-
CleanPortfolio['TE'].map(up_dict['Projection_map']),
|
504 |
-
CleanPortfolio['FLEX'].map(up_dict['Projection_map']),
|
505 |
-
CleanPortfolio['DST'].map(up_dict['Projection_map'])
|
506 |
-
]).astype(np.float16)
|
507 |
if insert_port == 1:
|
508 |
-
CleanPortfolio
|
509 |
-
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
518 |
|
519 |
if site_var1 == 'Draftkings':
|
520 |
-
RandomPortfolioDF = RandomPortfolioDF
|
521 |
-
RandomPortfolioDF = RandomPortfolioDF
|
|
|
522 |
elif site_var1 == 'Fanduel':
|
523 |
-
RandomPortfolioDF = RandomPortfolioDF
|
524 |
-
RandomPortfolioDF = RandomPortfolioDF
|
525 |
|
526 |
-
|
|
|
|
|
527 |
|
528 |
-
|
|
|
529 |
|
530 |
return RandomPortfolio, maps_dict
|
531 |
|
|
|
170 |
return FinalPortfolio, maps_dict
|
171 |
|
172 |
def create_stack_options(player_data, wr_var):
|
173 |
+
# Assuming player_data is already a Polars DataFrame
|
174 |
+
# Sort player_data by 'Median' in descending order
|
175 |
+
data_raw = player_data.sort("Median", reverse=True)
|
176 |
+
|
177 |
+
merged_frame = pl.DataFrame(
|
178 |
+
{
|
179 |
+
"QB": [],
|
180 |
+
"Player": []
|
181 |
+
}
|
182 |
+
)
|
183 |
+
|
184 |
+
for team in data_raw.select("Team").unique().get("Team"):
|
185 |
+
data_split = data_raw.filter(pl.col("Team") == team)
|
186 |
|
187 |
+
qb_frame = data_split.filter(pl.col("Position") == "QB")
|
188 |
+
wr_frame = data_split.filter(pl.col("Position") == "WR").slice(wr_var - 1, wr_var)
|
189 |
+
|
190 |
+
qb_name = qb_frame.head(1).get("Player")[0] if qb_frame.shape[0] > 0 else None
|
191 |
+
if qb_name is not None:
|
192 |
+
wr_frame = wr_frame.with_column(pl.lit(qb_name).alias("QB"))
|
193 |
+
merge_slice = wr_frame.select("QB", "Player")
|
194 |
+
merged_frame = merged_frame.vstack(merge_slice)
|
195 |
+
|
196 |
+
# Reset index (not necessary in Polars as index doesn't exist in the same way as Pandas)
|
197 |
+
# Build a dictionary from the DataFrame
|
198 |
+
correl_dict = dict(zip(merged_frame.get("QB"), merged_frame.get("Player")))
|
199 |
+
|
200 |
+
del merged_frame
|
201 |
+
del data_raw
|
202 |
|
203 |
+
return correl_dict
|
204 |
|
205 |
@st.cache_data
|
206 |
def apply_range(s: pl.Series) -> pl.Series:
|
|
|
289 |
def get_correlated_portfolio_for_sim(Total_Sample_Size):
|
290 |
|
291 |
sizesplit = round(Total_Sample_Size * sharp_split)
|
|
|
292 |
RandomPortfolio, maps_dict, ranges_dict, full_pos_player_dict = create_random_portfolio(sizesplit, raw_baselines)
|
293 |
stack_num = random.randint(1, 3)
|
294 |
stacking_dict = create_stack_options(raw_baselines, stack_num)
|
295 |
|
296 |
+
# Mapping series
|
297 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
298 |
+
pl.col("QB").apply(lambda x: qb_dict.get(x, x), return_dtype=pl.Utf8)
|
299 |
+
)
|
300 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
301 |
+
pl.col("RB1").apply(lambda x: full_pos_player_dict['pos_dicts'][0].get(x, x), return_dtype=pl.Utf8)
|
302 |
+
)
|
303 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
304 |
+
pl.col("RB2").apply(lambda x: full_pos_player_dict['pos_dicts'][0].get(x, x), return_dtype=pl.Utf8)
|
305 |
+
)
|
306 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
307 |
+
pl.col("WR1").apply(lambda x: stacking_dict.get(x, x), return_dtype=pl.Utf8)
|
308 |
+
)
|
309 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
310 |
+
pl.col("WR2").apply(lambda x: full_pos_player_dict['pos_dicts'][1].get(x, x), return_dtype=pl.Utf8)
|
311 |
+
)
|
312 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
313 |
+
pl.col("WR3").apply(lambda x: full_pos_player_dict['pos_dicts'][1].get(x, x), return_dtype=pl.Utf8)
|
314 |
+
)
|
315 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
316 |
+
pl.col("TE").apply(lambda x: full_pos_player_dict['pos_dicts'][2].get(x, x), return_dtype=pl.Utf8)
|
317 |
+
)
|
318 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
319 |
+
pl.col("FLEX").apply(lambda x: full_pos_player_dict['pos_dicts'][3].get(x, x), return_dtype=pl.Utf8)
|
320 |
+
)
|
321 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
322 |
+
pl.col("DST").apply(lambda x: def_dict.get(x, x), return_dtype=pl.Utf8)
|
323 |
+
)
|
324 |
|
325 |
+
# Creating 'plyr_list' and 'plyr_count'
|
326 |
+
plyr_list_exprs = [pl.col(name).alias(f"{name}_item") for name in RandomPortfolio.columns]
|
327 |
+
plyr_list = pl.col(plyr_list_exprs).apply(lambda x: list(set(x)), return_dtype=pl.List(pl.Utf8)).alias("plyr_list")
|
328 |
+
plyr_count = plyr_list.apply(lambda x: len(set(x)), return_dtype=pl.Int64).alias("plyr_count")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
329 |
|
330 |
+
# Add these to RandomPortfolio
|
331 |
+
RandomPortfolio = RandomPortfolio.with_columns([plyr_list, plyr_count])
|
|
|
|
|
|
|
332 |
|
333 |
+
# Filter out rows where 'plyr_count' is not 10
|
334 |
+
RandomPortfolio = RandomPortfolio.filter(pl.col("plyr_count") == 10).select_except("plyr_list", "plyr_count")
|
335 |
|
336 |
+
# Since polars DataFrame is lazy, you may want to call .collect() to materialize it
|
337 |
+
RandomPortfolio = RandomPortfolio.collect()
|
338 |
|
339 |
+
# Map and cast to specific data types
|
340 |
+
positions = ['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST']
|
341 |
+
for pos in positions:
|
342 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
343 |
+
pl.col(pos).apply(lambda x: maps_dict['Salary_map'].get(x, x), return_dtype=pl.Int32).alias(f"{pos}s")
|
344 |
+
)
|
345 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
346 |
+
pl.col(pos).apply(lambda x: maps_dict['Projection_map'].get(x, x), return_dtype=pl.Float32).alias(f"{pos}p")
|
347 |
+
)
|
348 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
349 |
+
pl.col(pos).apply(lambda x: maps_dict['Own_map'].get(x, x), return_dtype=pl.Float32).alias(f"{pos}o")
|
350 |
+
)
|
351 |
|
352 |
+
# Equivalent of converting to numpy array and performing einsum
|
353 |
+
RandomPortfolio = RandomPortfolio.with_columns([
|
354 |
+
pl.sum([pl.col(f"{pos}s") for pos in positions]).alias('Salary'),
|
355 |
+
pl.sum([pl.col(f"{pos}p") for pos in positions]).alias('Projection'),
|
356 |
+
pl.sum([pl.col(f"{pos}o") for pos in positions]).alias('Own')
|
357 |
+
])
|
|
|
|
|
|
|
358 |
|
359 |
+
# Select the columns you want in the final DataFrame
|
360 |
+
RandomPortfolio = RandomPortfolio.select(
|
361 |
+
positions + ['User/Field', 'Salary', 'Projection', 'Own']
|
362 |
+
)
|
|
|
|
|
|
|
|
|
|
|
363 |
|
364 |
+
# Since DataFrame is lazy, call collect() to materialize
|
365 |
+
RandomPortfolio = RandomPortfolio.collect()
|
366 |
|
367 |
+
# Sorting based on some function
|
368 |
+
# Note: Replace 'Sim_function' with the actual column name or expression you want to sort by
|
369 |
+
RandomPortfolio = RandomPortfolio.sort('Sim_function', reverse=True)
|
370 |
|
371 |
+
positions = ['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST']
|
|
|
|
|
|
|
|
|
372 |
|
373 |
if insert_port == 1:
|
374 |
+
CleanPortfolio = CleanPortfolio.with_column(
|
375 |
+
pl.sum([
|
376 |
+
pl.col(pos).apply(lambda x: maps_dict['Salary_map'].get(x, x), return_dtype=pl.Int16)
|
377 |
+
for pos in positions
|
378 |
+
]).alias('Salary')
|
379 |
+
)
|
380 |
+
|
381 |
+
CleanPortfolio = CleanPortfolio.with_column(
|
382 |
+
pl.sum([
|
383 |
+
pl.col(pos).apply(lambda x: up_dict['Projection_map'].get(x, x), return_dtype=pl.Float16)
|
384 |
+
for pos in positions
|
385 |
+
]).alias('Projection')
|
386 |
+
)
|
387 |
+
|
388 |
+
CleanPortfolio = CleanPortfolio.with_column(
|
389 |
+
pl.sum([
|
390 |
+
pl.col(pos).apply(lambda x: maps_dict['Own_map'].get(x, x), return_dtype=pl.Float16)
|
391 |
+
for pos in positions
|
392 |
+
]).alias('Own')
|
393 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
394 |
|
395 |
if site_var1 == 'Draftkings':
|
396 |
+
RandomPortfolioDF = RandomPortfolioDF.filter(pl.col('Salary') <= 50000)
|
397 |
+
RandomPortfolioDF = RandomPortfolioDF.filter(pl.col('Salary') >= (49500 - (5000 * (1 - (len(Teams_used) / 32))) - (FieldStrength * 1000)))
|
398 |
+
|
399 |
elif site_var1 == 'Fanduel':
|
400 |
+
RandomPortfolioDF = RandomPortfolioDF.filter(pl.col('Salary') <= 60000)
|
401 |
+
RandomPortfolioDF = RandomPortfolioDF.filter(pl.col('Salary') >= (59500 - (5000 * (1 - (len(Teams_used) / 32))) - (FieldStrength * 1000)))
|
402 |
|
403 |
+
# Sorting the DataFrame
|
404 |
+
# Note: Replace 'Sim_function' with the actual column name or expression you want to sort by
|
405 |
+
RandomPortfolioDF = RandomPortfolioDF.sort('Sim_function', reverse=True)
|
406 |
|
407 |
+
# Selecting the columns you want to keep
|
408 |
+
RandomPortfolioDF = RandomPortfolioDF.select(['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST', 'User/Field', 'Salary', 'Projection', 'Own'])
|
409 |
|
410 |
return RandomPortfolio, maps_dict
|
411 |
|
412 |
def get_uncorrelated_portfolio_for_sim(Total_Sample_Size):
|
413 |
|
414 |
+
sizesplit = round(Total_Sample_Size * sharp_split)
|
|
|
415 |
RandomPortfolio, maps_dict, ranges_dict, full_pos_player_dict = create_random_portfolio(sizesplit, raw_baselines)
|
416 |
+
stack_num = random.randint(1, 3)
|
417 |
+
stacking_dict = create_stack_options(raw_baselines, stack_num)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
418 |
|
419 |
+
# Mapping series
|
420 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
421 |
+
pl.col("QB").apply(lambda x: qb_dict.get(x, x), return_dtype=pl.Utf8)
|
422 |
+
)
|
423 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
424 |
+
pl.col("RB1").apply(lambda x: full_pos_player_dict['pos_dicts'][0].get(x, x), return_dtype=pl.Utf8)
|
425 |
+
)
|
426 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
427 |
+
pl.col("RB2").apply(lambda x: full_pos_player_dict['pos_dicts'][0].get(x, x), return_dtype=pl.Utf8)
|
428 |
+
)
|
429 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
430 |
+
pl.col("WR1").apply(lambda x: stacking_dict.get(x, x), return_dtype=pl.Utf8)
|
431 |
+
)
|
432 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
433 |
+
pl.col("WR2").apply(lambda x: full_pos_player_dict['pos_dicts'][1].get(x, x), return_dtype=pl.Utf8)
|
434 |
+
)
|
435 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
436 |
+
pl.col("WR3").apply(lambda x: full_pos_player_dict['pos_dicts'][1].get(x, x), return_dtype=pl.Utf8)
|
437 |
+
)
|
438 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
439 |
+
pl.col("TE").apply(lambda x: full_pos_player_dict['pos_dicts'][2].get(x, x), return_dtype=pl.Utf8)
|
440 |
+
)
|
441 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
442 |
+
pl.col("FLEX").apply(lambda x: full_pos_player_dict['pos_dicts'][3].get(x, x), return_dtype=pl.Utf8)
|
443 |
+
)
|
444 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
445 |
+
pl.col("DST").apply(lambda x: def_dict.get(x, x), return_dtype=pl.Utf8)
|
446 |
+
)
|
447 |
|
448 |
+
# Creating 'plyr_list' and 'plyr_count'
|
449 |
+
plyr_list_exprs = [pl.col(name).alias(f"{name}_item") for name in RandomPortfolio.columns]
|
450 |
+
plyr_list = pl.col(plyr_list_exprs).apply(lambda x: list(set(x)), return_dtype=pl.List(pl.Utf8)).alias("plyr_list")
|
451 |
+
plyr_count = plyr_list.apply(lambda x: len(set(x)), return_dtype=pl.Int64).alias("plyr_count")
|
|
|
|
|
|
|
|
|
|
|
452 |
|
453 |
+
# Add these to RandomPortfolio
|
454 |
+
RandomPortfolio = RandomPortfolio.with_columns([plyr_list, plyr_count])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
455 |
|
456 |
+
# Filter out rows where 'plyr_count' is not 10
|
457 |
+
RandomPortfolio = RandomPortfolio.filter(pl.col("plyr_count") == 10).select_except("plyr_list", "plyr_count")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
458 |
|
459 |
+
# Since polars DataFrame is lazy, you may want to call .collect() to materialize it
|
460 |
+
RandomPortfolio = RandomPortfolio.collect()
|
461 |
|
462 |
+
# Map and cast to specific data types
|
463 |
+
positions = ['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST']
|
464 |
+
for pos in positions:
|
465 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
466 |
+
pl.col(pos).apply(lambda x: maps_dict['Salary_map'].get(x, x), return_dtype=pl.Int32).alias(f"{pos}s")
|
467 |
+
)
|
468 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
469 |
+
pl.col(pos).apply(lambda x: maps_dict['Projection_map'].get(x, x), return_dtype=pl.Float32).alias(f"{pos}p")
|
470 |
+
)
|
471 |
+
RandomPortfolio = RandomPortfolio.with_column(
|
472 |
+
pl.col(pos).apply(lambda x: maps_dict['Own_map'].get(x, x), return_dtype=pl.Float32).alias(f"{pos}o")
|
473 |
+
)
|
474 |
+
|
475 |
+
# Equivalent of converting to numpy array and performing einsum
|
476 |
+
RandomPortfolio = RandomPortfolio.with_columns([
|
477 |
+
pl.sum([pl.col(f"{pos}s") for pos in positions]).alias('Salary'),
|
478 |
+
pl.sum([pl.col(f"{pos}p") for pos in positions]).alias('Projection'),
|
479 |
+
pl.sum([pl.col(f"{pos}o") for pos in positions]).alias('Own')
|
480 |
+
])
|
481 |
+
|
482 |
+
# Select the columns you want in the final DataFrame
|
483 |
+
RandomPortfolio = RandomPortfolio.select(
|
484 |
+
positions + ['User/Field', 'Salary', 'Projection', 'Own']
|
485 |
+
)
|
486 |
+
|
487 |
+
# Since DataFrame is lazy, call collect() to materialize
|
488 |
+
RandomPortfolio = RandomPortfolio.collect()
|
489 |
+
|
490 |
+
# Sorting based on some function
|
491 |
+
# Note: Replace 'Sim_function' with the actual column name or expression you want to sort by
|
492 |
+
RandomPortfolio = RandomPortfolio.sort('Sim_function', reverse=True)
|
493 |
+
|
494 |
+
positions = ['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST']
|
495 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
496 |
if insert_port == 1:
|
497 |
+
CleanPortfolio = CleanPortfolio.with_column(
|
498 |
+
pl.sum([
|
499 |
+
pl.col(pos).apply(lambda x: maps_dict['Salary_map'].get(x, x), return_dtype=pl.Int16)
|
500 |
+
for pos in positions
|
501 |
+
]).alias('Salary')
|
502 |
+
)
|
503 |
+
|
504 |
+
CleanPortfolio = CleanPortfolio.with_column(
|
505 |
+
pl.sum([
|
506 |
+
pl.col(pos).apply(lambda x: up_dict['Projection_map'].get(x, x), return_dtype=pl.Float16)
|
507 |
+
for pos in positions
|
508 |
+
]).alias('Projection')
|
509 |
+
)
|
510 |
+
|
511 |
+
CleanPortfolio = CleanPortfolio.with_column(
|
512 |
+
pl.sum([
|
513 |
+
pl.col(pos).apply(lambda x: maps_dict['Own_map'].get(x, x), return_dtype=pl.Float16)
|
514 |
+
for pos in positions
|
515 |
+
]).alias('Own')
|
516 |
+
)
|
517 |
|
518 |
if site_var1 == 'Draftkings':
|
519 |
+
RandomPortfolioDF = RandomPortfolioDF.filter(pl.col('Salary') <= 50000)
|
520 |
+
RandomPortfolioDF = RandomPortfolioDF.filter(pl.col('Salary') >= (49500 - (5000 * (1 - (len(Teams_used) / 32))) - (FieldStrength * 1000)))
|
521 |
+
|
522 |
elif site_var1 == 'Fanduel':
|
523 |
+
RandomPortfolioDF = RandomPortfolioDF.filter(pl.col('Salary') <= 60000)
|
524 |
+
RandomPortfolioDF = RandomPortfolioDF.filter(pl.col('Salary') >= (59500 - (5000 * (1 - (len(Teams_used) / 32))) - (FieldStrength * 1000)))
|
525 |
|
526 |
+
# Sorting the DataFrame
|
527 |
+
# Note: Replace 'Sim_function' with the actual column name or expression you want to sort by
|
528 |
+
RandomPortfolioDF = RandomPortfolioDF.sort('Sim_function', reverse=True)
|
529 |
|
530 |
+
# Selecting the columns you want to keep
|
531 |
+
RandomPortfolioDF = RandomPortfolioDF.select(['QB', 'RB1', 'RB2', 'WR1', 'WR2', 'WR3', 'TE', 'FLEX', 'DST', 'User/Field', 'Salary', 'Projection', 'Own'])
|
532 |
|
533 |
return RandomPortfolio, maps_dict
|
534 |
|