我的名字是Grégory,我正在尝试使用以下约束来计算最小方差组合:
0< =资产重量< = 5%(不允许卖空,最大资产重量为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)
}
当我查看MV组合权重时,所有资产权重都等于0,所以我不知道错误的来源。
如果你能帮助我,我将非常感激。
非常感谢,
亲切的问候,
了Grégory
答案 0 :(得分:0)
看看这个脚本。
O(nlogn)
在下面的链接中有一个很好的解释如何工作。
http://economistatlarge.com/portfolio-theory/r-optimized-portfolio