I have a cplex/docplex model with an "active risk" term. I believe I'm messing up the mix of Pandas and DocPlex, but I'm worried I'm trying to do something impossible.
The term should just be the quadratic form (Target-Optimal) \Sigma (Target-Optimal).
from docplex.mp.advmodel import AdvModel
from numpy import identity
from pandas import Series, DataFrame
model = AdvModel()
assets = ['AAA', 'BBB', 'CCC', 'DDD']
optimal = Series(1 / 4, assets)
covariances = DataFrame(identity(4) * 0.10, index=assets, columns=assets)
target = Series(model.continuous_var_list(assets, name='Target', lb=0, ub=1), index=assets)
active_risk = model.quad_matrix_sum(covariances, target - optimal) / 2
print(active_risk)
giving the error
AttributeError: 'LinearExpr' object has no attribute '_index'
Interestingly, something like the following works. So I could shift all the variables to be the difference but I'm trying to avoid this if possible as that would make other complicated terms in the optimization less clear.
# lb, ub are complicated now
difference = Series(model.continuous_var_list(assets, name='Target', lb=lb, ub=ub), index=assets)
model.quad_matrix_sum(covariances, difference) / 2