Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rolling Standard Deviation in a Matrix in R

Bellow is a stock daily returns matrix example (ret_matriz)

      IBOV        PETR4        VALE5        ITUB4        BBDC4        PETR3    
[1,] -0.040630825 -0.027795652 -0.052643733 -0.053488685 -0.048455772 -0.061668282
[2,] -0.030463489 -0.031010237 -0.047439725 -0.040229625 -0.030552275 -0.010409016
[3,] -0.022668170 -0.027012078 -0.022668170 -0.050372843 -0.080732363  0.005218051
[4,] -0.057468428 -0.074922051 -0.068414670 -0.044130126 -0.069032911 -0.057468428
[5,]  0.011897277 -0.004705891  0.035489885 -0.005934736 -0.006024115 -0.055017693
[6,]  0.020190656  0.038339130  0.009715552  0.014771317  0.023881732  0.011714308
[7,] -0.007047191  0.004529286  0.004135085  0.017442303 -0.005917177 -0.007047191
[8,] -0.022650593 -0.029481336 -0.019445057 -0.017442303 -0.011940440 -0.046076458
[9,]  0.033137223  0.035274722  0.038519205  0.060452104  0.017857617  0.046076458

For example purposes consider a 5 day moving window, i want as a result a new matrix as described bellow :

     IBOV        PETR4    ...       
[1,] 0           0        ...
[2,] 0           0        ... 
[3,] 0           0        ...
[4,] 0           0        ...
[5,] sd[1:5,1]  sd[1:5,2] ...
[6,] sd[2:6,1]  sd[2:6,2] ...
[7,] sd[3:7,1]  sd[3:7,2] ...
[8,] sd[4:8,1]  sd[4:8,2] ... 
[9,] sd[5:9,1]  sd[5:9,2] ...

Using the zoo package i was able to reach the result but it is a little bit slow, any ideias on how to improve the speed to reach the same result ?

zoo code bellow :

require(zoo)

apply(ret_matriz, 2, function(x) rollapply(x, width = 5, FUN = sd, fill = 0, align = 'r')) 
like image 976
RiskTech Avatar asked Jun 05 '14 17:06

RiskTech


People also ask

How do you calculate rolling standard deviation?

Subtract the moving average from each of the individual data points used in the moving average calculation. This gives you a list of deviations from the average. Square each deviation and add them all together. Divide this sum by the number of periods you selected.

What is sd () in R?

sd() function is used to compute the standard deviation of given values in R. It is the square root of its variance.

How do you find the standard deviation of a distribution in R?

To calculate the standard deviation in r, use the sd() function. The standard deviation of an observation variable in R is calculated by the square root of its variance. The sd in R is a built-in function that accepts the input object and computes the standard deviation of the values provided in the object.

How is R related to standard deviation?

2 Answers. R-squared measures how well the regression line fits the data. This is why higher R-squared values correlate with lower standard deviation.


2 Answers

1) The apply part can be eliminated. We also use rollapplyr for brevity:

rollapplyr(ret_matriz, 5, sd, fill = 0)

2) Also rollmean is faster than rollapply so we could construct it from that using the formula sd = sqrt(n/(n-1) * (mean(x^2) - mean(x)^2)):

sqrt((5/4) * (rollmeanr(ret_matriz^2, 5, fill = 0) - 
              rollmeanr(ret_matriz, 5, fill = 0)^2))
like image 73
G. Grothendieck Avatar answered Oct 06 '22 00:10

G. Grothendieck


You can use TTR::runSD instead.

library(quantmod)
getSymbols("SPY")
spy <- apply(ROC(SPY), 2, runSD, n=5)
# head(spy)
#         SPY.Open    SPY.High     SPY.Low   SPY.Close SPY.Volume SPY.Adjusted
# [1,]          NA          NA          NA          NA         NA           NA
# [2,]          NA          NA          NA          NA         NA           NA
# [3,]          NA          NA          NA          NA         NA           NA
# [4,]          NA          NA          NA          NA         NA           NA
# [5,]          NA          NA          NA          NA         NA           NA
# [6,] 0.004369094 0.003112967 0.001064232 0.005035266  0.1577499  0.005063025
like image 37
Joshua Ulrich Avatar answered Oct 06 '22 00:10

Joshua Ulrich