R - Portfolio Optimization - solve.QP - Constraints are incompatible

I am trying to use solve.QP to solve portfolio optimization problem (quadratic problem)

Only 3 assets

There are 4 limitations:

  • sum of weights equal to 1
  • expected portfolio return is 5.2%
  • the weight of each asset is greater than 0
  • each asset weight is less than .5

Dmat - covariance matrix

Dmat <- matrix(c(356.25808, 12.31581, 261.8830, 212.31581, 27.24840, 18.50515, 261.88302, 18.50515,535.45960), nrow=3, ncol=3) 

dvec - each asset, expected return

 dvec <- matrix(c(9.33, 3.33, 9.07), nrow=3, ncol=1) 

Amat - matrix of restrictions

 A.Equality <- matrix(c(1,1,1), ncol=1) Amat <- cbind(A.Equality, dvec, diag(3), -diag(3)) 

constraint A ^ T b> = b_0, b vector

 bvec <- c(1, 5.2, rep(0, 3), rep(-0.5, 3)) 

meq = 2, since there are two equality constraints, the first and second constraints are equality

Then I ran the solve.QP function

 library(quadprog) qp <- solve.QP(Dmat, dvec, Amat, bvec, meq=2) 

But he gives an error

 Error in solve.QP(Dmat, dvec, Amat, bvec, meq = 2) : constraints are inconsistent, no solution! 

I'm not sure where I made a mistake.

+8
r mathematical-optimization quadratic portfolio
source share
1 answer

There were two questions in the code you posted:

  • The published Dmat not actually symmetrical; You accidentally included the price of 212.31581 instead of 12.31581.
  • The meq=2 parameter means that your first two restrictions are kept equal, that is, your weights add up to 1, and your return is exactly 5.2%. The second limitation seems to be related to impossibility; it seems that there are no valid portfolios that have a return of 5.2%, given your other limitations. Indeed, since no more than half of the portfolio can have an income of 3.33%, and the rest must have an income of at least 9.07%, the return should be 6.2% or more. Therefore, you should reduce this value to the limit → by setting meq=1 .

Here's the working code:

 library(quadprog) Dmat <- matrix(c(356.25808, 12.31581, 261.88302, 12.31581, 27.24840, 18.50515, 261.88302, 18.50515,535.45960), nrow=3, ncol=3) dvec <- matrix(c(9.33, 3.33, 9.07), nrow=3, ncol=1) A.Equality <- matrix(c(1,1,1), ncol=1) Amat <- cbind(A.Equality, dvec, diag(3), -diag(3)) bvec <- c(1, 5.2, rep(0, 3), rep(-0.5, 3)) qp <- solve.QP(Dmat, dvec, Amat, bvec, meq=1) qp$solution # [1] 0.3808733 0.5000000 0.1191267 

The optimal solution is actually associated with a return of 6.3%.

+9
source share

All Articles