I would like to find the minimum variance portfolio for 3 risky assets where the sum of the weights of all assets = 2 and weight of asset1 is set at +1 (i.e the problem would be to minimise the portfolio vol by adjusting only the weights for asset asset 2 and 3 subject to them not being more than 1 and >=0. I have found the below snippet of code that minimise the risk for a 3 asset portfolio, but I am unsure about how to incorporate the weight constraints. Any help / suggestions are welcome.
covmat <- matrix(c(3.235343e-02, -3.378191e-03, -1.544574e-05,
-3.378191e-03, 8.769166e-03, 1.951734e-06,
-1.544574e-05, 1.951734e-06, 2.186799e-06),3,3)
mat <- rbind(cbind(2*covmat,rep(1, 3)), c(rep(1, 3), 0))
vec <- c(rep(0, 3),1)
smat <- solve(mat)%*%vec
smat[1:3,1]
Thank you,
R has a QP (quadratic programming) solver that is designed for these type of problems:
library(quadprog)
covmat <- matrix(c(3.235343e-02, -3.378191e-03, -1.544574e-05,
-3.378191e-03, 8.769166e-03, 1.951734e-06,
-1.544574e-05, 1.951734e-06, 2.186799e-06),3,3)
# linear constraint matrix
A <- rbind(c(1,1,1),diag(3))
# rhs
b <- c(2,1,0,0)
# solve QP model
solve.QP(covmat,dvec=rep(0,3),Amat=t(A),bvec=b,meq=2)$solution
Output:
[1] 1.0000000 0.3835757 0.6164243