calc_var: Calculate the dispersion (variance) of the columns of a _time...

View source: R/RcppExports.R

calc_varR Documentation

Calculate the dispersion (variance) of the columns of a time series or a matrix using RcppArmadillo.

Description

Calculate the dispersion (variance) of the columns of a time series or a matrix using RcppArmadillo.

Usage

calc_var(tseries, method = "moment", confl = 0.75)

Arguments

tseries

A time series or a matrix of data.

method

A character string specifying the type of the dispersion model (the default is method = "moment" - see Details).

confl

The confidence level for calculating the quantiles of returns (the default is confl = 0.75).

Details

The function calc_var() calculates the dispersion of the columns of a time series or a matrix of data using RcppArmadillo C++ code.

The dispersion is a measure of the variability of the data. Examples of dispersion are the variance and the Median Absolute Deviation (MAD).

If method = "moment" (the default) then calc_var() calculates the dispersion as the second moment of the data (the variance). Then calc_var() performs the same calculation as the function colVars() from package matrixStats, but it's much faster because it uses RcppArmadillo C++ code.

If method = "quantile" then it calculates the dispersion as the difference between the quantiles as follows:

\sigma = q_{\alpha} - q_{1-\alpha}

Where \alpha is the confidence level for calculating the quantiles.

If method = "nonparametric" then it calculates the dispersion as the Median Absolute Deviation (MAD):

MAD = median(abs(x - median(x)))

It also multiplies the MAD by a factor of 1.4826, to make it comparable to the standard deviation.

If method = "nonparametric" then calc_var() performs the same calculation as the function stats::mad(), but it's much faster because it uses RcppArmadillo C++ code.

If the number of rows of tseries is less than 3 then it returns zeros.

Value

A row vector equal to the dispersion of the columns of the matrix tseries.

Examples

## Not run: 
# Calculate VTI and XLF returns
retp <- na.omit(rutils::etfenv$returns[, c("VTI", "XLF")])
# Compare HighFreq::calc_var() with standard var()
all.equal(drop(HighFreq::calc_var(retp)), 
  apply(retp, 2, var), check.attributes=FALSE)
# Compare HighFreq::calc_var() with matrixStats
all.equal(drop(HighFreq::calc_var(retp)), 
  matrixStats::colVars(retp), check.attributes=FALSE)
# Compare the speed of RcppArmadillo with matrixStats and with R code
library(microbenchmark)
summary(microbenchmark(
  Rcpp=HighFreq::calc_var(retp),
  matrixStats=matrixStats::colVars(retp),
  Rcode=apply(retp, 2, var),
  times=10))[, c(1, 4, 5)]  # end microbenchmark summary
# Compare HighFreq::calc_var() with stats::mad()
all.equal(drop(HighFreq::calc_var(retp, method="nonparametric")), 
  sapply(retp, mad), check.attributes=FALSE)
# Compare the speed of RcppArmadillo with stats::mad()
summary(microbenchmark(
  Rcpp=HighFreq::calc_var(retp, method="nonparametric"),
  Rcode=sapply(retp, mad),
  times=10))[, c(1, 4, 5)]  # end microbenchmark summary

## End(Not run)


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