roll_var: Calculate a _matrix_ of dispersion (variance) estimates over...

View source: R/RcppExports.R

roll_varR Documentation

Calculate a matrix of dispersion (variance) estimates over a rolling look-back interval attached at the end points of a time series or a matrix.

Description

Calculate a matrix of dispersion (variance) estimates over a rolling look-back interval attached at the end points of a time series or a matrix.

Usage

roll_var(
  tseries,
  lookb = 1L,
  startp = 0L,
  endd = 0L,
  step = 1L,
  stub = 0L,
  method = "moment",
  confl = 0.75
)

Arguments

tseries

A time series or a matrix of data.

lookb

The number of end points in the look-back interval (the default is lookb = 1).

startp

An integer vector of start points (the default is startp = 0).

endd

An integer vector of end points (the default is endd = 0).

step

The number of time periods between the end points (the default is step = 1).

stub

An integer value equal to the first end point for calculating the end points (the default is stub = 0).

method

A character string representing the type of the measure of dispersion (the default is method = "moment").

Details

The function roll_var() calculates a matrix of dispersion (variance) estimates over rolling look-back intervals attached at the end points of the time series tseries.

The function roll_var() performs a loop over the end points, and at each end point it subsets the time series tseries over a look-back interval equal to lookb number of end points.

It passes the subset time series to the function calc_var(), which calculates the dispersion. See the function calc_var() for a description of the dispersion methods.

If the arguments endd and startp are not given then it first calculates a vector of end points separated by step time periods. It calculates the end points along the rows of tseries using the function calc_endpoints(), with the number of time periods between the end points equal to step time periods.

For example, the rolling variance at 25 day end points, with a 75 day look-back, can be calculated using the parameters step = 25 and lookb = 3.

The function roll_var() with the parameter step = 1 performs the same calculation as the function roll_var() from package RcppRoll, but it's several times faster because it uses RcppArmadillo C++ code.

The function roll_var() is implemented in RcppArmadillo RcppArmadillo C++ code, which makes it several times faster than R code.

Value

A matrix dispersion (variance) estimates with the same number of columns as the input time series tseries, and the number of rows equal to the number of end points.

Examples

## Not run: 
# Define time series of returns using package rutils
retp <- na.omit(rutils::etfenv$returns$VTI)
# Calculate the rolling variance at 25 day end points, with a 75 day look-back
varv <- HighFreq::roll_var(retp, lookb=3, step=25)
# Compare the variance estimates over 11-period look-back intervals
all.equal(HighFreq::roll_var(retp, lookb=11)[-(1:10), ], 
  drop(RcppRoll::roll_var(retp, n=11)), check.attributes=FALSE)
# Compare the speed of HighFreq::roll_var() with RcppRoll::roll_var()
library(microbenchmark)
summary(microbenchmark(
  Rcpp=HighFreq::roll_var(retp, lookb=11),
  RcppRoll=RcppRoll::roll_var(retp, n=11),
  times=10))[, c(1, 4, 5)]  # end microbenchmark summary
# Compare the speed of HighFreq::roll_var() with TTR::runMAD()
summary(microbenchmark(
    Rcpp=HighFreq::roll_var(retp, lookb=11, method="quantile"),
    TTR=TTR::runMAD(retp, n = 11),
    times=10))[, c(1, 4, 5)]  # end microbenchmark summary

## End(Not run)

algoquant/HighFreq documentation built on Oct. 26, 2024, 9:20 p.m.