sim_garch | R Documentation |
Simulate or estimate the rolling variance under a GARCH(1,1) process using Rcpp.
sim_garch(omegac, alphac, betac, innov, is_random = TRUE)
omegac |
Parameter proportional to the long-term average level of variance. |
alphac |
The weight associated with recent realized variance updates. |
betac |
The weight associated with the past variance estimates. |
innov |
A single-column matrix of innovations. |
is_random |
Boolean argument: Are the innovations random
numbers or historical returns? (The default is |
The function sim_garch()
simulates or estimates the rolling variance
under a GARCH(1,1) process using Rcpp.
If is_random = TRUE
(the default) then the innovations innov
are treated as random numbers \xi_i
and the GARCH(1,1)
process is given by:
r_i = \sigma_{i-1} \xi_i
\sigma^2_i = \omega + \alpha r^2_i + \beta \sigma_{i-1}^2
Where r_i
and \sigma^2_i
are the simulated returns and
variance, and \omega
, \alpha
, and \beta
are the
GARCH parameters, and \xi_i
are standard normal
innovations.
The long-term equilibrium level of the simulated variance is proportional
to the parameter \omega
:
\sigma^2 = \frac{\omega}{1 - \alpha - \beta}
So the sum of \alpha
plus \beta
should be less than 1
,
otherwise the volatility becomes explosive.
If is_random = FALSE
then the function sim_garch()
estimates the rolling variance from the historical returns. The
innovations innov
are equal to the historical returns r_i
and
the GARCH(1,1) process is simply:
\sigma^2_i = \omega + \alpha r^2_i + \beta \sigma_{i-1}^2
Where \sigma^2_i
is the rolling variance.
The above should be viewed as a formula for estimating the rolling
variance from the historical returns, rather than simulating them. It
represents exponential smoothing of the squared returns with a decay
factor equal to \beta
.
The function sim_garch()
simulates the GARCH process using
fast Rcpp C++
code.
A matrix with two columns and with the same number of rows as
the argument innov
. The first column are the simulated returns and
the second column is the variance.
## Not run:
# Define the GARCH model parameters
alphac <- 0.79
betac <- 0.2
omegac <- 1e-4*(1-alphac-betac)
# Calculate matrix of standard normal innovations
innov <- matrix(rnorm(1e3))
# Simulate the GARCH process using Rcpp
garch_data <- HighFreq::sim_garch(omegac=omegac, alphac=alphac, betac=betac, innov=innov)
# Plot the GARCH rolling volatility and cumulative returns
plot(sqrt(garch_data[, 2]), t="l", main="Simulated GARCH Volatility", ylab="volatility")
plot(cumsum(garch_data[, 1]), t="l", main="Simulated GARCH Cumulative Returns", ylab="cumulative returns")
# Calculate historical VTI returns
retp <- na.omit(rutils::etfenv$returns$VTI)
# Estimate the GARCH volatility of VTI returns
garch_data <- HighFreq::sim_garch(omegac=omegac, alphac=alphac, betac=betac,
innov=retp, is_random=FALSE)
# Plot dygraph of the estimated GARCH volatility
dygraphs::dygraph(xts::xts(sqrt(garch_data[, 2]), index(retp)),
main="Estimated GARCH Volatility of VTI")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.