DFS_Portfolio_Manager / global_func /exposure_spread.py
James McCool
Fix indentation in exposure_spread function to ensure proper flow control during player replacement, enhancing the logic for lineup adjustments.
8a49e0f
raw
history blame
2.85 kB
import random
import numpy as np
import math
#### Goal is to choose a player and adjust the amount of lineups that have them
#### First thing you need to do is find comparable players in the projections, so any player in the projections that is within $500 of the player and within 10% of the projection
#### Take that list of players and create a list that can be accessed for random insertion into the portfolio
#### Find the player and the amount of rows that contain them and then find an exposure rate which is the percentage of total rows
#### Use the exposure target argument and try to replace the player from as many rows as necessary to be at or just under the target
def exposure_spread(working_frame, exposure_player, exposure_target, exposure_stack_bool, projections_df):
# Find comparable players in the projections
comparable_players = projections_df[projections_df['player_names'] == exposure_player]
comp_salary_high = comparable_players['salary'][0]
comp_salary_low = comparable_players['salary'][0] - 500
comp_projection_high = comparable_players['median'][0]
comp_projection_low = comparable_players['median'][0] - (comparable_players['median'][0] * .9)
comparable_players = projections_df[
(projections_df['salary'] >= comp_salary_low) &
(projections_df['salary'] <= comp_salary_high) &
(projections_df['median'] >= comp_projection_low) &
(projections_df['median'] <= comp_projection_high)
]
# Create a list of comparable players
comparable_player_list = comparable_players['player_names'].tolist()
# find the exposure rate of the player in the working frame
player_mask = working_frame[working_frame.columns].apply(
lambda row: exposure_player in list(row), axis=1
)
player_exposure = player_mask.sum() / len(working_frame)
# find the number of lineups that need to be removed to reach the target exposure
lineups_to_remove = (player_exposure - exposure_target) * len(working_frame)
# isolate the rows that contain the player
player_rows = working_frame[player_mask]
print(player_rows.head(10))
print(working_frame.head(10))
# for each row to the the number of lineups to remove, replace with random choice from comparable player list
for row in player_rows.index:
for change in range(math.ceil(lineups_to_remove)):
insert_player = random.choice(comparable_player_list)
# Find which column contains the exposure_player
row_data = working_frame.iloc[row]
for col in working_frame.columns:
if row_data[col] == exposure_player:
working_frame.at[row, col] = insert_player
break
break
return working_frame