I just install the latest version of cvxpy using pip install and am working my way through the examples provided at http://nbviewer.jupyter.org/github/cvxgrp/cvx_short_course/blob/master/applications/portfolio_optimization.ipynb The code does not work for me. When I run the following:
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])
I get the following error:
---------------------------------------------------------------------------
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
I have tried using e.g., cvx.sum_entries as mentioned here, but it did not work.
Can anyone help?
sum_entries
was renamed to sum
in 1.0.
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, ())