Hi, I am trying to replicate a code form a paper and I am facing an issue with the parallel fashion . the code works perfectly with the sequential mode however when I use the parallel fashion the parallel execution is not providing the expected results. the sfLapply
for Forecasts.GARCH
returned a list of NULL
values.
Blockquote
library(zoo)
library(xts)
library(parallel)
library(rugarch)
library(mvtnorm)
library(copula)
DAX.data <- read.csv("GDAXI.csv", header=TRUE)
DAX <- rev(DAX.data$Close)
Datum.DAX <- rev(DAX.data$Date)[-1]
Datum.DAX <- as.Date(Datum.DAX, "%m/%d/%y")
lr.DAX <- diff(log(DAX))
lr.DAX <- xts(x = lr.DAX, order.by = Datum.DAX)
GSPC
GSPC.data <- read.csv("GSPC.csv", header=TRUE)
GSPC <- rev(GSPC.data$Close)
Datum.GSPC <- rev(GSPC.data$Date)[-1]
Datum.GSPC <- as.Date(Datum.GSPC, "%m/%d/%y")
lr.GSPC <- diff(log(GSPC))
lr.GSPC <- xts(x = lr.GSPC, order.by = Datum.GSPC)
merge both DAX and GSPC
V.lr <- merge.xts(lr.DAX, lr.GSPC, all=FALSE)
V.ll <- -V.lr
Datum <- index(V.lr)
N <- nrow(V.lr)
n <- 5190
===========================================================
2. Rolling-window analysis of CoVaR and VaR forecasts
===========================================================
lag <- 1
p <- 0.95
q <- 0.95
library(snow)
library(snowfall) # Parallel computing
library(Rmpi)
library(rlecuyer) # RNG
sfSetMaxCPUs(number = 10)
sfInit(parallel = TRUE, cpus = 10, type = "MPI")
sfSource("CopulaFunctions.R")
sfSource("MLEstimation.R")
sfSource("RMApplication.R")
sfExport("V.ll")
sfLibrary(rugarch)
sfLibrary(evd)
sfLibrary(zoo)
sfLibrary(copula)
sfLibrary(mvtnorm)
wrapper <- function(i, n, lag, p, q, spec, V.ll) {
print("i = ")
print(i)
1. Estimate rolling-window parameters
R <- t(coredata(V.ll[(i - n + 1):(i + lag), ]))
Est <- GARCH.est(R, spec, lag, v = 10)
print("Length of Est:")
print(length(Est))
print("Est:")
print(Est)
if (any(sapply(Est, function(x) any(is.na(x))))) {
print("Estimators did not converge.")
return(list(CoVaR.forecasts.t = NA, CoVaR.forecasts.Gauss = NA))
}
print("Est$GAS.Gauss = ")
print(Est$GAS.Gauss)
print("Est$GAS.t = ")
print(Est$GAS.t)
2. Produce rolling window CoVaR forecasts
R <- t(coredata(V.ll[(i + 1):(i + lag), ]))
print("t Distr:")
CoVaR.forecasts.t <- CoVaR.t.forecast(R, Est$U, Est$eps, Est$Fn.1, Est$Fn.2, Est$outp1, Est$outp2, Est$GAS.t, lag, p, q)
print("Gauss distr:")
CoVaR.forecasts.Gauss <- CoVaR.Gauss.forecast(R, Est$U, Est$eps, Est$Fn.1, Est$Fn.2, Est$outp1, Est$outp2, Est$GAS.Gauss, lag, p, q)
return(list(CoVaR.forecasts.t = CoVaR.forecasts.t, CoVaR.forecasts.Gauss = CoVaR.forecasts.Gauss))
}
start <- Sys.time()
loop <- seq.int(from = n, to = N - lag, by = lag)
GARCH(1,1)
spec_garch <- ugarchspec(variance.model = list(model = "fGARCH", garchOrder = c(1, 1), submodel = "GARCH"),
mean.model = list(armaOrder = c(0, 0), include.mean = TRUE),
distribution.model = "norm")
Forecasts.GARCH <- sfLapply(loop, function(i) {
tryCatch({
wrapper(i, n = n, lag = lag, p = p, q = q, spec = spec_garch, V.ll = V.ll)
}, error = function(e) {
print(paste("Error in iteration", i))
print(e)
return(NULL)
})
})
print(Sys.time() - start)
Stop parallel computing
sfStop()
Any advise how to fix the issue. Thank you