Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help with dfs_quant constraints (calculating optimal portfolio)

Hi everyone! Not a coder by trade, but I've been playing with the dfs_quant_finance github after watching the videos.

I'm using a draftkings .csv and got to a good point, but when I go to calculate optimal portfolio I get this error

'ValueError: zero-size array to reduction operation maximum which has no identity'

Would love some help on this! Thanks so much <3

1 response

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)