roll_scale: Perform a rolling standardization (centering and scaling) of...

View source: R/RcppExports.R

roll_scaleR Documentation

Perform a rolling standardization (centering and scaling) of the columns of a time series of data using RcppArmadillo.

Description

Perform a rolling standardization (centering and scaling) of the columns of a time series of data using RcppArmadillo.

Usage

roll_scale(matrix, lookb, center = TRUE, scale = TRUE, use_median = FALSE)

Arguments

tseries

A time series or matrix of data.

lookb

The length of the look-back interval, equal to the number of rows of data used in the scaling.

center

A Boolean argument: if TRUE then center the columns so that they have zero mean or median (the default is TRUE).

scale

A Boolean argument: if TRUE then scale the columns so that they have unit standard deviation or MAD (the default is TRUE).

use_median

A Boolean argument: if TRUE then the centrality (central tendency) is calculated as the median and the dispersion is calculated as the median absolute deviation (MAD) (the default is FALSE). If use_median = FALSE then the centrality is calculated as the mean and the dispersion is calculated as the standard deviation.

Details

The function roll_scale() performs a rolling standardization (centering and scaling) of the columns of the tseries argument using RcppArmadillo. The function roll_scale() performs a loop over the rows of tseries, subsets a number of previous (past) rows equal to lookb, and standardizes the subset matrix by calling the function calc_scale(). It assigns the last row of the standardized subset matrix to the return matrix.

If the arguments center and scale are both TRUE and use_median is FALSE (the defaults), then calc_scale() performs the same calculation as the function roll::roll_scale().

If the arguments center and scale are both TRUE (the defaults), then calc_scale() standardizes the data. If the argument center is FALSE then calc_scale() only scales the data (divides it by the standard deviations). If the argument scale is FALSE then calc_scale() only demeans the data (subtracts the means).

If the argument use_median is TRUE, then it calculates the centrality as the median and the dispersion as the median absolute deviation (MAD).

Value

A matrix with the same dimensions as the input argument tseries.

Examples

## Not run: 
# Calculate a time series of returns
retp <- zoo::coredata(na.omit(rutils::etfenv$returns[, c("IEF", "VTI")]))
lookb <- 11
rolled_scaled <- roll::roll_scale(retp, width=lookb, min_obs=1)
rolled_scaled2 <- HighFreq::roll_scale(retp, lookb=lookb)
all.equal(rolled_scaled[-(1:2), ], rolled_scaled2[-(1:2), ],
  check.attributes=FALSE)

## End(Not run)


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