push_eigen: Update the trailing eigen values and eigen vectors of...

View source: R/RcppExports.R

push_eigenR Documentation

Update the trailing eigen values and eigen vectors of streaming asset return data, with a row of new returns.

Description

Update the trailing eigen values and eigen vectors of streaming asset return data, with a row of new returns.

Usage

push_eigen(retsn, covmat, eigenval, eigenvec, eigenret, meanv, lambdacov)

Arguments

retsn

A vector of new asset returns.

covmat

A trailing covariance matrix of asset returns.

eigenval

A vector of eigen values.

eigenvec

A matrix of eigen vectors.

eigenret

A vector of eigen portfolio returns.

meanv

A vector of trailing means of asset returns.

lambdacov

A decay factor which multiplies the past covariance.

Details

The function push_eigen() updates the trailing eigen values, eigen vectors, and the eigen portfolio returns of streaming asset returns, with a row of new data. It updates the eigenelements in place, without copying the data in memory.

The streaming asset returns r_t contain multiple columns and the parameter retsn represents a single row of r_t - the asset returns at time t. The elements of the vectors retsn, eigenret, and meanv represent single rows of data with multiple columns.

The function push_eigen() accepts pointers to the arguments eigenval, eigenval, eigenvec, meanv, and eigenret, and it overwrites the old values with the new values. It performs the calculation in place, without copying the data in memory, which can significantly increase the computation speed for large matrices.

First, the function push_eigen() calls the function HighFreq::push_covar() to update the trailing covariance matrix of streaming asset returns, with a row of new returns. It updates the covariance matrix in place, without copying the data in memory.

It then calls the Armadillo function arma::eig_sym to calculate the eigen decomposition of the trailing covariance matrix.

The function push_eigen() calculates the eigen portfolio returns by multiplying the scaled asset returns times the eigen vectors \strong{v}_{t-1}:

r^{eigen}_t = \strong{v}_{t-1} \frac{r_t}{\sigma_{t-1}}

Where \strong{v}_{t-1} is the matrix of previous eigen vectors that are passed by reference through the parameter eigenvec. The eigen returns r^{eigen}_t are the returns of the eigen portfolios, with weights equal to the eigen vectors \strong{v}_{t-1}. The eigen weights are applied to the asset returns scaled by their volatilities. The eigen returns r^{eigen}_t are passed by reference through the parameter eigenret.

The decay factor \lambda determines the strength of the updates, with smaller \lambda values giving more weight to the new data. If the asset returns are not stationary, then applying more weight to the new returns reduces the bias of the trailing covariance matrix, but it also increases its variance. Simulation can be used to find the value of the \lambda parameter to achieve the best bias-variance tradeoff.

The function push_eigen() is written in RcppArmadillo C++ so it's much faster than R code.

Value

Void (no return value - modifies the trailing eigen values, eigen vectors, the eigen portfolio returns, and the return means in place).

Examples

## Not run: 
# Calculate a time series of returns
retp <- na.omit(rutils::etfenv$returns[, c("IEF", "VTI", "DBC")])
# Calculate the returns without the last row
nrows <- NROW(retp)
retss <- retp[-nrows]
# Calculate the previous covariance of returns
meanv <- colMeans(retss)
covmat <- cov(retss)
# Update the covariance of returns
eigenret <- numeric(NCOL(retp))
HighFreq::push_eigen(retsn=retp[nrows], covmat=covmat, 
  eigenval=eigenval, eigenvec=eigenvec, 
  eigenret=eigenret, meanv=meanv, lambdacov=0.9)

## End(Not run)


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