DFS_Portfolio_Manager / global_func /reduce_volatility_preset.py
James McCool
Enhance preset functions in app.py and related modules to include 'sport' parameter for improved flexibility in lineup management. This update modifies the small_field_preset, large_field_preset, volatility_preset, hedging_preset, and reduce_volatility_preset functions to accommodate sport-specific logic, enhancing user experience and accuracy in lineup strategies.
206da8c
raw
history blame
1.72 kB
import pandas as pd
import numpy as np
def reduce_volatility_preset(portfolio: pd.DataFrame, lineup_target: int, exclude_cols: list, sport: str):
excluded_cols = ['salary', 'median', 'Own', 'Finish_percentile', 'Dupes', 'Stack', 'Size', 'Win%', 'Lineup Edge', 'Weighted Own', 'Geomean', 'Diversity']
player_columns = [col for col in portfolio.columns if col not in excluded_cols]
for slack_var in range(1, 20):
concat_portfolio = pd.DataFrame(columns=portfolio.columns)
for team in portfolio['Stack'].unique():
rows_to_drop = []
working_portfolio = portfolio.copy()
working_portfolio = working_portfolio[working_portfolio['Stack'] == team].sort_values(by='Weighted Own', ascending = False)
working_portfolio = working_portfolio.reset_index(drop=True)
curr_own_type_max = working_portfolio.loc[0, 'Diversity'] + (slack_var / 20 * working_portfolio.loc[0, 'Diversity'])
for i in range(1, len(working_portfolio)):
if working_portfolio.loc[i, 'Diversity'] < curr_own_type_max:
rows_to_drop.append(i)
else:
curr_own_type_max = working_portfolio.loc[i, 'Diversity'] + (slack_var / 20 * working_portfolio.loc[i, 'Diversity'])
working_portfolio = working_portfolio.drop(rows_to_drop).reset_index(drop=True)
concat_portfolio = pd.concat([concat_portfolio, working_portfolio])
if len(concat_portfolio) >= lineup_target:
return concat_portfolio.sort_values(by='Weighted Own', ascending=False).head(lineup_target)
return concat_portfolio.sort_values(by='Weighted Own', ascending=False)