sim_garch: Simulate or estimate the rolling variance under a...

View source: R/RcppExports.R

sim_garchR Documentation

Simulate or estimate the rolling variance under a GARCH(1,1) process using Rcpp.

Description

Simulate or estimate the rolling variance under a GARCH(1,1) process using Rcpp.

Usage

sim_garch(omega, alpha, beta, innov, is_random = TRUE)

Arguments

omega

Parameter proportional to the long-term average level of variance.

alpha

The weight associated with recent realized variance updates.

beta

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 is_random = TRUE.)

Details

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.

Value

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.

Examples

## Not run: 
# Define the GARCH model parameters
alpha <- 0.79
betav <- 0.2
om_ega <- 1e-4*(1-alpha-betav)
# Calculate matrix of standard normal innovations
innov <- matrix(rnorm(1e3))
# Simulate the GARCH process using Rcpp
garch_data <- HighFreq::sim_garch(omega=om_ega, alpha=alpha,  beta=betav, 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(omega=om_ega, alpha=alpha,  beta=betav, 
  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)


algoquant/HighFreq documentation built on Feb. 9, 2024, 8:15 p.m.