I am working on boyd MOOC, CVX101, I am trying to do the third homework in Python and I am encoutering a few problems
We have to solve the following program
We generate the data with:
# ---- Data Generation ---- # np.random.seed(5) n = 20 # Covariance matrix S = np.random.rand(n,n) S = S.T.dot(S) S = S/np.max(np.abs(np.diag(S)))*.2 S[:,-1] = 0 S[-1,:] = 0 # Uniform weight vector x_unif = np.ones((n,1)) / n # Price vector pbar = np.ones((n,1))*.03 + np.array(np.append(np.random.rand(n-1,1),0)).reshape((-1,1))*.12 And what I did
from cvxpy import quad_form from cvxpy import sum as cvxsum x_unconstrained = cp.Variable(n) constraints = [cvxsum(x_unconstrained) == 1, pbar.T * x_unconstrained == x_unif.T * pbar ] obj_3 = cp.Minimize(quad_form(x_unconstrained, S)) prob = cp.Problem(obj_3, constraints) prob.solve() print("status:", prob.status) print("optimal value", prob.value) print("optimal var", x_unconstrained.value) This is the result I got
status: infeasible optimal value inf optimal var None As a side note I have the solution in matlab
simple_portfolio_data; %% part i %minimum-risk unconstrained portfolio %with same expected return as uniform %allocation cvx_begin cvx_quiet(true) variable x_unconstrained(n) minimize(quad_form(x_unconstrained,S)) subject to sum(x_unconstrained)==1; pbar’*x_unconstrained==x_unif’*pbar; cvx_end 