sim_ar: Simulate _autoregressive_ returns by recursively filtering a...

View source: R/RcppExports.R

sim_arR Documentation

Simulate autoregressive returns by recursively filtering a matrix of innovations through a matrix of autoregressive coefficients.

Description

Simulate autoregressive returns by recursively filtering a matrix of innovations through a matrix of autoregressive coefficients.

Usage

sim_ar(coeff, innov)

Arguments

innov

A single-column matrix of innovations.

coeff

A single-column matrix of autoregressive coefficients.

Details

The function sim_ar() recursively filters the matrix of innovations innov through the matrix of autoregressive coefficients coeff, using fast RcppArmadillo C++ code.

The function sim_ar() simulates an autoregressive process AR(n) of order n:

r_i = \varphi_1 r_{i-1} + \varphi_2 r_{i-2} + \ldots + \varphi_n r_{i-n} + \xi_i

Where r_i is the simulated output time series, \varphi_i are the autoregressive coefficients, and \xi_i are the standard normal innovations.

The order n of the autoregressive process AR(n), is equal to the number of rows of the autoregressive coefficients coeff.

The function sim_ar() performs the same calculation as the standard R function
filter(x=innov, filter=coeff, method="recursive"), but it's several times faster.

Value

A single-column matrix of simulated returns, with the same number of rows as the argument innov.

Examples

## Not run: 
# Define AR coefficients
coeff <- matrix(c(0.1, 0.3, 0.5))
# Calculate matrix of innovations
innov <- matrix(rnorm(1e4, sd=0.01))
# Calculate recursive filter using filter()
innof <- filter(innov, filter=coeff, method="recursive")
# Calculate recursive filter using RcppArmadillo
retp <- HighFreq::sim_ar(coeff, innov)
# Compare the two methods
all.equal(as.numeric(retp), as.numeric(innof))
# Compare the speed of RcppArmadillo with R code
library(microbenchmark)
summary(microbenchmark(
  Rcpp=HighFreq::sim_ar(coeff, innov),
  Rcode=filter(innov, filter=coeff, method="recursive"),
  times=10))[, c(1, 4, 5)]  # end microbenchmark summary

## End(Not run)


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