I am trying to perform a portfolio optimization that returns the weights which maximize my utility function. I can do this portion just fine including the constraint that we
Not totally sure I understand, but I think you can add the following as another constraint:
def w_opt(W):
def filterer(x):
v = x.range.values
tp = v[0]
lower, upper = tp
return lower <= x[column_name].sum() <= upper
return not W.groupby(level=0, axis=0).filter(filterer).empty
c_ = {'type': 'eq', 'fun': w_opt} # add this to your other constraints
where x.range
is the interval (tuple
) repeated K[i]
times where K
is the number of times a particular level occurs and i
is the i
th level. column_name
in your case happens to be a date.
This says constrain the weights such that the sum of the weights in the i
th group is between the associated tuple
interval.
To map each of the level names to an interval do this:
intervals = [(.08,.51), (.05,.21), (.05,.41), (.05,.41), (.2,.66), (0,.16), (0,.76), (0,.11)]
names = ['equity', 'intl_equity', 'bond', 'intl_bond', 'commodity', 'pe', 'hf', 'cash']
mapper = Series(zip(names, intervals))
fully_mapped = mapper[init_weights.get_level_values(0)]
original_dataset['range'] = fully_mapped.values