DFS_Portfolio_Manager / global_func /exposure_spread.py
James McCool
Add debug print statement in exposure_spread function to log player assignments, aiding in troubleshooting during player replacement process.
efac5fb
raw
history blame
2.62 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]
# for each row to the the number of lineups to remove, replace with random choice from comparable player list
for row in range(math.ceil(lineups_to_remove)):
insert_player = random.choice(comparable_player_list)
player_location = list(player_rows.iloc[row]).index(exposure_player)
working_frame.at[row, player_location] = insert_player
print(working_frame.at[row, player_location])
return working_frame