I read some posts about portfolio optimization with quadprog and i learned many tricks from this platform. Now i am trying to optimize a portfolio of 03 stocks with quadprog under the constrains i.e.,.
- Weights must sum to 1
- No short selling
- portfolio return = 2%
- Each stock weight must not exceed 50% of the total weight
The covariancce matrix for my 3 stock is
Dmat = matrix(c(0.0119, 0.0071, -0.0023,0.0071, 0.0093,
-0.0006,-0.0023,-0.0006,0.0066), nrow = 3)
The stock returns/colmeans are stored in Dvec
Dvec = c(0.0373, 0.0173, 0.0261)
I used the code and procedure mentioned in this post Portfolio Optimization constraints Matrix/bvec explanation. I also read the similar posts Portfolio Optimization constraints Matrix/bvec explanation and Constraints on weight in portfolio optimization using quadprog package in R So the code i tried looks like this
N = 3
total.returns = 0.02
lo = rep(0,N)
up = rep(0.50,N)
A = rbind(rep(1,N), dvec, diag(N), -diag(N))
A = t(A)
B = c(1,total.returns, lo, -up)
neq = 1
qp = solve.QP(Dmat, dvec, A, B,neq)
qp$solution
I start the analysis with two equalities i.e. one for hundred percent investment and the other for portfolio returns. I read it in another post that the returns may be causing problem such that the returns we are looking for are not possible with the current data and constrains. So i also change the return to different level but it does not work so i relaxed the return equality and use neq=1. Still it is not working. I also tried different weights constrains on individual stocks i.e. i tried to change upper limit (up) from 5% to 80%.
NOTE: When i write the amat and bvev with out the total returns then the code works. i.e. when i use A and B as
A = rbind(rep(1,N), diag(N), -diag(N))
B = c(1, lo, -up)
instead of
A = rbind(rep(1,N), dvec, diag(N), -diag(N)) and
B = c(1,total.returns, lo, -up).
My question is why i am getting the "constraints are inconsistent, no solution" when i use my code mentioned above? I will be thankful if some one can figure out what mistake i am doing? Thank you so much in advance to all of you.