滚动 window 以计算 R 中的风险值

Rolling window to calculate Value at Risk in R

我确定这很明显,但我是 R 的初学者,我花了下午的大部分时间试图解决这个问题...

我正在尝试创建滚动 window 来计算随时间变化的风险价值 (VaR)。

我已经计算了我每天 7298 的整个时间序列的无条件 VaR returns。

现在,我想做的是滚动 window 计算 25 天 window 的 VaR,这将滚动我整个时间序列的每一个观察值。

我试过了

apply.rolling(nas, trim = TRUE, gap = 25, by = 1, FUN = function(x) VaR(R = nas, p = 0.99, method="historical"))

rollapply(nas, width = 25, FUN = function(x) VaR(R = nas, p = 0.99, method="historical"))

其中 nas 是我的时间序列。

我的代码仍然从一个小时前运行...我不知道我做错了什么...

非常感谢您提供的任何帮助。

H.

应该是:

rollapply(nas, width = 25, FUN = function(x) VaR(R = x, p = 0.99, method="historical"))

基本上,您应用的函数接受值 x(过滤 nas 到 25 个时间单位),并根据 x 生成输出。在您最初的尝试中,该函数是 function(x) VaR(R = nas, p = 0.99, method="historical"),因此它接受值 x,但仍然计算整个 nas 的 VaR,并且它执行了 >7000 次,因此需要永远.