首页 > 解决方案 > How to set a semi-positive Parameter(matrix) in CVXPY?

问题描述

I want to use warm start to check whether there's performance benefit by setting a cvx.Parameter, while I come accross this:

cvxpy.error.DCPError: Problem does not follow DCP rules.

My code is here:

import time 
import numpy as np
import cvxpy as cvx


m = 300
x = cvx.Variable((m, 1))
p_cov = cvx.Parameter((m, m))
prob = cvx.Problem(cvx.Maximize(-cvx.quad_form(x, p_cov)), [x>=0, cvx.sum(x)==1])

for _ in range(10):
    df_return = np.random.randn(m, m+1)
    cov = np.cov(df_return)
    p_cov.value = cov
    t1 = time.time()
    prob.solve(warm_start=True)
    t2 = time.time()
    print("Solve time:", round(t2-t1, 2))

I think the point is that p_cov should be a semi-positive matrix, but I just can't solve it. Look forward your help, thank you!

标签: pythoncvxpy

解决方案


You're right! You just need to tell cvxpy that p_cov is positive semidefinite.

p_cov = cvx.Parameter((m, m), PSD=True)

DCP can now compute the correct nature of this expression:

cvx.quad_form(x, p_cov)

Which is:

Expression(CONVEX, NONNEGATIVE, (1, 1))

Instead of:

Expression(UNKNOWN, UNKNOWN, (1, 1))

Find more documentation in Advanced Features.


推荐阅读