Weight restrictions in portfolio optimization using the quadprog package in R

I am new to using R and portfolio optimization. I am trying to optimize a portfolio with 7 assets, so that assets 3 and 4 have a minimum weight of 0.35 each, and the sum of all 7 assets is 1. Below is the code I tried:

library(quadprog) dmat <- cov(dr) #dr stores the daily return of the 7 assets and is a timeSeries object dvec <- colMeans(dr) c1 <- c(0,0,1,0,0,0,0) c2 <- c(0,0,0,1,0,0,0) amat <- t(rbind(matrix(1, ncol = ncol(dmat)), c1, c2)) #used transpose because earlier when I didn't use the transpose I got an error saying amat and dvec are not compatible bvec <- matrix(c(1,0.35, 0.35), nrow =3) meq <- 1 sol <- solve.QP(dmat, dvec, amat, bvec, meq) 

Here is the answer I get from the above code:

 $solution [1] -0.01619018 -2.10640140 0.35000000 0.35000000 -0.82522310 1.27499728 1.97281741 $value [1] -0.0007364101 $unconstrained.solution [1] 0.026872891 12.595238193 -0.256430652 0.008918392 0.743618974 2.212816019 3.749097189 $iterations [1] 4 0 $Lagrangian [1] 0.0002874682 0.0002846590 0.0003015167 $iact [1] 1 3 2 

Since the solution has weight for 2 assets in excess of 1, I must have made a mistake in Amat or bvec or meq. However, I can’t understand what this error is.

Can someone help me build these matrices to solve this problem? Thanks in advance for any help.

+4
source share
1 answer

Your answer sums up to one, but what allowed some weight to be more than one is that you did not limit your weight to positive. If this is what you want, you need to add one constraint to the variable. It works:

 dr <- matrix(runif(100*7), 100, 7) # made up data for this example n <- ncol(dmat) dmat <- cov(dr) dvec <- colMeans(dr) c1 <- c(0,0,1,0,0,0,0) c2 <- c(0,0,0,1,0,0,0) amat <- t(rbind(matrix(1, ncol = n), c1, c2, diag(n))) bvec <- c(1, 0.35, 0.35, rep(0, n)) meq <- 1 solve.QP(dmat, dvec, amat, bvec, meq) # $solution # [1] 0.0000000 0.0291363 0.3500000 0.4011211 0.0000000 # [6] 0.0000000 0.2197425 # [...] 

Following your comments on the possibility of a short one, it now sounds like your variables should be limited to -1 and 1. Then use:

 amat <- t(rbind(matrix(1, ncol = n), c1, c2, diag(n), -diag(n))) bvec <- c(1, 0.35, 0.35, rep(-1, n), -rep(1, n)) solve.QP(dmat, dvec, amat, bvec, meq) # $solution # [1] -0.51612776 0.30663800 0.35000000 0.54045253 -0.14679397 # [6] 0.02342572 0.44240548 # [...] 
+2
source

Source: https://habr.com/ru/post/1480907/


All Articles