calc_invsvd: Calculate the _reduced inverse_ of a _matrix_ of data using...

View source: R/RcppExports.R

calc_invsvdR Documentation

Calculate the reduced inverse of a matrix of data using Singular Value Decomposition (SVD).

Description

Calculate the reduced inverse of a matrix of data using Singular Value Decomposition (SVD).

Usage

calc_invsvd(matrixv, dimax = 0L, singmin = 0)

Arguments

matrixv

A matrix of data.

dimax

An integer equal to the number of singular values used for calculating the reduced inverse of the matrix matrixv (the default is dimax = 0 - standard matrix inverse using all the singular values).

singmin

A numeric threshold level for discarding small singular values in order to regularize the inverse of the matrix matrixv (the default is 0.0).

Details

The function calc_invsvd() calculates the reduced inverse of the matrix matrixv using Singular Value Decomposition (SVD).

The function calc_invsvd() first performs Singular Value Decomposition (SVD) of the matrix matrixv. The SVD of a matrix \strong{C} is defined as the factorization:

\strong{C} = \strong{U} \, \Sigma \, \strong{V}^T

Where \strong{U} and \strong{V} are the left and right singular matrices, and \Sigma is a diagonal matrix of singular values.

The inverse \strong{C}^{-1} of the matrix \strong{C} can be calculated from the SVD matrices as:

\strong{C}^{-1} = \strong{V} \, \Sigma^{-1} \, \strong{U}^T

The reduced inverse of the matrix \strong{C} is obtained by removing singular vectors with very small singular values:

\strong{C}^{-1} = \strong{V}_n \, \Sigma_n^{-1} \, \strong{U}_n^T

Where \strong{U}_n, \strong{V}_n and \Sigma_n are the SVD matrices with the rows and columns corresponding to very small singular values removed.

The function calc_invsvd() applies regularization to the matrix inverse using the arguments dimax and singmin.

The function calc_invsvd() applies regularization by discarding the smallest singular values \sigma_i that are less than the threshold level singmin times the sum of all the singular values:

\sigma_i < eigen\_thresh \cdot (\sum{\sigma_i})

It also discards additional singular vectors so that only the highest order singular vectors remain, up to order dimax. It calculates the reduced inverse from the SVD matrices using only the largest singular values up to order dimax. For example, if dimax = 3 then it only uses the 3 highest order singular vectors, with the largest singular values. This has the effect of dimension reduction.

If the matrix matrixv has a large number of small singular values, then the number of remaining singular values may be less than dimax.

Value

A matrix equal to the reduced inverse of the matrix matrixv.

Examples

## Not run: 
# Calculate ETF returns
retp <- na.omit(rutils::etfenv$returns[, c("VTI", "TLT", "DBC")])
# Calculate covariance matrix
covmat <- cov(retp)
# Calculate matrix inverse using RcppArmadillo
invmat <- HighFreq::calc_invsvd(covmat)
# Calculate matrix inverse in R
invr <- solve(covmat)
all.equal(invmat, invr, check.attributes=FALSE)
# Calculate reduced inverse using RcppArmadillo
invmat <- HighFreq::calc_invsvd(covmat, dimax=3)
# Calculate reduced inverse from SVD in R
svdec <- svd(covmat)
dimax <- 1:3
invr <- svdec$v[, dimax] %*% (t(svdec$u[, dimax])/svdec$d[dimax])
# Compare RcppArmadillo with R
all.equal(invmat, invr)

## End(Not run)


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