DataCamp GARCH Models in R
GARCH MODELS IN R
There are old traders and
there are bold traders, but...
Kris Boudt
Professor of finance and econometrics
DataCamp GARCH Models in R
About the instructor
Kris Boudt
PhD in financial risk forecasting
Use GARCH models to win by not
losing (much)
R package rugarch of Alexios
Ghalanos.
DataCamp GARCH Models in R
Calculating returns
Relative financial gains and losses, expressed in terms of returns
Function CalculateReturns in PerformanceAnalytics
# Example in R for daily S&P 500 prices (xts object)
library(PerformanceAnalytics)
SP500returns <- CalculateReturns(SP500prices)
DataCamp GARCH Models in R
Daily S&P 500 returns
Properties of daily returns:
The average return is zero
Return variability changes through
time
Standard deviation = measure of return
variability.
Synonym: Return volatility.
Greek letter σt .
DataCamp GARCH Models in R
DataCamp GARCH Models in R
How to estimate return volatility
Function sd() computes the standard deviation:
# Compute daily standard deviation
> sd(sp500ret)
[1] 0.01099357
Corresponding formula for T daily returns:
1
^=
T
∑(Rt − μ
⎷T − 1
σ ^ )2 ,
t=1
where μ
^ is the mean return.
DataCamp GARCH Models in R
Annualized volatility
sd(sp500ret) is daily volatility
Annualized volatility = √252 × daily volatility
# Compute annualized standard deviation
> sqrt(252)*sd(sp500ret)
[1] 0.1745175
DataCamp GARCH Models in R
DataCamp GARCH Models in R
Rolling volatility estimation
Rolling estimation windows :
Window width? Multiple of 22 (trading days).
DataCamp GARCH Models in R
Function chart.RollingPerformance()
library(PerformanceAnalytics)
chart.RollingPerformance(R = sp500ret ,
width = 22,
FUN = "sd.annualized",
scale = 252,
main = "Rolling 1 month volatility")
DataCamp GARCH Models in R
DataCamp GARCH Models in R
About GARCH models in R
Estimation of σt requires time series models, like GARCH.
DataCamp GARCH Models in R
GARCH MODELS IN R
Let's refresh the basics of
computing rolling standard
deviations in R
DataCamp GARCH Models in R
GARCH MODELS IN R
GARCH models: The way
forward
Kris Boudt
Professor of finance and econometrics
DataCamp GARCH Models in R
Inventors of GARCH models
Robert Engle Tim Bollerslev
DataCamp GARCH Models in R
Notation (i)
Input: Time series of returns
DataCamp GARCH Models in R
Notation (ii)
At time t-1, you make the prediction about the the future return Rt , using the
information set available at time t − 1:
DataCamp GARCH Models in R
Notation (iii)
Predicting the mean return: what is the best possible prediction of the actual
return?
DataCamp GARCH Models in R
Notation (iv)
We then predict the variance: how far off the return can be from its mean?
DataCamp GARCH Models in R
From theory to practice: Models for the mean
We need an equation that maps the past returns into a prediction of the mean
For AR(MA) models for the mean, see Datacamp course on time series analysis.
DataCamp GARCH Models in R
From theory to practice: Models for the variance
We need an equation that maps the past returns into predictions of the variance
DataCamp GARCH Models in R
ARCH(p) model: Autoregressive Conditional Heteroscedasticity
We need an equation that maps the past returns into predictions of the variance
DataCamp GARCH Models in R
GARCH(1,1) model: Generalized ARCH
We need an equation that maps the past returns into predictions of the variance
DataCamp GARCH Models in R
Parameter restrictions
To make the GARCH process realistic, we need that:
1. ω, α and β are > 0: this ensures that σt2 > 0 at all times.
2. α + β < 1: this ensures that the predicted variance σt2 always returns to the
long run variance:
The variance is therefore "mean-reverting"
The long run variance equals ω
1−α−β
DataCamp GARCH Models in R
R implementation - Specify the inputs
Let's familiarize ourselves with the GARCH equations using R code:
# Set parameter values
alpha <- 0.1
beta <- 0.8
omega <- var(sp500ret)*(1-alpha-beta)
# Then: var(sp500ret) = omega/(1-alpha-beta)
# Set series of prediction error
e <- sp500ret - mean(sp500ret) # Constant mean
e2 <- e^2
DataCamp GARCH Models in R
R implementation - compute predicted variances
# We predict for each observation its variance.
nobs <- length(sp500ret)
predvar <- rep(NA, nobs)
# Initialize the process at the sample variance
predvar[1] <- var(sp500ret)
# Loop starting at 2 because of the lagged predictor
for (t in 2:nobs){
# GARCH(1,1) equation
predvar[t] <- omega + alpha * e2[t - 1] + beta * predvar[t-1]
}
DataCamp GARCH Models in R
R implementation - Plot of GARCH volatilities
# Volatility is sqrt of predicted variance
predvol <- sqrt(predvar)
predvol <- xts(predvol, order.by = time(sp500ret))
# We compare with the unconditional volatility
uncvol <- sqrt(omega / (1 - alpha-beta))
uncvol <- xts(rep(uncvol, nobs), order.by = time(sp500ret))
# Plot
plot(predvol)
lines(uncvol, col = "red", lwd = 2)
DataCamp GARCH Models in R
DataCamp GARCH Models in R
GARCH MODELS IN R
Let's practice!
DataCamp GARCH Models in R
GARCH MODELS IN R
Alpha - Beta - Sigma:
The rugarch package
Kris Boudt
Professor of finance and econometrics
DataCamp GARCH Models in R
The normal GARCH(1,1) model with constant mean
The normal GARCH model
Four parameters: μ, ω, α, β.
Estimation by maximum likelihood: find the parameter values for which the
GARCH model is most likely to have generated the observed return series.
DataCamp GARCH Models in R
Alexios Ghalanos
library(rugarch)
citation("rugarch")
When using rugarch in publications, please cite:
To cite the rugarch package, please use:
Alexios Ghalanos (2018). rugarch: Univariate GARCH models. R package version 1.4
DataCamp GARCH Models in R
Workflow
Three steps:
ugarchspec(): Specify which GARCH model you want to use (mean μ t ,
variance σt2 , distribution of et )
ugarchfit(): Estimate the GARCH model on your time series with returns
R1 , ..., RT .
ugarchforecast(): Use the estimated GARCH model to make volatility
predictions for RT+1 ,...
DataCamp GARCH Models in R
Workflow in R
ugarchspec(): Specify which GARCH model you want to use.
# Constant mean, standard garch(1,1) model
garchspec <- ugarchspec(
mean.model = list(armaOrder = c(0,0)),
variance.model = list(model = "sGARCH"),
distribution.model = "norm")
ugarchfit(): Estimate the GARCH model
garchfit <- ugarchfit(data = sp500ret ,
spec = garchspec)
ugarchforecast(): Forecast the volatility of the future returns
garchforecast <- ugarchforecast(fitORspec = garchfit,
n.ahead = 5)
DataCamp GARCH Models in R
ugarchfit object
The ugarchfit yields an object that contains all the results related to the
estimation of the garch model.
Methods coef, uncvar, fitted and sigma:
# Coefficients
garchcoef <- coef(garchfit)
# Unconditional variance
garchuncvar <- uncvariance(garchfit)
# Predicted mean
garchmean <- fitted(garchfit)
# Predicted volatilities
garchvol <- sigma(garchfit)
DataCamp GARCH Models in R
Estimated GARCH coefficients for daily S&P 500 returns
print(garchcoef)
mu omega alpha1 beta1
5.728020e-04 1.220515e-06 7.792031e-02 9.111455e-01
Estimated model:
sqrt(garchuncvar)
0.01056519
DataCamp GARCH Models in R
Estimated volatilities
garchvol <- sigma(garchfit)
plot(garchvol)
DataCamp GARCH Models in R
What about future volatility?
tail(garchvol, 1)
2017-12-29 0.004862908
What about the volatility for the days following the end of the time series?
DataCamp GARCH Models in R
Forecasting h-day ahead volatilities
Applying the sigma() method to the ugarchforecast object gives the volatility
forecasts:
sigma(garchforecast)
2017-12-29
T+1 0.005034754
T+2 0.005127582
T+3 0.005217770
T+4 0.005305465
T+5 0.005390797
DataCamp GARCH Models in R
Forecasting h-day ahead volatilities
Applying the fitted() method to the ugarchforecast object gives the mean
forecasts:
fitted(garchforecast)
2017-12-29
T+1 0.000572802
T+2 0.000572802
T+3 0.000572802
T+4 0.000572802
T+5 0.000572802
DataCamp GARCH Models in R
Application to tactical asset allocation
A portfolio that invests a percentage w in a risky asset (with volatility σt ) and
keeps 1 − w on a risk-free bank deposit account has volatility equal to
σp = wσt .
How to set w? One approach is volatility targeting: w is such that the predicted
annualized portfolio volatility equals a target level, say 5%. Then:
w∗ = 0.05/σt
Since GARCH volatilities change, the optimal weight changes as well.
DataCamp GARCH Models in R
GARCH MODELS IN R
Let's play with rugarch!