0

I used the CVXR library to find the weights of a minimum variance portfolio consisting of 183 assets.

(1) min(w) (1/2)*((w)^T)*sigma*(w)

(2) s.t. sum(w) ==1

(3) w >= 0

  • “Sigma” is the matrix of variances and covariances and has dimension 183*183. The matrix of variances and covariances is semidefinite positive and was estimated one using historical data of security returns (313 return per security).

  • “w” are the weights of the assets in the minimum variance portfolio.

I tried to fix this in code below, but nothing was useful.

library(readxl)
library(matrixcalc)
Library(CVXR)

Return <- read_excel ("datasets.xlsx")
Return <- as.data.frame(Return)

sigma <- cov(Return)
is.positive.definite(sigma)
[1] TRUE


#quadratic programming

w <- Variable(183)

objective <- Minimize((1/2) %*% (quad_form(w, sigma)))

constraints <- list( w >= 0,   sum(w) == 1 )

problem <- Problem(objective, constraints)

soln <- solve(problem)

b <- soln$getValue(w)


#verify

sum(b)
[1] 1

min(b)
[1] -5.781906e-22

Constraint (2) is verified, while constraint (3) is not verified. Despite the no-short-selling constraint (3), I obtained weights of securities with negative value. What am I doing wrong?

  • See https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f . And try `all.equal(-5.781906e-22, 0)`. – Enrico Schumann Jun 12 '23 at 07:15
  • All solvers use **tolerances**. So a solution can be a very small amount outside its bounds. `-5.781906e-22` is well within a normal feasibility tolerance. For the exact tolerance a solver is using, see the solver documentation. – Erwin Kalvelagen Jun 12 '23 at 10:57

0 Answers0