My name is Grégory and I am trying to compute a Minimum Variance portfolio with the following constraints:
- Sum of the weights lower or equal to 1 (the portfolio can be fully invested, but it's not an obligation)
- Sum of the weights higher or equal to 0 (the portfolio can be fully in cash, but it's not an obligation)
0<= Asset weight <= 5% (no short-sales are allowed, and the maximum asset weight is 5%)
MV<-function (Returns, percentage = TRUE, ...) { if (is.null(dim(Returns))) { stop("Argument for 'Returns' must be rectangular.\n") } call <- match.call() V <- cov(Returns, ...) V <- make.positive.definite(V) N <- ncol(Returns) a1 <- rep(-1, N) b1 <- -1 a2 <- diag(N) b2 <- rep(0, N) c1<- -diag(N) ## This has been added to the model (to say: min 5%) c2<-rep(-0.05,N) ## This has been added to the model (to say: min 5%) c3<- rep(1,N) c4<- 0 Amat <- cbind(a1,c3,a2,c1) ### Corresponds to the matrix defining the different constraints Bvec <- c(b1,c4,b2,c2) ### Corresponds to the vector of constraints Dvec <- rep(0, N) ### Set to 0 because the first term of the routine must be equal to 0 #meq<- c(1,1,rep(1,N), rep(1,N)) opt <- solve.QP(Dmat = 2 * V, dvec = Dvec, Amat = Amat, bvec = Bvec, meq =0) w_mv <- opt$solution names(w_mv) <- colnames(Returns) if (percentage) w_mv <- w_mv * 100 return(w_mv) }
When I look at the MV portfolio weights, all the asset weights are equal to 0, so I don't know where the error comes from.
I would be very grateful if you could help me.
Many thanks in advance,
Kind regards,
Grégory
Take a look at this script.
There is a good explanation of how this works at the link below.
http://economistatlarge.com/portfolio-theory/r-optimized-portfolio