python - 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!
解决方案
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.
推荐阅读
- jsf - 如何使用 PrimeFaces Diagram 导出图像文件
- rest - 无法通过 JAVA REST API 连接到雪花
- c++ - C++:从文件中读取 xy 数据会创建无限循环?
- git - 如何构建符合 GDPR 的基于 git 的静态页面?
- c - typedef 的用途是什么,它的正确用途是什么?
- json - 将 JSON 转换为 CSV1,然后将输出与另一个 CSV2、vb.net 进行比较
- imagemagick - 如何将 dzi 文件转换为多瓦锥体 tiff 格式
- javascript - 我怎样才能让我的循环更实用?
- javascript - ReactJS 游戏 - 在组件之间切换 - 无法执行它
- python - 魔杖对象颜色创建非常慢