I recently got very interested in portfolio optimization and started playing around in R, to create a minimum variance portfolio,
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)
Example above has the following constraints ( example from here)
There are 4 constraints:
- sum of weights equal to 1
- portfolio expected return equals to 5.2%
- each asset weight greater than 0
- each asset weight smaller than .5
I am currently trying to refresh my matrix/vector math, I would really appreciate if someone could tell me how you add the individual constraints together in aMat and bvec and the basic algebra background for it. And as another question how a constraint for weights <0 (shorting) would look like.
thanks in advance
First step is to write down the mathematical model. That could look like:
The next part is to implement this in R's quadprog. That could look like: