I have written a small code to do a simple min variance optimisation using CVXOPT, you can see the whole code below
By using solvers.qp(P, q, G, h, A, b)
in CVXOPT the code runs fine and it finds a solution
solvers.qp(P, q, G, h, A, b)
I wanted to try a different solver too, hence I used MOSEK by solving the same problem with the following parameters
solvers.qp(P, q, G, h, A, b, solver='mosek')
When using solver='mosek'
the code cannot run and it is giving me the following error
MOSEK error 1295: The quadratic coefficient matrix in the objective is not positive semidefinite as expected for a minimization problem
Can anyone explain why I got this error (have I coded something in the wrong way?) and if there is a workaround to solve the issue I am facing with MOSEK?
import numpy as np
import cvxopt as opt
import mosek
from cvxopt import matrix, solvers
def optimize_portfolio(n, Var_Cov):
P = opt.matrix (Var_Cov)
q = opt.matrix(np.matrix(np.zeros((n, 1))))
G = opt.matrix(np.array(-np.identity(n)))
h = opt.matrix(np.zeros((n,1)))
A = opt.matrix(1.0, (1,n))
b = opt.matrix(1.0)
# Finding a solution
sol = solvers.qp(P, q, G, h, A, b, solver='mosek')
return sol
### Parameters setup
Var_Cov = np.loadtxt('C:\VAR_COV.txt')
n = len (Var_Cov)
### solve
solution = optimize_portfolio(n, Var_Cov)
# Save Results
Port_Opt = np.matrix(solution['x'])