11

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?

Thank you.

mchangun
  • 9,814
  • 18
  • 71
  • 101
  • 1
    I'd suggest using `ROC` to calculate returns; PerformanceAnalytics can't decide on what to make the defaults. `index.ret <- ROC(index, type='discrete', na.pad=FALSE)` – GSee Oct 10 '12 at 16:05
  • Thanks for the tip. I try and limit most of my functions to as few libraries as possible, in case there are incompatibilities or inconsistencies when I start mixing function from different libraries. Is this thinking of mine completely misguided? – mchangun Oct 10 '12 at 16:22
  • Not at all. The fewer libraries you have to load, the less time it takes to load libraries, the less likely your code is to break from something upstream, and the more likely it is that someone else can use your code without having to install a new package. – GSee Oct 10 '12 at 16:47

1 Answers1

12

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)
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • This works brilliantly - takes abt 2 seconds. Why is this so much faster than my approach? – mchangun Oct 10 '12 at 16:24
  • 1
    @mchangun: `runSD` does almost all the calculations in C, where your approach does most of the calculations in pure R. – Joshua Ulrich Oct 10 '12 at 16:27
  • @GSee: One of those. I have been in the process of moving them from Fortran to C for quite some time. Eventually the run* functions in TTR will be moved to roll* functions in zoo and xts. – Joshua Ulrich Oct 10 '12 at 16:34
  • @JoshuaUlrich this may be a bit off topic, but this realized volatility is actually the historical volatility, right ? I am asking because there's a whole bunch of models estimating the unknown realized volatility; or I might have confused myself with the jargons.. – stucash Jul 27 '20 at 13:46
  • @stucash: you are right. The models that estimate 'unknown realized volatility' are implied volatility models and use options data to forecast realized volatility. – Joshua Ulrich Jul 27 '20 at 14:44
  • @JoshuaUlrich Thanks for confirming back! – stucash Jul 27 '20 at 14:46
  • @JoshuaUlrich- why the n=20. why not just start with n=2??? Thank you for any clarity that you could offer me – Kevin K Aug 30 '21 at 02:34