| calc_covar | R Documentation |
RcppArmadillo.Calculate the covariance matrix of the columns of a time series
using RcppArmadillo.
calc_covar(timeser, method = "moment", confl = 0.75)
timeser |
A time series or a matrix of data. |
method |
A character string specifying the type of the
covariance model (the default is |
confl |
The confidence level for calculating the quantiles of
returns (the default is |
The function calc_covar() calculates the covariance matrix of the
columns of a time series or a matrix of data using
RcppArmadillo C++ code.
The covariance is a measure of the codependency of the data.
If method = "moment" (the default) then calc_covar()
calculates the covariance as the second co-moment:
\sigma_{xy} = \frac{1}{n-1} \sum_{i=1}^n (x_i - \bar{x}) (y_i - \bar{y})
Then calc_covar() performs the same calculation as the R
function stats::cov().
If method = "quantile" then it calculates the covariance as the
difference between the quantiles as follows:
\mu = q_{\alpha} - q_{1-\alpha}
Where \alpha is the confidence level for calculating the quantiles.
If method = "nonparametric" then it calculates the covariance as the
Median Absolute Deviation (MAD):
MAD = median(abs(x - median(x)))
It also multiplies the MAD by a factor of 1.4826, to make it
comparable to the standard deviation.
If method = "nonparametric" then calc_covar() performs the
same calculation as the function stats::mad(), but it's much faster
because it uses RcppArmadillo C++ code.
If the number of rows of timeser is less than 3 then it
returns zeros.
A square matrix with the covariance coefficients of the columns of
the time series timeser.
## Not run:
# Calculate VTI and XLF returns
retp <- na.omit(rutils::etfenv$returns[, c("VTI", "XLF")])
# Compare HighFreq::calc_covar() with standard var()
all.equal(drop(HighFreq::calc_covar(retp)),
cov(retp), check.attributes=FALSE)
# Compare the speed of RcppArmadillo with matrixStats and with R code
library(microbenchmark)
summary(microbenchmark(
Rcpp=HighFreq::calc_covar(retp),
Rcode=cov(retp),
times=10))[, c(1, 4, 5)] # end microbenchmark summary
# Compare HighFreq::calc_covar() with stats::mad()
all.equal(drop(HighFreq::calc_covar(retp, method="nonparametric")),
sapply(retp, mad), check.attributes=FALSE)
# Compare the speed of RcppArmadillo with stats::mad()
summary(microbenchmark(
Rcpp=HighFreq::calc_covar(retp, method="nonparametric"),
Rcode=sapply(retp, mad),
times=10))[, c(1, 4, 5)] # end microbenchmark summary
## End(Not run) # end dontrun
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.