我正在尝试针对不同的alpha值在我的时间序列上运行VaR模型,并始终提出错误Error in
[< - (
tmp {{ 1}}当我尝试以不同p值的矩阵形式得到我的结果时。如果我仅使用一个, i + seq[1], j, value = 0.09265792) : subscript out of bounds
运行模型且for
的固定值,我会收到正确的结果。为什么我得到这个错误,我在很多线程中都没有找到关于这个主题的解决方案!我模拟了我的时间序列以便重现!感谢
p
答案 0 :(得分:0)
我更改了代码如下:
我删除了seq
和vseq
,因为它们不需要。我将estperiod
重新定义为窗口的长度。已删除了内置循环(j
的循环)并替换为sapply
,同时计算所有p
的值,然后将其置于适当的位置行。您应该检查数学运算,以确保ret
的正确部分进入每个预测的VaRhistorical
函数。
#HS VaR function
VaRhistorical <- function(returns, prob=.05) {
ans <- -quantile(returns, prob)
signif(ans, digits=7)
}
#Parameter specification
ret <- runif(5000,-0.1,0.1)
p <- c(0.05, 0.025)#, 0.01, 0.005, 0.001)
estperiod<-500 #now its the length of the window
VaRhs <- matrix(nrow=length(ret)-estperiod, ncol=length(p))
for (i in 1:nrow(VaRhs) ) {
VaRhs[i,]<- sapply(X=p, FUN=VaRhistorical,returns=ret[i:(estperiod+i-1)] )
}
act <- ret[(estperiod+1):length(ret)]
violationratio <- ifelse(act>=(-VaRhs),0,1)
sum(violationratio)