使用R中的Quadprog软件包进行投资组合优化时的权重约束 [英] Constraints on weight in portfolio optimization using quadprog package in R

查看:287
本文介绍了使用R中的Quadprog软件包进行投资组合优化时的权重约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用R和投资组合优化的新手.我正在尝试优化具有7种资产的投资组合,以使资产编号3和4的最小权重分别为0.35,并且所有7个资产的总和等于1. 以下是我尝试过的代码:

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 sum of all 7 assets equal to 1. Following is the code I have 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 that 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

由于解决方案的2个资产的权重超过1,因此我肯定在Amat或bvec或meq中犯了一个错误.但是,我无法弄清楚那是什么错误.

Since the solution has weights for 2 assets in excess of 1, I must have made a mistake in the Amat or bvec or meq. However, I am unable to figure out what is that mistake.

有人可以指导我如何构造这些矩阵来解决这个问题吗?预先感谢您的帮助.

Could someone guide me on how to construct those matrices to tackle this problem? Thanks in advance for any help.

推荐答案

您的答案为1,但是某些权重大于1的原因是您没有将权重限制为正.如果这是您想要的,则需要为每个变量添加一个约束.这有效:

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
# [...]

在对短路可能性发表评论之后,现在听起来您的变量应以-1和1为界.然后使用:

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
# [...]

这篇关于使用R中的Quadprog软件包进行投资组合优化时的权重约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆