Constraints on weight in portfolio optimization using quadprog package in R

前端 未结 1 1052
别那么骄傲
别那么骄傲 2021-01-13 19:40

I am new to using R and portfolio optimization. I am trying to optimize a portfolio with 7 assets such that asset number 3 and 4 have a minimum weight of 0.35 each and the s

相关标签:
1条回答
  • 2021-01-13 19:57

    Your answer sums to one but what allowed some of the weights to be greater than one is that you did not restrict your weights to be positive. If that's what you want, you need to add one constraint per variable. This 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 about the possibility to short, it now sounds like your variables should be bounded by -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
    # [...]
    
    0 讨论(0)
提交回复
热议问题