Constraints on weight in portfolio optimization using quadprog package in R -


i new using r , portfolio optimization. trying optimize portfolio 7 assets such asset number 3 , 4 have minimum weight of 0.35 each , sum of 7 assets equal 1. following code have tried:

library(quadprog) dmat <- cov(dr) #dr stores daily return of 7 assets , 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 didn't use transpose got error saying amat , dvec not compatible bvec <- matrix(c(1,0.35, 0.35), nrow =3) meq <- 1 sol <- solve.qp(dmat, dvec, amat, bvec, meq) 

here answer 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 solution has weights 2 assets in excess of 1, must have made mistake in amat or bvec or meq. however, unable figure out mistake.

could guide me on how construct matrices tackle problem? in advance help.

your answer sums 1 allowed of weights greater 1 did not restrict weights positive. if that's want, need add 1 constraint per variable. works:

dr <- matrix(runif(100*7), 100, 7) # made data 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 comments possibility short, sounds variables should bounded -1 , 1. 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 # [...] 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -