| calc_inv | R Documentation |
Calculate the reduced inverse of a symmetric matrix of data using eigen decomposition.
calc_inv(matrixv, dimax = 0L, singmin = 0)
matrixv |
A symmetric matrix of data. |
dimax |
An integer equal to the number of eigen
values used for calculating the reduced inverse of the matrix
|
singmin |
A numeric threshold level for discarding
small eigen values in order to regularize the inverse of the matrix
|
The function calc_inv() calculates the reduced inverse
of the matrix matrixv using eigen decomposition.
The function calc_inv() first performs eigen decomposition of the
matrix matrixv.
The eigen decomposition of a matrix \strong{C} is defined as the
factorization:
\strong{C} = \strong{O} \, \Sigma \, \strong{O}^T
Where \strong{O} is the matrix of eigen vectors and
\Sigma is a diagonal matrix of eigen values.
The inverse \strong{C}^{-1} of the matrix \strong{C} can be
calculated from the eigen decomposition as:
\strong{C}^{-1} = \strong{O} \, \Sigma^{-1} \, \strong{O}^T
The reduced inverse of the matrix \strong{C} is obtained
by removing eigen vectors with very small eigen values:
\strong{C}^{-1} = \strong{O}_{dimax} \, \Sigma^{-1}_{dimax} \, \strong{O}^T_{dimax}
Where \strong{O}_{dimax} is the matrix of eigen vectors
that correspond to the largest eigen values \Sigma_{dimax}.
The function calc_inv() applies regularization to the matrix
inverse using the arguments dimax and singmin.
The function calc_inv() applies regularization by discarding the
smallest eigen values \Sigma_i that are less than the
threshold level singmin times the sum of all the eigen
values:
\Sigma_i < eigen\_thresh \cdot (\sum{\Sigma_i})
It also discards additional eigen vectors so that only the highest
order eigen vectors remain, up to order dimax.
It calculates the reduced inverse from the eigen decomposition
using only the largest eigen values up to dimax. For
example, if
dimax = 3 then it only uses the 3 highest order eigen
vectors, with the largest eigen values. This has the effect of
dimension reduction.
If the matrix matrixv has a large number of small eigen
values, then the number of remaining eigen values may be less than
dimax.
A matrix equal to the reduced inverse of the
matrix matrixv.
## 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_inv(covmat)
# Calculate matrix inverse in R
invr <- solve(covmat)
all.equal(invmat, invr, check.attributes=FALSE)
# Calculate the reduced inverse using RcppArmadillo
invmat <- HighFreq::calc_inv(covmat, dimax=3)
# Calculate the reduced inverse using eigen decomposition in R
eigend <- eigen(covmat)
dimax <- 1:3
invr <- eigend$vectors[, dimax] %*% (t(eigend$vectors[, dimax])/eigend$values[dimax])
# Compare RcppArmadillo with R
all.equal(invmat, invr)
## End(Not run) # end dontrun
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.