Skip to content

Commit c687392

Browse files
Merge pull request robertmartin8#531 from andyherfer/master
fixed key error when adding multiple sector constraints
2 parents 6f699fd + 3feb53a commit c687392

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

pypfopt/base_optimizer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,10 @@ def add_sector_constraints(self, sector_mapper, sector_lower, sector_upper):
397397
"Sector constraints may not produce reasonable results if shorts are allowed."
398398
)
399399
for sector in sector_upper:
400-
is_sector = [sector_mapper[t] == sector for t in self.tickers]
400+
is_sector = [sector_mapper.get(t) == sector for t in self.tickers]
401401
self.add_constraint(lambda w: cp.sum(w[is_sector]) <= sector_upper[sector])
402402
for sector in sector_lower:
403-
is_sector = [sector_mapper[t] == sector for t in self.tickers]
403+
is_sector = [sector_mapper.get(t) == sector for t in self.tickers]
404404
self.add_constraint(lambda w: cp.sum(w[is_sector]) >= sector_lower[sector])
405405

406406
def convex_objective(self, custom_objective, weights_sum_to_one=True, **kwargs):

0 commit comments

Comments
 (0)