我想计算一组指数的滚动 20 天实际波动率。这是我用来下载指数价格、计算每日收益和 20 天实际波动率的代码。
library(quantmod)
library(PerformanceAnalytics)
tickers = c("^RUT","^STOXX50E","^HSI", "^N225", "^KS11")
myEnv <- new.env()
getSymbols(tickers, src='yahoo', from = "2003-01-01", env = myEnv)
index <- do.call(merge, c(eapply(myEnv, Ad), all=FALSE))
#Calculate daily returns for all indices and convert to arithmetic returns
index.ret <- exp(CalculateReturns(index,method="compound")) - 1
index.ret[1,] <- 0
#Calculate realized volatility
realizedvol <- rollapply(index.ret, width = 20, FUN=sd.annualized)
一切都很快,直到最后一行。我没有计时,但它是在几分钟的范围内,而我希望它只需要几秒钟。是否有更快的方法来计算已实现的波动率?
谢谢你。