我目前正在使用Rstudio工作。我正在做一些VaR模型然后我会对它们进行回测。我现在已经完成了一个VaR计算,执行滚动窗口(固定一个),如下所示:
fit_st_dowjones <- function(x){
fit_dstd_dowjones <- fitdist(x , dstd,
start = list(mean = mean(x),
sd= sd(x), nu= 2.5),
lower = c (-Inf , 0, 2),
upper = c (Inf, Inf, Inf),
control = list (trace= FALSE,
report = TRUE,
macxit = 1000))
return(fit_dstd_dowjones$estimate)
}
rf_dstd_dow_jones <- function(dowjones_returns,
rolling_window){
rwret_dstd_dowjones <- rollapply(dowjones_returns$ret,
width = rolling_window, FUN=fit_st_dowjones)
rwdates_dstd_dowjones <- rollapply(dowjones_returns$Date,
width = rolling_window,
FUN = function(x) x[length(x)])
result_dstd_dowjones <- bind_cols(as_data_frame(rwdates_dstd_dowjones),
as_data_frame(rwret_dstd_dowjones))
return(result_dstd_dowjones)
}
rw_dstd_dowjones <- rf_dstd_dow_jones(dowjones_returns,
rolling_window=2500)
并将VaR计算为:
var_rw_dstd_dowjones <- rw_dstd_dowjones %>%
mutate(var_dstd_dowjones = qstd(0.05, mean = mean, sd = sd, nu = nu))
现在我想使用包VaRTest
中的rugarch
来执行bactest但我需要从实际返回中获取数据框的值以匹配滚动数据框中的值窗口VaR估计以便比较它们。
我需要匹配的两个数据帧如下:
> head(var_rw_dstd_dowjones)
# A tibble: 6 × 5
value mean sd nu var_dstd_dowjones
<chr> <dbl> <dbl> <dbl> <dbl>
1 26-06-1975 -6.201075e-05 0.008824476 4.357486 -0.01357950
2 27-06-1975 -6.456846e-05 0.008826638 4.350107 -0.01358159
3 30-06-1975 -6.546042e-05 0.008824543 4.355752 -0.01358217
4 01-07-1975 -6.817885e-05 0.008826171 4.350462 -0.01358467
5 02-07-1975 -7.324408e-05 0.008827353 4.359455 -0.01359615
6 03-07-1975 -7.019445e-05 0.008823156 4.360549 -0.01358722
> head(dowjones_returns)
# A tibble: 6 × 4
Date index price ret
<chr> <chr> <dbl> <dbl>
1 05-01-1965 DOW_JONES_INDUSTRIALS 875.86 0.006990273
2 06-01-1965 DOW_JONES_INDUSTRIALS 879.68 0.004361428
3 07-01-1965 DOW_JONES_INDUSTRIALS 884.36 0.005320116
4 08-01-1965 DOW_JONES_INDUSTRIALS 882.60 -0.001990140
5 11-01-1965 DOW_JONES_INDUSTRIALS 883.22 0.000702470
6 12-01-1965 DOW_JONES_INDUSTRIALS 885.89 0.003023029
我正在使用的包裹: 需要(readr) 需要(dplyr) 需要(tidyr) 需要(fBasics) 需要(fitdistrplus) 需要(fGarch) 需要(actuar) 需要(动物园) 要求(rugarch)
我想要完成的事情:
> VaRTest(alpha = 0.05, actual = dowjones_returns$ret, VaR = var_rw_dstd_dowjones$var_dstd_dowjones, conf.level = 0.95)
和错误消息:
Error in VaRTest(alpha = 0.05, actual = dowjones_returns$ret, VaR = var_rw_dstd_dowjones$var_dstd_dowjones, :
length of realized not equal to length of VaR!