0

I am building risk term in a quadratic optimization problem (QP) using CVXPY and I'm struggling to combine expressions with a covariance matrix using quad_form.

from cvxpy import Variable, quad_form
from numpy import identity
from pandas import Series, DataFrame

assets = ['AAA', 'BBB', 'CCC', 'DDD']
optimal = Series(1 / 4, assets)
covariances = DataFrame(identity(4) * 0.20, index=assets, columns=assets)
covariances.iloc[2, 3] += 0.01
covariances.iloc[3, 2] = covariances.iloc[2, 3]

target = Series([Variable(name=asset) for asset in assets], index=assets)
difference = target - optimal

active_variance = quad_form(difference.values, covariances.values)

I'm getting the following error:

ValueError: setting an array element with a sequence.

I can just do the calculation in Pandas, but then CVXPY does not know the expression is convex.

active_variance = difference.T.dot(covariances).dot(difference)
print(active_variance.curvature)

This problem is related to my earlier question with docplex though the solution there doesn't work here as docplex does not require the problem be convex but CVXPY does. Is there a correct form in CVXPY?

rhaskett
  • 1,864
  • 3
  • 29
  • 48
  • The usage of `quad_form` here makes only sense iff `difference` would be a vector of decision variables (or constants). Due to your usage of pandas (i'm not sure if intended or not) here, `difference` **is becoming non-numeric / dtype=object which can never be used within cvxpy**. Hint: `AAA + -0.25`. In general, i would not recommend mixing pandas and cvxpy without knowing a lot about all the internals: hidden casts and co. – sascha Jan 31 '21 at 02:01
  • OK, this is a bit sad because the larger optimization is pretty complicated without Pandas keeping track of the data alignment, but that appears to be what is required. Thanks. – rhaskett Jan 31 '21 at 18:18

0 Answers0