My goal is to map daily return of 5 stocks (a xts object) to a rolling standard deviation of a look back period of 90 days (calculating the SD of the return of the last 90 days) with the same data structure and with fast speed. The approach by using core function "lapply" works great. However, the parallel approach "sfLapply" in snowfall package did not work for some reasons. Here is illustration:
Initializing libraries and simulating a data-set & parameters:
require(PerformanceAnalytics)
require(quantmod)
require(snowfall)
adjReturns <- replicate(5, rnorm(10000, mean = 0.01, sd = 0.008))
colnames(adjReturns) <- c('stock1','stock2','stock3','stock4','stock5')
timeIndex <- seq.Date(as.Date("2015-01-01", "%Y-%m-%d"), by ="day", length.out = 10000)
adjReturns <- as.xts(adjReturns, order.by = timeIndex)
Calculating Rolling SD using lapply resulting a solution that works:
rollingSD <- list()
rollingSD <- lapply(adjReturns, function(x) apply.rolling(x, width = 90, FUN = "sd"))
rollingSD <- do.call(cbind, rollingSD)
Here is the parallel version that did not work:
sfInit(parallel = TRUE, cpus = 4, type = "SOCK", socketHosts = rep("localhost", 2))
sfLibrary(snowfall)
sfLibrary(PerformanceAnalytics)
sfLibrary(xts)
sfLibrary(quantmod)
sfExportAll()
rollingSDSnow <- list()
rollingSDSnow <- sfLapply(adjReturns, function(x) apply.rolling(x, width = 90, FUN = "sd"))
rollingSDSnow <- do.call(cbind, rollingSDSnow)
sfStop()
The code above return the following error:
Error in `[.xts`(x, i) : subscript out of bounds
I am not sure why I would get this error as I am not even writing my own for loops. Please point out any possible mistakes, Any thought would be appreciated and thanks for helping!
Environment: R:3.2.0/ RStudio:0.99.472 / snow:0.3-13 / snowfall:1.84-6/ xts:0.9-7/ PerfomanceAnalytics:1.4.3541
P.S. runSD could have been used instead of apply.rolling, apply.rolling is used as it can work with different functions.