run_autocovar: Calculate the trailing autocovariance of a _time series_ of...

View source: R/RcppExports.R

run_autocovarR Documentation

Calculate the trailing autocovariance of a time series of returns using an online recursive formula.

Description

Calculate the trailing autocovariance of a time series of returns using an online recursive formula.

Usage

run_autocovar(tseries, lambda, lagg = 1L)

Arguments

tseries

A time series or a matrix with a single column of returns data.

lambda

A decay factor which multiplies past estimates.

lagg

An integer equal to the number of periods to lag. (The default is lagg = 1.)

Details

The function run_autocovar() calculates the trailing autocovariance of a streaming time series of returns, by recursively weighting the past autocovariance estimates {cov}_{t-1}, with the products of their returns minus their means, using the decay factor \lambda:

\bar{x}_t = \lambda \bar{x}_{t-1} + (1-\lambda) x_t

\sigma^2_{t} = \lambda \sigma^2_{t-1} + (1-\lambda) (x_t - \bar{x}_t)^2

{cov}_t = \lambda {cov}_{t-1} + (1-\lambda) (x_t - \bar{x}_t) (x_{t-l} - \bar{x}_{t-l})

Where {cov}_t is the trailing autocovariance estimate at time t, with lagg=l. And \sigma^2_{t} and \bar{x}_t are the trailing variances and means of the streaming data.

The above online recursive formulas are convenient for processing live streaming data because they don't require maintaining a buffer of past data. The formulas are equivalent to a convolution with exponentially decaying weights, but they're much faster to calculate. Using exponentially decaying weights is more natural than using a sliding look-back interval, because it gradually "forgets" about the past data.

The value of the decay factor \lambda must be in the range between 0 and 1. If \lambda is close to 1 then the decay is weak and past values have a greater weight, and the trailing covariance values have a stronger dependence on past data. This is equivalent to a long look-back interval. If \lambda is much less than 1 then the decay is strong and past values have a smaller weight, and the trailing covariance values have a weaker dependence on past data. This is equivalent to a short look-back interval.

The function run_autocovar() returns three columns of data: the trailing autocovariances, the variances, and the mean values of the argument tseries. This allows calculating the trailing autocorrelations.

Value

A matrix with three columns of data: the trailing autocovariances, the variances, and the mean values of the argument tseries.

Examples

## Not run: 
# Calculate historical returns
retp <- zoo::coredata(na.omit(rutils::etfenv$returns$VTI))
# Calculate the trailing autocovariance
lambdaf <- 0.9
lagg <- 3
covars <- HighFreq::run_autocovar(retp, lambda=lambdaf, lagg=lagg)
# Calculate the trailing autocorrelation
correl <- covars[, 1]/covars[, 2]
# Calculate the trailing autocovariance using R code
nrows <- NROW(retp)
retm <- numeric(nrows)
retm[1] <- retp[1, ]
retd <- numeric(nrows)
covarr <- numeric(nrows)
covarr[1] <- retp[1, ]^2
for (it in 2:nrows) {
  retm[it] <- lambdaf*retm[it-1] + (1-lambdaf)*(retp[it])
  retd[it] <- retp[it] - retm[it]
  covarr[it] <- lambdaf*covarr[it-1] + (1-lambdaf)*retd[it]*retd[max(it-lagg, 1)]
} # end for
all.equal(covarr, covars[, 1])

## End(Not run)


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