roll_zscores: Calculate a _vector_ of z-scores of the residuals of rolling...

View source: R/RcppExports.R

roll_zscoresR Documentation

Calculate a vector of z-scores of the residuals of rolling regressions at the end points of the predictor matrix.

Description

Calculate a vector of z-scores of the residuals of rolling regressions at the end points of the predictor matrix.

Usage

roll_zscores(
  respv,
  predm,
  startp = 0L,
  endd = 0L,
  step = 1L,
  lookb = 1L,
  stub = 0L
)

Arguments

respv

A single-column time series or a vector of response data.

predm

A time series or a matrix of predictor 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).

Details

The function roll_zscores() calculates a vector of z-scores of the residuals of rolling regressions at the end points of the time series predm.

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

It passes the subset time series to the function calc_lm(), which calculates the regression data.

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

Value

A column vector of the same length as the number of rows of predm.

Examples

## Not run: 
# Calculate historical returns
retp <- na.omit(rutils::etfenv$returns[, c("XLF", "VTI", "IEF")])
# Response equals XLF returns
respv <- retp[, 1]
# Predictor matrix equals VTI and IEF returns
predm <- retp[, -1]
# Calculate Z-scores from rolling time series regression using RcppArmadillo
lookb <- 11
zscores <- HighFreq::roll_zscores(respv=respv, predm=predm, lookb)
# Calculate z-scores in R from rolling multivariate regression using lm()
zscoresr <- sapply(1:NROW(predm), function(ro_w) {
  if (ro_w == 1) return(0)
  startpoint <- max(1, ro_w-lookb+1)
  responsi <- response[startpoint:ro_w]
  predicti <- predictor[startpoint:ro_w, ]
  regmod <- lm(responsi ~ predicti)
  residuals <- regmod$residuals
  residuals[NROW(residuals)]/sd(residuals)
})  # end sapply
# Compare the outputs of both functions
all.equal(zscores[-(1:lookb)], zscoresr[-(1:lookb)], 
  check.attributes=FALSE)

## End(Not run)


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