Package: cvxcovariance
cvxcovarianceThecvxcovariancepackage
provides simple tools for creating an estimate $\hat\Sigma_t$ of the covariance
$\Sigma_t$ of the $n$-dimensional return vectors $r_t$, $t=1,2,\ldots$, based on
the observed returns $r_1, \ldots, r_{t-1}$. (Here $r_t$ is the return from
$t-1$ to $t$.) The covariance predictor $\hat\Sigma_t$ is generated by blending
$K$ different "expert" predictors $\hat\Sigma_t^{(1)},\ldots,\hat\Sigma_t^{(K)}$,
by solving a convex optimization problem at each time step.For a detailed description of the methodology, see our manuscriptA Simple Method for Predicting Covariance Matrices of Financial Returns(in particular Section 3).In the simplest case the user provides a $T\times n$ pandas DataFrame
of returns $r_1,\ldots,r_T$ and $K$ half-life pairs, and gets back covariance
predictors for each time step. (The $K$ experts are computed as iterated
exponentially weighted moving average (IEWMA) predictors as described in
Section 2.6 of thepaper.)
In the more general case, the user provides the $K$ expert predictors
$\hat\Sigma_t^{(1)},\ldots,\hat\Sigma_t^{(K)}$, $t=1,\ldots,T$, and these are
blended together by solving the convex optimization problems. In either case
the result is returned as an iterator object over namedtuples:Result = namedtuple("Result", ["time", "mean", "covariance", "weights"]).Note: at time $t$ the user is provided with $\Sigma_{t+1}$,
$\textit{i.e.}$, the covariance matrix for the next time step.
SoResult.covariancereturns the covariance prediction fortime+1.InstallationTo install the package, run the following command in the terminal:pipinstallcvxcovarianceUsageThere are two alternative ways to use the package. The first is to use thefrom_ewmasfunction to create a combined multiple IEWMA (CM-IEWMA) predictor.
The second is to provide your own covariance experts, via dictionaries,
and pass them to thefrom_sigmasfunction. Both functions return an object
of the_CovarianceCombinationclass, which can be used to solve the covariance
combination problem.CM-IEWMAThefrom_ewmasfunction takes as input a pandas DataFrame of
returns and the IEWMA half-life pairs (each pair consists of one half-life for
volatility estimation and one for correlation estimation), and returns an
iterator object that iterates over the CM-IEWMA covariance predictors defined
via namedtuples. Through the namedtuple you can access thetime,mean,covariance, andweightsattributes.timeis the timestamp.meanis the estimated mean of the return at the $\textit{next}$ timestamp,
$\textit{i.e.}$time+1, if the user wants to estimate the mean; per default
the mean is set to zero, which is a reasonable assumption for many financial
returns.covarianceis the estimated covariance matrix for the $\textit{next}$
timestamp, $\textit{i.e.}$time+1.weightsare the $K$ weights attributed
to the experts. Here is an example:importpandasaspdfromcvx.covariance.combinationimportfrom_ewmas# Load return datareturns=pd.read_csv("data/ff5.csv",index_col=0,header=0,parse_dates=True).iloc[:1000]n=returns.shape[1]# Define half-life pairs for K=3 experts, (halflife_vola, halflife_cov)halflife_pairs=[(10,21),(21,63),(63,125)]# Define the covariance combinatorcombinator=from_ewmas(returns,halflife_pairs,min_periods_vola=n,# min periods for volatility estimationmin_periods_cov=3*n)# min periods for correlation estimation# Solve combination problem and loop through combination results to get predictorscovariance_predictors={}forpredictorincombinator.solve(window=10):# lookback window for optimization# From predictor we can access predictor.time, predictor.mean (=0 here),# predictor.covariance, and predictor.weightscovariance_predictors[predictor.time]=predictor.covarianceHerecovariance_predictors[t]is the covariance prediction for time $t+1$,
$\textit{i.e.}$, it is uses knowledge of $r_1,\ldots,r_t$.General covariance combinationThefrom_sigmasfunction takes as input a pandas DataFrame of
returns and a dictionary of covariance predictors{key: {time: sigma}, wherekeyis the key of an expert predictor and{time: sigma}is the expert predictions. For example, here we combine two
EWMA covariance predictors from pandas:importpandasaspdfromcvx.covariance.combinationimportfrom_sigmas# Load return datareturns=pd.read_csv("data/ff5.csv",index_col=0,header=0,parse_dates=True).iloc[:1000]n=returns.shape[1]# Define 21 and 63 day EWMAs as dictionaries (K=2 experts)ewma21=returns.ewm(halflife=21,min_periods=5*n).cov().dropna()expert1={time:ewma21.loc[time]fortimeinewma21.index.get_level_values(0).unique()}ewma63=returns.ewm(halflife=63,min_periods=5*n).cov().dropna()expert2={time:ewma63.loc[time]fortimeinewma63.index.get_level_values(0).unique()}# Create expert dictionaryexperts={1:expert1,2:expert2}# Define the covariance combinatorcombinator=from_sigmas(sigmas=experts,returns=returns)# Solve combination problem and loop through combination results to get predictorscovariance_predictors={}forpredictorincombinator.solve(window=10):# From predictor we can access predictor.time, predictor.mean (=0 here),# predictor.covariance, and predictor.weightscovariance_predictors[predictor.time]=predictor.covarianceHerecovariance_predictors[t]is the covariance prediction for time
$t+1$, $\textit{i.e.}$, it is uses knowledge of $r_1,\ldots,r_t$.PoetryWe assume you share already the love forPoetry.
Once you have installed poetry you can performmakeinstallto replicate the virtual environment we have defined inpyproject.tomland locked inpoetry.lock.JupyterWe installJupyterLabon fly within the aforementioned
virtual environment. Executingmakejupyterwill install and start the jupyter lab.CitingIf you want to reference our paper in your research,
please consider citing us by using the following BibTeX:@misc{johansson2023covariance,title={A Simple Method for Predicting Covariance Matrices of Financial Returns},author={Kasper Johansson and Mehmet Giray Ogut and Markus Pelgerand Thomas Schmelzer and Stephen Boyd},year={2023},eprint={2305.19484},archivePrefix={arXiv},}