ef = setup_efficient_frontier()
for sector in sector_upper:
is_sector = [v == sector for k, v in sector_mapper.items()]
ef.add_constraint(lambda w: cp.sum(w[is_sector]) <= sector_upper[sector])
for sector in sector_lower:
is_sector = [v == sector for k, v in sector_mapper.items()]
ef.add_constraint(lambda w: cp.sum(w[is_sector]) >= sector_lower[sector])
After Change
is_sector = [sector_mapper[t] == sector for t in ef.tickers]
ef.add_constraint(lambda w: cp.sum(w[is_sector]) <= sector_upper[sector])
for sector in sector_lower:
is_sector = [sector_mapper[t] == sector for t in ef.tickers]
ef.add_constraint(lambda w: cp.sum(w[is_sector]) >= sector_lower[sector])
weights = ef.max_sharpe()