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.
Afterwards, the package can be attached easily in the current R session as follows.
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.
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.
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.
model1 <- spec %>%
fEGarch(rt, n_test = 250)
model1
#> *************************************
#> * Fitted EGARCH Family Model *
#> *************************************
#>
#> Type: egarch
#> Orders: (1, 1)
#> Modulus: (FALSE, FALSE)
#> Powers: (1, 1)
#> Long memory: FALSE
#> Cond. distribution: ged
#>
#> ARMA orders (cond. mean): (0, 0)
#> Long memory (cond. mean): FALSE
#>
#> Scale estimation: FALSE
#>
#> Fitted parameters:
#>
#> par se tval pval
#> mu 0.0004 0.0001 3.2881 0.0010
#> omega_sig -9.8020 0.1208 -81.1614 0.0000
#> phi1 0.9498 0.0079 119.9600 0.0000
#> kappa -0.2263 0.0224 -10.1147 0.0000
#> gamma 0.1762 0.0241 7.3165 0.0000
#> shape 1.2639 0.0512 24.6875 0.0000
#>
#> Information criteria (parametric part):
#> LLH: 3.4579, AIC: -6.9105, BIC: -6.8954The 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.
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 errorsSome results may also be illustrated directly.
plot(model1, which = 3, xlab = "Year", main = "Fitted conditional standard deviations",
ylab = "Volatility")Alternatively, the package also offers an autoplot()
method to create plots within the ggplot2 framework.
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.
model2 <- fiaparch(rt, cond_dist = "std", n_test = 250)
model2
#> *************************************
#> * Fitted FIAPARCH Model *
#> *************************************
#>
#> Type: fiaparch
#> Orders: (1, 1)
#> Long memory: TRUE
#> Cond. distribution: std
#>
#> ARMA orders (cond. mean): (0, 0)
#> Long memory (cond. mean): FALSE
#>
#> Scale estimation: FALSE
#>
#> Fitted parameters:
#>
#> par se tval pval
#> mu 0.0005 0.0001 3.6302 0.0003
#> omega 0.0014 0.0010 1.4103 0.1585
#> phi1 0.1570 0.0392 4.0017 0.0001
#> beta1 0.6370 0.0917 6.9508 0.0000
#> gamma 0.9999 0.0131 76.5373 0.0000
#> delta 1.0785 0.1029 10.4832 0.0000
#> d 0.6072 0.1122 5.4141 0.0000
#> df 5.6220 0.6647 8.4580 0.0000
#>
#> Scaling constant at optimal solution:
#> Cz: 0.7914
#>
#> Numerical stability at optimal solution:
#> Cem: 0.7841
#>
#> Information criteria (parametric part):
#> LLH: 3.4596, AIC: -6.9122, BIC: -6.8920The same fitting may be done as follows.
model2 <- fiaparch_spec(cond_dist = "std") %>% fAParch(rt, n_test = 250)
model2
#> *************************************
#> * Fitted FIAPARCH Model *
#> *************************************
#>
#> Type: fiaparch
#> Orders: (1, 1)
#> Long memory: TRUE
#> Cond. distribution: std
#>
#> ARMA orders (cond. mean): (0, 0)
#> Long memory (cond. mean): FALSE
#>
#> Scale estimation: FALSE
#>
#> Fitted parameters:
#>
#> par se tval pval
#> mu 0.0005 0.0001 3.6302 0.0003
#> omega 0.0014 0.0010 1.4103 0.1585
#> phi1 0.1570 0.0392 4.0017 0.0001
#> beta1 0.6370 0.0917 6.9508 0.0000
#> gamma 0.9999 0.0131 76.5373 0.0000
#> delta 1.0785 0.1029 10.4832 0.0000
#> d 0.6072 0.1122 5.4141 0.0000
#> df 5.6220 0.6647 8.4580 0.0000
#>
#> Scaling constant at optimal solution:
#> Cz: 0.7914
#>
#> Numerical stability at optimal solution:
#> Cem: 0.7841
#>
#> Information criteria (parametric part):
#> LLH: 3.4596, AIC: -6.9122, BIC: -6.8920Or with the most general specification function as follows.
model2 <- fAParch_spec(
cond_dist = "std",
model_type = "aparch",
long_memo = TRUE,
adjust = FALSE,
orders = c(1, 1)
) %>% fAParch(rt, n_test = 250)
model2
#> *************************************
#> * Fitted FIAPARCH Model *
#> *************************************
#>
#> Type: fiaparch
#> Orders: (1, 1)
#> Long memory: TRUE
#> Cond. distribution: std
#>
#> ARMA orders (cond. mean): (0, 0)
#> Long memory (cond. mean): FALSE
#>
#> Scale estimation: FALSE
#>
#> Fitted parameters:
#>
#> par se tval pval
#> mu 0.0005 0.0001 3.6302 0.0003
#> omega 0.0014 0.0010 1.4103 0.1585
#> phi1 0.1570 0.0392 4.0017 0.0001
#> beta1 0.6370 0.0917 6.9508 0.0000
#> gamma 0.9999 0.0131 76.5373 0.0000
#> delta 1.0785 0.1029 10.4832 0.0000
#> d 0.6072 0.1122 5.4141 0.0000
#> df 5.6220 0.6647 8.4580 0.0000
#>
#> Scaling constant at optimal solution:
#> Cz: 0.7914
#>
#> Numerical stability at optimal solution:
#> Cem: 0.7841
#>
#> Information criteria (parametric part):
#> LLH: 3.4596, AIC: -6.9122, BIC: -6.8920An AFIAPARCH, as a model related to but different from the FIAPARCH, with conditional \(t\)-distribution could be fitted using one of the following codes.
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
#> *************************************
#> * Fitted AFIAPARCH Model *
#> *************************************
#>
#> Type: afiaparch
#> Orders: (1, 1)
#> Long memory: TRUE
#> Cond. distribution: std
#>
#> ARMA orders (cond. mean): (0, 0)
#> Long memory (cond. mean): FALSE
#>
#> Scale estimation: FALSE
#>
#> Fitted parameters:
#>
#> par se tval pval
#> mu 0.0005 0.0001 3.4266 0.0006
#> omega 0.0003 0.0002 1.2480 0.2120
#> phi1 0.1708 0.0583 2.9288 0.0034
#> beta1 0.4661 0.0785 5.9348 0.0000
#> gamma 0.9999 0.0147 68.1603 0.0000
#> delta 1.2146 0.1389 8.7433 0.0000
#> d 0.4053 0.0511 7.9276 0.0000
#> df 5.3353 0.6227 8.5676 0.0000
#>
#> Scaling constant at optimal solution:
#> Cz: 0.8802
#>
#> Numerical stability at optimal solution:
#> Cem: 0.9552
#>
#> Information criteria (parametric part):
#> LLH: 3.4585, AIC: -6.9099, BIC: -6.8897The same methods as before can be used to retrieve elements of the fitting object and to produce simple plots.
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.
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.
Simple plots may be created from such risk objects by selecting the confidence level among the ones available in the object.
Elements of the outputs from predict_roll() and
measure_risk() may be accessed via @.
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().
risk_fcasts %>% backtest_suite()
#>
#> ***********************
#> * Traffic light tests *
#> ***********************
#>
#> VaR results:
#> ************
#>
#> Conf. level: 0.975
#> Breaches: 7
#> Cumul. prob.: 0.7103
#> Zone: Green zone
#>
#> Conf. level: 0.99
#> Breaches: 4
#> Cumul. prob.: 0.8922
#> Zone: Green zone
#>
#>
#> ES results:
#> ***********
#>
#> Conf. level: 0.975
#> Severity of breaches: 4.1293
#> Cumul. prob.: 0.7588
#> Zone: Green zone
#>
#> Conf. level: 0.99
#> Severity of breaches: 1.6958
#> Cumul. prob.: 0.6880
#> Zone: Green zone
#>
#>
#>
#> *******************************
#> * Weighted Absolute Deviation *
#> *******************************
#>
#> Following 99%-VaR, 97.5%-VaR and 97.5%-ES.
#>
#> WAD: 1.0414
#>
#>
#> ***************************************
#> * Unconditional coverage test for VaR *
#> ***************************************
#>
#> H0: true share of covered observations = theoretical share of VaR
#>
#> Conf. level: 0.975
#> Breaches: 7
#> Test statistic: 0.0889
#> p-value: 0.7656
#> Decision: Do not reject H0
#>
#> Conf. level: 0.99
#> Breaches: 4
#> Test statistic: 0.7691
#> p-value: 0.3805
#> Decision: Do not reject H0
#>
#>
#> *****************************
#> * Independence test for VaR *
#> *****************************
#>
#> H0: true share of covered observations independent
#> of breach or no breach at previous time point
#>
#> Conf. level: 0.975
#> Breaches: 7
#> Test statistic: 0.4050
#> p-value: 0.5245
#> Decision: Do not reject H0
#>
#> Conf. level: 0.99
#> Breaches: 4
#> Test statistic: 0.1306
#> p-value: 0.7178
#> Decision: Do not reject H0
#>
#>
#> *************************************
#> * Conditional coverage test for VaR *
#> *************************************
#>
#> H0: true share of covered observations simultaneously
#> independent of breach or no breach at previous time point
#> and equal to theoretical share of VaR
#>
#> Conf. level: 0.975
#> Breaches: 7
#> Test statistic: 0.5002
#> p-value: 0.7787
#> Decision: Do not reject H0
#>
#> Conf. level: 0.99
#> Breaches: 4
#> Test statistic: 0.9120
#> p-value: 0.6338
#> Decision: Do not reject H0The 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).
An entire estimation, forecasting and backtesting pipeline for the EGARCH(1, 1) with conditional GED thus can be implemented entirely as follows.
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.
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.