我是 R 和投资组合优化的新手。我正在尝试优化包含 7 种资产的投资组合,使得资产号 3 和 4 的最小权重均为 0.35,并且所有 7 种资产的总和等于 1。以下是我尝试过的代码:
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 <- …Run Code Online (Sandbox Code Playgroud)