The package CovEstim includes various functions for estimating the covariance matrix of a dataset, especially in a high-dimensional setting. A dataset of 200 monthly returns of stocks from the S&P 500 index is provided for testing purposes.
Here is a short overview of the estimation methods:
You can install this version of the package from Github with:
install.packages("devtools")
library(devtools)
install_github("antshi/CovEstim")
library(CovEstim)
These are basic examples which show you how to use the wrapper function sigma_estim_wrapper. First, let's prepare with
# include the package
library(covEstim)
# data
data(sp200)
str(sp200)
# delete the date column, since the estimation functions expect a matrix.
sp_rets <- as.matrix(sp200[,-1])
Maximum-Likelihood Estimator
sigma_ml <- sigma_estim_ml(sp_rets)[[1]]
sigma_ml <- sigma_estim_wrapper(sp_rets, est_func=sigma_estim_ml)
sigma_ml <- sigma_estim(sp_rets, est_type="ML")
Linear Shrinkage towards the Constant Correlation Covariance Matrix
sigma_lwcc_results <- sigma_estim_lwcc(sp_rets)
sigma_lwcc <- sigma_lwcc_results[[1]]
sigma_lwcc_param <- sigma_lwcc_results[[2]]
# The argument res_all=FALSE (default) in the wrapper functions sigma_estim and sigma_estim_wrapper returns only the covariance estimator.
sigma_lwcc <- sigma_estim_wrapper(sp_rets, sigma_estim_lwcc, res_all=FALSE)
You can also parse the user-defined tuning parameters such as shrinking intensity to the estimation function.
sigma_lwcc_results <- sigma_estim_wrapper(sp_rets, sigma_estim_lwcc, res_all=TRUE, shrink_int=0.3)
str(sigma_lwcc_results)
The linear and nonlinear shrinkage estimators by Ledoit and Wolf are implemented additionally in Rcpp for higher efficiency. An important note here is that while the R-function can deal with data frames and matrices, the Rcpp-function requires the data to be preformated as a matrix.
sigma_lwcc_results <- sigma_estim_wrapper(sp_rets, sigma_estim_lwcc, res_all=TRUE)
str(sigma_lwcc_results)
sigma_lwcc_results <- sigma_estim_wrapper(sp_rets, sigma_estim_lwcc_cpp, res_all=TRUE)
str(sigma_lwcc_results)
Some more complicated application would be the Approximate Factor Model with a nonlinear shrinkage estimation on the covariance matrix of residuals
sigma_afm_lwnl_results <- sigma_estim_wrapper(sp_rets, est_func = sigma_estim_afm, resid_est_func=sigma_estim_lwnl)
str(sigma_afm_lwnl_results)
While the function sigma_estim_wrapper can take up any estimation function from the package, the function sigma_estim is interesting for practical applications. For example, if you want to calculate different covariance estimators on the same dataset for comparison purposes, sigma_estim is easier to work with.
# Maximum-Likelihood, Exact Factor Model, Ledoit-Wolf linear shrinkage and Approximate Factor Model with nonlinear shrinkage on the residuals:
est_types <- c("ML", "EFM", "LW-IDENT", "AFM-LWNL")
sigma_list <- lapply(est_types, sigma_estim, data=sp_rets)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.