# In-sample
window.zoo <- get("window.zoo", envir = asNamespace("zoo"))
rt <- window.zoo(SP500, end = "2002-12-31")
model <- fEGarch(egarch_spec(), rt)
risk <- measure_risk(model, measure = c("VaR", "ES"), level = c(0.95, 0.975, 0.99))
risk
# Out-of-sample rolling point forecasts
window.zoo <- get("window.zoo", envir = asNamespace("zoo"))
rt <- window.zoo(SP500, end = "2002-12-31")
model2 <- fEGarch(egarch_spec(), rt, n_test = 250)
# parallel = FALSE only for better compatibility with CRAN checks;
# should be set to TRUE for speed boost
fcast <- predict_roll(model2, parallel = FALSE)
risk2 <- measure_risk(fcast, measure = c("VaR", "ES"), level = c(0.95, 0.975, 0.99))
risk2
# Use some model to obtain rolling point forecasts of
# the conditional mean and the conditional standard deviation for
# some test period; in practice, this will not be from a GARCH-type
# model, because it is parametric and includes a distribution assumption,
# but instead from some nonparametric model
window.zoo <- get("window.zoo", envir = asNamespace("zoo"))
rt <- window.zoo(SP500, end = "2005-12-31")
model <- fEGarch(egarch_spec(), rt, n_test = 250)
# parallel = FALSE only for better compatibility with CRAN checks;
# should be set to TRUE for speed boost
fc <- model %>% predict_roll(refit_after = 50, parallel = FALSE)
test_obs <- model@test_obs # Test observations
sigt <- fc@sigt # Conditional volatility forecasts
cmeans <- fc@cmeans # Conditional mean forecasts
resids <- model@etat # In-sample standardized residuals
# Given 'test_obs', 'sigt', 'cmeans' and 'resids', we can now
# compute the VaR and ES forecasts for the test period
dist <- find_dist(resids, fix_mean = 0, fix_sdev = 1)
dist
risk <- dist %>%
measure_risk(test_obs = test_obs, sigt = sigt, cmeans = cmeans)
plot(risk, which = 0.975)
Run the code above in your browser using DataLab