2
votes

I am trying to minimize the portfolio variance using Python's cvxopt. However, after lots of trying, it doesn't seem to work. The function and my code and the error are pasted below. Thanks for helping!

the minimize problem

objective function: min x.dot(sigma_mv).dot(x.T)

the constraint condition is all x>=0, sum(X) = 1

sigma_mv is the covariance matrix of 800*800, dim = 800

code

dim = sigma_mv.shape[0]
P = 2*sigma_mv   
q = np.matrix([0.0])
G = -1*np.identity(dim)
h = np.matrix(np.zeros((dim,1)))

sol = solvers.qp(P,q,G,h)

Traceback (most recent call last):

  File "<ipython-input-47-a077fa141ad2>", line 6, in <module>
    sol = solvers.qp(P,q)   

  File "D:\spyder\lib\site-packages\cvxopt\coneprog.py", line 4470, in qp
    return coneqp(P, q, G, h, None, A,  b, initvals, kktsolver = kktsolver, options = options)

  File "D:\spyder\lib\site-packages\cvxopt\coneprog.py", line 1822, in coneqp
    raise ValueError("use of function valued P, G, A requires a "\

ValueError: use of function valued P, G, A requires a user-provided kktsolver
1
Welcome to the site: you may want to read help center, How to Ask and minimal reproducible example, and re-word your question accordingly.boardrider
The error tells you that cvxopt is not able to see you input as matrices. It thinks some of these are functions. As your code is incomplete (no def of sigma_mv; your description does not help here) it's hard to reason about. So either, sigma_mv is broken, or, and i'm too lazy to check: does cvxopt really can use numpy.matrices? cvxopt usually has it's own matrix-format (there are even numpy -> cvxopt transformation examples somewhere in the docs).sascha
thanks for your answer, it really solves the problem. The P Q G H is not the correct matrix form defined in the cvxopt. the correct form is as following: from cvxopt import solvers, matrix P = 2*matrix(mat) q = matrix([0.0]*dim) G = -1*matrix(np.identity(dim)) h = matrix(np.zeros((dim,1)))Y.cady
also np.array(1.0) * matrix(G) is not a matrixuser3226167
cvxopt is a great solver. We just need to take care that our P matrix is positive definite or any other important considerations.farshad-nsh

1 Answers

0
votes

You have both equality and inequality constraints so you need to provide all the arguments to the built-in qp solver Gx <=h Ax=b

Here x>=0 can be written as -x<=0 So, G matrix will look like -1*(Identity matrix) and h will a 0 vector Similarly, your A will be an Identity matrix and b will be a unity vector(all elements =1)

Finally, solve expression should look like :

sol=solvers.qp(P, q, G, h, A, b)