This is a screenshot of the cells running prior to
result = opt.calculate_optimal_portfolio(
objective=obj,
constraints=[
discretize_players,
player_position_constraint,
max_players,
cost_limit,
]
)
Player costs
player_costs = pd.DataFrame(df['dk_salary']).dropna().sort_index(level=['game_date', 'player_id'])
Player positions.
player_positions = df['dk_position'].dropna()
Define the game date that we want to test. Change this and re-run
cells below to build a lineup for another date.
TEST_DATE = '2019-10-21'
Format our expected value factor (dk_score) for use in Optimize.
expected_value = dk_score[player_costs.index].dropna()
expected_value = expected_value[expected_value.index.get_level_values(0) == TEST_DATE]
Objective function to be fed into Optimize later on.
obj = opt.MaximizeAlpha(expected_value)
Salary cap constraint. The total cost of our lineup has to be less than 50,000.
We will define this as a FactorExposure constraint.
cost_limit = opt.FactorExposure(
player_costs,
min_exposures={'dk_salary': 0},
max_exposures={'dk_salary': 50000}
)
Map from each player to its position.
labels = df['dk_position'].apply(lambda x: x[0])
Maps from each position to its min/max exposure.
min_exposures = {'WR/FLEX': 3, 'RB/FLEX': 2, 'QB': 1, 'TE/FLEX':1, 'DST':1}
max_exposures = {'WR/FLEX': 4, 'RB/FLEX': 3, 'QB': 1, 'TE/FLEX':2, 'DST':1}
player_position_constraint = opt.NetGroupExposure(labels, min_exposures, max_exposures)
This constraints tells the Optimizer than we can hold at most 1 of each player
discretize_players = opt.PositionConcentration(
pd.Series([]),
pd.Series([]),
default_min_weight=0,
default_max_weight=1,
)
max_players = opt.MaxGrossExposure(9)