I have a daily data base of retunrs from a portfolio. For a model I am replicating the authors calculate for each month, the realized variance RVt from daily returns in the previous 21 sessions.
To do this here is a small example of how I am trying to calculate it:
x <- rnorm(24795, 0, 0.2) #Generate random numbers to simulate my sample
x_2 <-x^2 #the model specify to work with the square returns
# I need the monthly sum of the square returns. For this I create a matrix
#with the length if x/20 because each month consist in 20 trading sessions
rv <- matrix(NA, nrow=(length(x_2)/20), ncol=1)
#I create the first step
rv[1] <- sum(x_2[1:20])
#I create a loop to make the sum of from x_2[21:40] and continue
# with this 20 steps sums
for (i in 2:1239){
rv[i] <- sum(x_2[i+20:i+39])
}
rv
The problem is that my loop is summing as:
x_2[21:40]
x_2[22:41]
x_2[23:42]
instead of
x_2[21:40]
x_2[41:60]
x_2[61:80]
Does anyone knows what I a doing wrong?
Here is a picture of the forula from the paper: Formula
Thanks
Miguel