roll_skew: Calculate a _matrix_ of skewness estimates over a rolling...

View source: R/RcppExports.R

roll_skewR Documentation

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

Description

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

Usage

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

Arguments

tseries

A time series or a matrix of data.

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).

lookb

The number of end points in the look-back interval (the default is lookb = 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 specifying the type of the skewness 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 roll_skew() calculates a matrix of skewness estimates over rolling look-back intervals attached at the end points of the time series tseries.

The function roll_skew() 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_skew(), which calculates the skewness. See the function calc_skew() for a description of the skewness 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 skewness 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_skew() is implemented in RcppArmadillo C++ code, which makes it several times faster than R code.

Value

A matrix of skewness 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)
# Define end points and start points
endd <- 1 + HighFreq::calc_endpoints(NROW(retp), step=25)
startp <- HighFreq::calc_startpoints(endd, lookb=3)
# Calculate the rolling skewness at 25 day end points, with a 75 day look-back
skewv <- HighFreq::roll_skew(retp, step=25, lookb=3)
# Calculate the rolling skewness using R code
skewr <- sapply(1:NROW(endd), function(it) {
  HighFreq::calc_skew(retp[startp[it]:endd[it], ])
})  # end sapply
# Compare the skewness estimates
all.equal(drop(skewv), skewr, check.attributes=FALSE)
# Compare the speed of RcppArmadillo with R code
library(microbenchmark)
summary(microbenchmark(
  Rcpp=HighFreq::roll_skew(retp, step=25, lookb=3),
  Rcode=sapply(1:NROW(endd), function(it) {
    HighFreq::calc_skew(retp[startp[it]:endd[it], ])
  }),
  times=10))[, c(1, 4, 5)]  # end microbenchmark summary

## End(Not run)

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