在 R 中计算滚动实现波动率的更快方法 [英] Faster Way of Calculating Rolling Realized Volatility in R

查看:34
本文介绍了在 R 中计算滚动实现波动率的更快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算一组指数的滚动 20 天实现波动率.这是我用来下载指数价格、计算每日回报和 20 天实现波动率的代码.

I want to calculate the rolling 20 day realized volatility for a collection of indices. Here is the code I use to download the index prices, calculate the daily returns and the 20 day realized volatility.

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)

一切都很快,直到最后一行.我没有计时,但它以分钟为单位,而我希望它只需要几秒钟.有没有更快的方法来计算已实现的波动率?

Everything works pretty quick until the final line. I haven't timed it but it is on the scale of minutes whereas I would expect it to take only seconds. Is there a faster way to calculate the realized volatility?

谢谢.

推荐答案

你可以在 TTR 包(由 quantmod 加载)中使用 runSD,但你需要应用 runSD到每一列,将apply的结果转回xts对象,手动将结果年化.

You can use runSD in the TTR package (which is loaded by quantmod), but you will need to apply runSD to each column, convert the result of apply back to an xts object, and manually annualize the result.

realized.vol <- xts(apply(index.ret,2,runSD,n=20), index(index.ret))*sqrt(252)

这篇关于在 R 中计算滚动实现波动率的更快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆