0

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 find 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'])
Marco_sbt
  • 309
  • 1
  • 12
  • If you move your question to another stack site https://or.stackexchange.com/, maybe you can find an answer. – kur ag Mar 01 '20 at 15:52
  • The error means P is not positive semidefinite. Impossible to say more since we don't have your full input data. – Michal Adamaszek Mar 01 '20 at 16:23
  • @MichalAdamaszek thanks, I know what the error means but if P is not positive semidefinite, how can the solver in CVXOPT using KKT can find a solution? in the case of KKT shouldn't return an error like KKT singular matrix as for negative eigenvalues? I can post a txt file with the var_covar matrix if helps answer my question – Marco_sbt Mar 01 '20 at 16:30
  • @kurag thanks, have posted the same question there – Marco_sbt Mar 01 '20 at 16:45
  • @Marco_sbt Have you checked P's eigenvalues? Maybe there is a tiny negative eigenvalue and Mosek cannot find the Cholesky factorization, or some other numerical issue. Hard to say. – Michal Adamaszek Mar 01 '20 at 17:00
  • @MichalAdamaszek The Var_cov matrix is for 738 odd security (all equities). I did not check for P's eigenvalues but this was my thought, a tiny negative eigenvalue where the Cholesky factorization cannot be found. I thought I could have still used Mosek to run the optimisation by simply transforming the matrix so that there were no negative eigenvalues but I am unsure how that can be done. As said if can help solving my issue I can post the whole matrix I tried to relax the convexity check by using the following parameter but it didn't work either MSK_DPAR_CHECK_CONVEXITY_REL_TOL – Marco_sbt Mar 01 '20 at 17:10
  • @MichalAdamaszek there are 11 negative eigenvalues – Marco_sbt Mar 01 '20 at 17:38
  • There is an alternative for the standard mean-variance portfolio model that uses mean-adjusted returns. This will always yield a positive definite Q matrix. See [link](https://yetanothermathprogrammingconsultant.blogspot.com/2018/04/covariance-matrix-not-positive-definite.html). – Erwin Kalvelagen Mar 01 '20 at 17:47
  • @ErwinKalvelagen thanks for this suggestion! I will surely try this as it looks like will solve any problem – Marco_sbt Mar 01 '20 at 17:55

0 Answers0