Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

Rollapply for time series

$
0
0

I am trying to calculate the rolling 20 period historical volatility. I take the daily returns:

ret<-ROC(data1)

And then I use rollapply to get the 20 day HV for each column:

vol<-rollapply(ret,20,sd,by.column=T,fill=NA)

The problem is that observations in vol starts appearing after ten days which is wrong as I specified 20.

For demonstration here is sample of the data:

0.000000000, 0.005277045, 0.023622047, 0.002564103,-0.002557545, -0.020512821,
0.007853403,-0.012987013,  0.007894737,  0.015665796,  0.000000000, -0.002570694,
0.002577320, -0.015424165, 0.002610966,  0.010416667,  0.002577320,  0.015424165, 
0.000000000, -0.002531646, -0.002538071, 0.030534351,  0.014814815, -0.007299270,
-0.009803922, -0.012376238,  0.002506266, -0.015000000,-0.002538071,  0.002544529

Assume the data above is stored in x, then:

rollapply(x,20,sd,fill=NA)

will yield a first observation at 10th row instead of 20. Also the sd is wrong too.

I should be missing something here...


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>