EWMA volatility in a date.frame

Asked

Viewed 156 times

5

I have a date.frame base_retorno_diario, of 3560 observations on 110 shares (daily returns), I want to create another data.frame from that with Volatility ewma with decay_factor = 0.97

Example of data.frame

  Data       IBOV         ABEV3       AEDU3   ALLL3 BBAS3        BBDC3        BBDC4
1 2000-01-04 -0.063756245 0.00000000  0       0     -0.029935852 -0.080866107 -0.071453347
2 2000-01-05 0.024865308  -0.03762663 0       0     -0.008082292 0.043269231 0.060889055
3 2000-01-06 -0.008510238 -0.03157895 0       0      0.014074074 0.014285714 0.008098592
4 2000-01-07 0.012557359  -0.02484472 0       0     -0.022644266 0.017719219 0.000000000
5 2000-01-10 0.043716564  0.00000000  0       0     0.050074738 0.005357143 0.006985679
6 2000-01-11 -0.026401514 -0.02388535 0       0    -0.008540925 -0.059058615 -0.046479362

The first line I created with the following code (n_row and n_col are the number of rows and columns in base_retorno_diario)

EWMA_VARIANCE = as.data.frame(base_retorno_diario[1,2:n_col]^2)

then I created the following loop

i = 2
DECAY_FACTOR = 0.97
while(i<=n_row){EWMA_VARIANCE = rbind(EWMA_VARIANCE,EWMA_VARIANCE[(i-1),1:(n_col-1)]*DECAY_FACTOR +(1-DECAY_FACTOR)*base_retorno_diario[i,2:n_col]^2)
i=i+1
}

It works and creates the new data.frame with the volatility of all stocks in the database, however it takes a long time, there is some more efficient way to encode the same situation ?

  • Friend, I believe you can GREATLY improve the execution speed if you use matrix instead of dataframe or datatable. I had a similar problem here: https://answall.com/questions/215414/somar-linha-a-linha-em-um-data-table-em-r There is a good difference in how data is allocated in a dataframe and matrix. When traversed by a for or while, the way data is allocated impacts a lot on runtime.

1 answer

2

Follow the code with the answer

The second function variance.ewma receives a matrix with stock returns and the value of the EWMA decay factor, for each column of the matrix it calls the function ewma.func and passes the decay factor by applying the formula ewma to each column, when finished applying the formula in each column it returns an array with the results.

ewma.func <- function(rets, lambda) {
rets[is.infinite(rets)] = 0
rets[is.nan(rets)] = 0
ewma.ant <- rets[1]^2
ewma <- vapply(rets, function(r) ewma.ant <<- ewma.ant *lambda + (r^2)*(1 - lambda), 0)
return(ewma)
}

variance.ewma <- function(bancodados,decayfactor){
EWMA = bancodados
for (i in 1:ncol(EWMA)){
EWMA[, i] = ewma.func(EWMA[, i], decayfactor) }
return(EWMA)}
  • do not forget to put the source for the Mrflick answer on Soen. His method is quite interesting.

  • It is interesting to check the following link for an alternative way to get the same result. (http://stackoverflow.com/questions/23791321/ewma-volatility-in-r-using-data-frames)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.