【发布时间】:2014-07-18 17:41:40
【问题描述】:
我正在使用 slove.QP 解决二次问题(资产分配问题)。我对不同的预期回报范围有四个限制。例如,对于从 6.1 到 6.5 的返回范围,我对这个范围进行了约束,对于从 6.6 到 7.0 的范围,我对其进行了另一个约束,等等。
我创建了一个约束矩阵,它看起来像这样:
min max min max min max min max
.. .. .. .. .. .. .. ..
.. .. .. .. .. .. .. ..
.. .. .. .. .. .. .. ..
.. .. .. .. .. .. .. ..
......
循环中的mincons包含约束矩阵的所有最小列,maxcons包含约束矩阵的所有最大列。
例如,对于第一个循环,预期返回范围从 6.1 到 7.0,我在 solve.QP 函数中使用 mincons[,1] 和 maxcons[,1] 作为约束。同样的事情适用于接下来的三个循环。
但是 R 一直给我“下标越界”错误。我已经在 stackoverflow 上阅读了一些类似的问题,但我仍然无法弄清楚为什么会遇到此错误。任何人都可以帮助我。谢谢。
You can download dataconstraints.csv and datacorrelations.csv at
https://www.dropbox.com/s/6bosunahysdfpcj/dataconstraints.csv
https://www.dropbox.com/s/vb94obm83lttdej/datacorrelations.csv
library(quadprog)
mydata = read.csv("dataconstraints.csv")
er <- matrix(mydata[,1], nrow=23, ncol=1)
stdevs <- matrix(mydata[,2], nrow=23, ncol=1)
min <- mydata[,c(FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE)]
mincons <- as.matrix(sapply(min, as.numeric))
max <- -mydata[,c(FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE)]
maxcons <- as.matrix(sapply(max, as.numeric))
mycorr = read.csv("datacorrelations.csv")
correlation <- mycorr
b <- stdevs %*% t(stdevs)
covariance <- b * correlation
dvec <- er
Dmat <- as.matrix(sapply(covariance, as.numeric))
A.Equality <- matrix(c(1), nrow=length(er), ncol=1)
Amat <- cbind(A.Equality, er, diag(length(er)),-diag(length(er)))
port_ret1 = NULL
bvec1 = matrix(data=NA, nrow=5, ncol=48)
w1 = matrix(data=NA, nrow=5, ncol=23)
sd1 = matrix(data=NA, nrow=5, ncol=1)
ret1 = matrix(data=NA, nrow=5, ncol=1)
port_ret2 = NULL
bvec2 = matrix(data=NA, nrow=5, ncol=48)
w2 = matrix(data=NA, nrow=5, ncol=23)
sd2 = matrix(data=NA, nrow=5, ncol=1)
ret2 = matrix(data=NA, nrow=5, ncol=1)
port_ret3 = NULL
bvec3 = matrix(data=NA, nrow=5, ncol=48)
w3 = matrix(data=NA, nrow=5, ncol=23)
sd3 = matrix(data=NA, nrow=5, ncol=1)
ret3 = matrix(data=NA, nrow=5, ncol=1)
port_ret4 = NULL
bvec4 = matrix(data=NA, nrow=5, ncol=48)
w4 = matrix(data=NA, nrow=5, ncol=23)
sd4 = matrix(data=NA, nrow=5, ncol=1)
ret4 = matrix(data=NA, nrow=5, ncol=1)
n <- (8.0-6.1)/0.1+1
for(i in 1:n){
if(i>=1 & i<=5){
port_ret1[i]<-6.1+0.1*(i-1)
bvec1[i,] <- c(1, port_ret1[i], mincons[,1], maxcons[,1])
w1[i,] <- solve.QP(Dmat, dvec, Amat, bvec1[i,], meq=1)$solution
sd1[i,] <- sqrt(w1[i,] %*% Dmat %*% w1[i,])
ret1[i,] <- w1[i,] %*% er
}
if(i>=6 & i<=10){
port_ret2[i]<-6.1+0.1*(i-1)
bvec2[i,] <- c(1, port_ret2[i], mincons[,2], maxcons[,2])
w2[i,] <- solve.QP(Dmat, dvec, Amat, bvec2[i,], meq=1)$solution
sd2[i,] <- sqrt(w2[i,] %*% Dmat %*% w2[i,])
ret2[i,] <- w2[i,] %*% er
}
if(i>=11 & i<=15){
port_ret3[i]<-6.1+0.1*(i-1)
bvec3[i,] <- c(1, port_ret3[i], mincons[,3], maxcons[,3])
w3[i,] <- solve.QP(Dmat, dvec, Amat, bvec3[i,], meq=1)$solution
sd3[i,] <- sqrt(w3[i,] %*% Dmat %*% w3[i,])
ret3[i,] <- w3[i,] %*% er
}
if(i>=16 & i<=20){
port_ret4[i]<-6.1+0.1*(i-1)
bvec4[i,] <- c(1, port_ret4[i], mincons[,4], maxcons[,4])
w4[i,] <- solve.QP(Dmat, dvec, Amat, bvec4[i,], meq=1)$solution
sd4[i,] <- sqrt(w4[i,] %*% Dmat %*% w4[i,])
ret4[i,] <- w4[i,] %*% er
}
}
Error in `[<-`(`*tmp*`, i, , value = c(1, 6.6, 0, 0, 0, 0, 0, 0, 0, 0, :
subscript out of bounds
error 看起来像这样,所以我猜第二个 if 语句越界了。如果我只运行第一个 if 语句,它运行正常,没有错误。我只是看不到任何越界的问题。
【问题讨论】:
-
您的代码不可执行 - 请更新它以包含
mincons、maxcons、Dmat,并仔细检查以确保没有其他任何遗漏。您还应该在顶部添加library(quadprog),因为您的示例使用函数solve.QP。将来,您应该在发布之前在新的R会话中测试您的代码,以确保它可以正常工作。 -
感谢 cmets。我刚刚在我的代码中添加了所有内容。