https://github.com/cvxgrp/cov_pred_finance

https://github.com/cvxgrp/cov_pred_finance

Science Score: 57.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
    Found 4 DOI reference(s) in README
  • Academic publication links
  • Committers with academic emails
    1 of 7 committers (14.3%) from academic institutions
  • Institutional organization owner
    Organization cvxgrp has institutional domain (www.stanford.edu)
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (14.9%) to scientific vocabulary

Keywords from Contributors

archive transformer parallel optimizer quantum-circuit gan distributed investing projection argument-parser
Last synced: 7 months ago · JSON representation

Repository

Basic Info
  • Host: GitHub
  • Owner: cvxgrp
  • License: apache-2.0
  • Language: Jupyter Notebook
  • Default Branch: main
  • Size: 198 MB
Statistics
  • Stars: 68
  • Watchers: 4
  • Forks: 11
  • Open Issues: 17
  • Releases: 16
Created almost 3 years ago · Last pushed 10 months ago
Metadata Files
Readme Contributing License Code of conduct

README.md

cvxcovariance

Coverage Status PyPI version License Downloads Renovate enabled

Quick Start in Online IDEs

GitHub Codespaces

Open in GitHub Codespaces Start coding instantly in a pre-configured cloud development environment.

The cvxcovariance package provides simple tools for creating an estimate $\hat\Sigmat$ of the covariance $\Sigmat$ of the $n$-dimensional return vectors $rt$, $t=1,2,\ldots$, based on the observed returns $r1, \ldots, r{t-1}$. (Here $rt$ is the return from $t-1$ to $t$.) The covariance predictor $\hat\Sigmat$ is generated by blending $K$ different "expert" predictors $\hat\Sigmat^{(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 manuscript A 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 $r1,\ldots,rT$ 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.5 of the paper.) In the more general case, the user provides the $K$ expert predictors $\hat\Sigmat^{(1)},\ldots,\hat\Sigmat^{(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 $\hat\Sigma_{t+1}$, $\textit{i.e.}$, the covariance matrix for the next time step. So Result.covariance returns the covariance prediction for time+1.

Installation

To install the package, run the following command in the terminal:

bash pip install cvxcovariance

Usage

There are two alternative ways to use the package. The first is to use the from_ewmas function to create a combined multiple IEWMA (CM-IEWMA) predictor. The second is to provide your own covariance experts, via dictionaries, and pass them to the from_sigmas function. Both functions return an object of the _CovarianceCombination class, which can be used to solve the covariance combination problem.

CM-IEWMA

The from_ewmas function 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 the time, mean, covariance, and weights attributes. time is the timestamp. mean is 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. covariance is the estimated covariance matrix for the $\textit{next}$ timestamp, $\textit{i.e.}$ time+1. weights are the $K$ weights attributed to the experts. Here is an example:

```python import pandas as pd from cvx.covariance.combination import from_ewmas

Load return data

returns = pd.readcsv("data/ff5norf.csv", indexcol=0, header=0, parse_dates=True).iloc[:1000] n = returns.shape[1]

Define half-life pairs for K=3 experts, (halflifevola, halflifecov)

halflife_pairs = [(10, 21), (21, 63), (63, 125)]

Define the covariance combinator

combinator = fromewmas(returns, halflifepairs, minperiodsvola=n, # min periods for volatility estimation minperiodscov=3 * n) # min periods for correlation estimation

Solve combination problem and loop through combination results to get predictors

covariancepredictors = {} for predictor in combinator.solve(window=10): # lookback window for optimization # From predictor we can access predictor.time, predictor.mean (=0 here), # predictor.covariance, and predictor.weights covariancepredictors[predictor.time] = predictor.covariance ```

Here covariance_predictors[t] is the covariance prediction for time $t+1$, $\textit{i.e.}$, it is uses knowledge of $r1,\ldots,rt$.

General covariance combination

The from_sigmas function takes as input a pandas DataFrame of returns and a dictionary of covariance predictors {key: {time: sigma}, where key is the key of an expert predictor and {time: sigma} is the expert predictions. For example, here we combine two EWMA covariance predictors from pandas:

```python import pandas as pd from cvx.covariance.combination import from_sigmas

Load return data

returns = pd.readcsv("data/ff5norf.csv", indexcol=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, minperiods=5 * n).cov().dropna() expert1 = {time: ewma21.loc[time] for time in ewma21.index.getlevelvalues(0).unique()} ewma63 = returns.ewm(halflife=63, minperiods=5 * n).cov().dropna() expert2 = {time: ewma63.loc[time] for time in ewma63.index.getlevelvalues(0).unique()}

Create expert dictionary

experts = {1: expert1, 2: expert2}

Define the covariance combinator

combinator = from_sigmas(sigmas=experts, returns=returns)

Solve combination problem and loop through combination results to get predictors

covariancepredictors = {} for predictor in combinator.solve(window=10): # From predictor we can access predictor.time, predictor.mean (=0 here), # predictor.covariance, and predictor.weights covariancepredictors[predictor.time] = predictor.covariance ```

Here covariance_predictors[t] is the covariance prediction for time $t+1$, $\textit{i.e.}$, it is uses knowledge of $r1,\ldots,rt$.

Poetry

We assume you share already the love for Poetry. Once you have installed poetry you can perform

bash make install

to replicate the virtual environment we have defined in pyproject.toml and locked in poetry.lock.

Jupyter

We install JupyterLab on fly within the aforementioned virtual environment. Executing

bash make jupyter

will install and start the jupyter lab.

Citing

If you want to reference our paper in your research, please consider citing us by using the following BibTeX:

BibTeX @article{johansson2023covariance, url = {http://dx.doi.org/10.1561/0800000047}, year = {2023}, volume = {12}, journal = {Foundations and Trends in Econometrics}, title = {A Simple Method for Predicting Covariance Matrices of Financial Returns}, doi = {10.1561/0800000047}, issn = {1551-3076}, number = {4}, pages = {324-407}, author = {Kasper Johansson and Mehmet G. Ogut and Markus Pelger and Thomas Schmelzer and Stephen Boyd} }

Owner

  • Name: Stanford University Convex Optimization Group
  • Login: cvxgrp
  • Kind: organization
  • Location: Stanford, CA

GitHub Events

Total
  • Create event: 55
  • Release event: 3
  • Issues event: 11
  • Watch event: 13
  • Delete event: 44
  • Issue comment event: 58
  • Push event: 162
  • Pull request event: 94
  • Fork event: 6
Last Year
  • Create event: 55
  • Release event: 3
  • Issues event: 11
  • Watch event: 13
  • Delete event: 44
  • Issue comment event: 58
  • Push event: 162
  • Pull request event: 94
  • Fork event: 6

Committers

Last synced: 11 months ago

All Time
  • Total Commits: 265
  • Total Committers: 7
  • Avg Commits per committer: 37.857
  • Development Distribution Score (DDS): 0.581
Past Year
  • Commits: 88
  • Committers: 5
  • Avg Commits per committer: 17.6
  • Development Distribution Score (DDS): 0.534
Top Committers
Name Email Commits
dependabot[bot] 4****] 111
Thomas Schmelzer t****r@g****m 111
kasperjo 7****o 21
Thomas Schmelzer t****r@a****e 14
renovate[bot] 2****] 4
pre-commit-ci[bot] 6****] 3
Stephen Boyd b****d@s****u 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 7 months ago

All Time
  • Total issues: 46
  • Total pull requests: 102
  • Average time to close issues: 3 days
  • Average time to close pull requests: about 21 hours
  • Total issue authors: 4
  • Total pull request authors: 7
  • Average comments per issue: 0.11
  • Average comments per pull request: 0.5
  • Merged pull requests: 80
  • Bot issues: 1
  • Bot pull requests: 31
Past Year
  • Issues: 5
  • Pull requests: 43
  • Average time to close issues: 4 days
  • Average time to close pull requests: about 23 hours
  • Issue authors: 2
  • Pull request authors: 5
  • Average comments per issue: 0.0
  • Average comments per pull request: 0.81
  • Merged pull requests: 29
  • Bot issues: 1
  • Bot pull requests: 25
Top Authors
Issue Authors
  • tschm (39)
  • kasperjo (16)
  • dependabot[bot] (2)
  • jingmouren (1)
  • renovate[bot] (1)
  • enzbus (1)
Pull Request Authors
  • dependabot[bot] (92)
  • tschm (66)
  • kasperjo (17)
  • renovate[bot] (9)
  • pre-commit-ci[bot] (4)
  • mojafa (2)
  • stephenpboyd (1)
Top Labels
Issue Labels
dependencies (2) python (1)
Pull Request Labels
dependencies (98) python (65) github_actions (7) pre-commit (6) renovate (3)

Dependencies

.github/workflows/ci.yml actions
  • actions/checkout v3 composite
  • cvxgrp/.github/actions/book main composite
  • cvxgrp/.github/actions/test main composite
.github/workflows/release.yml actions
  • actions/checkout v3 composite
  • cvxgrp/.github/actions/release main composite
.github/workflows/book.yml actions
  • cvxgrp/.github/actions/book main composite
  • cvxgrp/.github/actions/jupyter main composite
  • cvxgrp/.github/actions/sphinx main composite
  • cvxgrp/.github/actions/test main composite
.github/workflows/pre-commit.yml actions
  • actions/checkout v3 composite
  • coverallsapp/github-action v2 composite
  • cvxgrp/.github/actions/test main composite
  • pre-commit/action v3.0.0 composite