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.
– mbranbilla