--- title: "Introduction to fEGarch" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Introduction to fEGarch} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) Sys.setlocale("LC_TIME", "English") ``` ```{r setup, message = FALSE, echo = FALSE, warning = FALSE} library(fEGarch) ``` `fEGarch` is a comprehensive library for GARCH (generalized autoregressive conditional heteroskedasticity) model estimation, corresponding value-at-risk (VaR) and expected shortfall (ES) forecasting and backtesting of those quantities, all in one pipeline. As a novelty in the R universe, `fEGarch` gives access to a broader selection of short-memory and long-memory GARCH-type models, including the fractionally integrated GARCH (FIGARCH), the fractionally integrated asymmetric power ARCH (FIAPARCH), and the fractionally integrated exponential GARCH (FIEGARCH). At the same time, `fEGarch` allows for the implementation of a broad exponential GARCH family (EGF), including EGARCH, FIEGARCH, Log-GARCH (logarithmic GARCH) and FILog-GARCH (fractionally integrated Log-GARCH) as special cases. New submodels of the EGF family include the MEGARCH, the MLog-GARCH, the FIMEGARCH and the FIMLog-GARCH models, among others. As a further novelty, the package gives access to the new adjusted FIAPARCH (AFIAPARCH) class, including the AFIAPARCH model, the AFITGARCH (adjusted fractionally integrated threshold GARCH) model, the AFIGJR-GARCH (adjusted fractionally integrated Glosten-Jagannathan-Runkle GARCH) model, and more. Semiparametric extensions of all the pure volatility models and models with simultaneous modelling in the mean (via ARMA or FARIMA) are also implemented. As a further contribution, the backtesting pipeline of `fEGarch` can also be applied to volatility forecasts not produced by the package itself, if they are formatted accordingly. This document provides a brief introduction to the `fEGarch` package and shows some simple applications to start with. For further reading, we refer to the README and the manual of the package. To get started with `fEGarch`, an initial installation of the package from CRAN is required. ```{r, eval = FALSE, echo = TRUE} install.packages("fEGarch") ``` Afterwards, the package can be attached easily in the current R session as follows. ```{r, eval = FALSE, echo = TRUE, warning = FALSE} library(fEGarch) ``` # Model estimation At first, it has to be noted that the `fEGarch` package works with data formatted as numerical vectors, `"ts"` time series objects and `"zoo"` time series objects. The object `SP500` in the package is already formatted as a time series object of class `"zoo"` for easier handling and automated axis formatting in plots generated by the package. The following code is run here to shorten the object and reduce computation times for this brief introduction. In detail, we reduce the observation period from the object `SP500`. ```{r, message = FALSE} library(zoo) rt <- window(SP500, start = "2010-01-01", end = "2019-12-31") ``` ## Fitting an EGF model A model from the EGF can be fitted using the estimation function `fEGarch()`. However, a specification object needs to be created beforehand. The most general way to do this is via `fEGarch_spec()`. Nonetheless, the package gives access to various wrappers, namely `egarch_spec()`, `megarch_spec()`, `mloggarch_spec()`, `loggarch_spec()`, `fiegarch_spec()`, `fimegarch_spec()`, `fimloggarch_spec()` and `filoggarch_spec()`. These can be used to specify a model immediately following the model stated in the function name. For example, to specify an EGARCH model, we may call `egarch_spec()`. These wrappers only have two arguments: `orders` and `cond_dist`. While `orders` can be used to specify the model orders as a numeric vector with two elements, `cond_dist` is used to set the conditional distribution of the model (available are `"norm"` (the default), `"std"`, `"ged"`, `"ald"`, `"snorm"`, `"sstd"`, `"sged"`, `"sald"`). To specify an EGARCH(1, 1) model with conditional generalized error distribution (GED), we run the following code. ```{r} spec <- egarch_spec(orders = c(1, 1), cond_dist = "ged") ``` Since `orders = c(1, 1)` is the default, we may also drop that part from the previous code. Afterwards, the model specification object can be passed to `fEGarch()` alongside the series we want to fit the model to. We also use `n_test = 250` to reserve the last 250 observations for backtesting. ```{r} model1 <- spec %>% fEGarch(rt, n_test = 250) model1 ``` The fitting of the model was successful and we may now access some of the output object's elements. Since the output of `fEGarch()` is an S4-class object, we need to use the operator `@` to access elements. Alternatively, the package also provides accessor functions. ```{r} vola <- sigma(model1) # Fitted conditional standard deviations means <- cmeans(model1) # Fitted (conditional) means res <- residuals(model1) # (Standardized) residuals params <- pars(model1) # Estimated parameters serrors <- se(model1) # Standard errors ``` Some results may also be illustrated directly. ```{r, fig.height = 3.5, fig.width = 7, fig.align='center'} plot(model1, which = 1, xlab = "Year", main = "S&P500 log-returns", ylab = "Log-return") ``` ```{r, fig.height = 3.5, fig.width = 7, fig.align='center'} plot(model1, which = 3, xlab = "Year", main = "Fitted conditional standard deviations", ylab = "Volatility") ``` ```{r, fig.height = 3.5, fig.width = 7, fig.align='center'} plot(model1, which = 4, xlab = "Year", main = "Residual value", ylab = "Standardized residuals") ``` Alternatively, the package also offers an `autoplot()` method to create plots within the `ggplot2` framework. ## Fitting other GARCH models For other GARCH-type models than those of the EGF, a slightly different estimation pipeline is available. Either a combination of `fAParch_spec()` and `fAParch()`, where `fAParch_spec()` may be replaced by a more direct specification function like `garch_spec()` or `afiaparch_spec()`, can be applied. Alternatively, designated wrappers for different models can be used directly, for example `garch()`, `aparch()`, `figarch()`, `fiaparch()`, and `afitgarch()`. When calling these functions, the argument `cond_dist` may be adjusted to set the conditional distribution. A FIAPARCH with conditional $t$-distribution can be fitted as follows. ```{r} model2 <- fiaparch(rt, cond_dist = "std", n_test = 250) model2 ``` The same fitting may be done as follows. ```{r} model2 <- fiaparch_spec(cond_dist = "std") %>% fAParch(rt, n_test = 250) model2 ``` Or with the most general specification function as follows. ```{r} model2 <- fAParch_spec( cond_dist = "std", model_type = "aparch", long_memo = TRUE, adjust = FALSE, orders = c(1, 1) ) %>% fAParch(rt, n_test = 250) model2 ``` An AFIAPARCH, as a model related to but different from the FIAPARCH, with conditional $t$-distribution could be fitted using one of the following codes. ```{r} model3 <- fAParch_spec( cond_dist = "std", model_type = "aparch", long_memo = TRUE, adjust = TRUE, orders = c(1, 1) ) %>% fAParch(rt, n_test = 250) model3 <- afiaparch_spec(cond_dist = "std") %>% fAParch(rt, n_test = 250) model3 <- afiaparch(rt, cond_dist = "std", n_test = 250) model3 ``` The same methods as before can be used to retrieve elements of the fitting object and to produce simple plots. # Rolling VaR and ES forecasts over test period Model objects, where `n_test` has been set to a positive integer in the estimation process, can be passed to `predict_roll()` to produce rolling point forecasts (of conditional means and volatility) over the reserved observations. By default, no model refitting is done (`refit_after = NULL`), but this may be adjusted via setting the argument `refit_after` to a positive integer that indicates after how many steps the model should be re-estimated again. `steady_window` is a logical that allows the user to set the training window for the re-estimations: for `TRUE`, the training window is moved in its entirety by `refit_after` observations and keeps the same number of training observations throughout. For `FALSE`, the training window is just expanded to the right-hand side and kept the same throughout at the left-hand side. ```{r} vol_fcasts <- model1 %>% predict_roll() ``` The output of `predict_roll()` can then be passed to `measure_risk()` to transform the rolling forecasts of mean and volatility to rolling VaR and ES forecasts in accordance with the underlying conditional distribution. By default, the 97.5% and 99% confidence levels are considered but can be adjusted via the argument `levels`. ```{r} risk_fcasts <- vol_fcasts %>% measure_risk() ``` Simple plots may be created from such risk objects by selecting the confidence level among the ones available in the object. ```{r, fig.height = 3.5, fig.width = 7, fig.align='center'} plot(risk_fcasts, which = 0.975, main = "97.5%", xlab = "Month") ``` Elements of the outputs from `predict_roll()` and `measure_risk()` may be accessed via `@`. # VaR and ES backtesting The output of `measure_risk()` can be easily tested using `backtest_suite()`. Sub-functions called by `backtest_suite()` are `trafflight_test()`, `WAD()` and `cov_tests()`. ```{r} risk_fcasts %>% backtest_suite() ``` The output printed to the console gives various backtesting results for VaR and ES, including trafficlight tests, independence and coverage tests, as well as the weighted absolute deviation criterion (WAD). # Bringing it all together An entire estimation, forecasting and backtesting pipeline for the EGARCH(1, 1) with conditional GED thus can be implemented entirely as follows. ```{r, eval = FALSE} egarch_spec(cond_dist = "ged") %>% fEGarch(rt, n_test = 250) %>% predict_roll() %>% measure_risk() %>% backtest_suite() ``` Results are omitted to avoid repetition. Aside from the initial estimation, all models in `fEGarch` can be backtested using the exact pipeline `%>% predict_roll() %>% measure_risk() %>% backtest_suite()` with optional arguments to adjust along the way. # Further capabilities Residual tests of fitted models can be implemented via `fit_test_suite()`. It includes a Ljung-Box test for the simple and the squared residuals, a sign-bias test and an adjusted Pearson goodness-of-fit test. Furthermore, if conditional means and conditional standard deviations have been obtained externally using some other method, one can implement a simple VaR and ES backtesting procedure (without model refitting) using the pipeline `find_dist() %>% measure_risk() %>% backtest_suite()`. `find_dist()` fits the best distribution to a residual series, `measure_risk()` then again (by also supplying it with the mean and volatility forecasts) produces VaR and ES forecasts, and `backtest_suite()` runs the backtests.