我只是使用pip install安装了最新版本的cvxpy,并且正在通过http://nbviewer.jupyter.org/github/cvxgrp/cvx_short_course/blob/master/applications/portfolio_optimization.ipynb中提供的示例进行工作 该代码对我不起作用。当我运行以下命令时:
import numpy as np
np.random.seed(1)
n = 10
mu = np.abs(np.random.randn(n, 1))
Sigma = np.random.randn(n, n)
Sigma = Sigma.T.dot(Sigma)
# Long only portfolio optimization.
from cvxpy import *
w = Variable(n)
gamma = Parameter(nonneg=True)
ret = mu.T*w
risk = quad_form(w, Sigma)
prob = Problem(Maximize(ret - gamma*risk),
[sum_entries(w) == 1,
w >= 0])
我收到以下错误:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-2-fde65e194bce> in <module>
14 risk = quad_form(w, Sigma)
15 prob = Problem(Maximize(ret - gamma*risk),
---> 16 [sum_entries(w) == 1,
17 w >= 0])
NameError: name 'sum_entries' is not defined
我曾尝试使用here中提到的cvx.sum_entries,但没有用。
有人可以帮忙吗?
答案 0 :(得分:5)
sum_entries
在1.0中重命名为sum
。
https://www.cvxpy.org/updates/index.html#numpy-compatibility
In [1]: from cvxpy import *
In [2]: x = Variable(10)
In [3]: sum(x)
Out[3]: Expression(AFFINE, UNKNOWN, ())