Skip to content

Commit

Permalink
add pre-commit (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
tschm authored Jun 2, 2023
1 parent af5d2b4 commit 29e479f
Show file tree
Hide file tree
Showing 24 changed files with 248 additions and 149 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: pre-commit

on:
pull_request:
push:

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
- uses: pre-commit/action@v3.0.0
19 changes: 19 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-toml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: fix-encoding-pragma

- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black

- repo: https://github.com/asottile/reorder_python_imports
rev: v3.9.0
hooks:
- id: reorder-python-imports
args: [--py37-plus, --add-import, 'from __future__ import annotations']
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ 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 combinator
# Define the covariance combinator
combinator = from_ewmas(returns,
halflife_pairs,
min_periods_vola=n, # min periods for volatility estimation
min_periods_cov=3 * n) # min periods for correlation estimation (must be at least n)

# Solve combination problem and loop through combination results to get predictors
# Solve combination problem and loop through combination results to get predictors
covariance_predictors = {}
for predictor in combinator.solve(window=10): # lookback window in convex optimization problem
# From predictor we can access predictor.time, predictor.mean (=0 here), predictor.covariance, and predictor.weights
Expand Down Expand Up @@ -83,7 +83,7 @@ expert2 = {time: ewma63.loc[time] for time in ewma63.index.get_level_values(0).u
# Create expert dictionary
experts = {1: expert1, 2: expert2}

# Define the covariance combinator
# Define the covariance combinator
combinator = from_sigmas(sigmas=experts, returns=returns)

# Solve combination problem and loop through combination results to get predictors
Expand Down Expand Up @@ -117,5 +117,3 @@ environment. Executing
constructs a dedicated
[Kernel](https://docs.jupyter.org/en/latest/projects/kernels.html) for the
project.


4 changes: 2 additions & 2 deletions book/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ sphinx:
config:
html_js_files:
- https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js


# Information about where the book exists on the web
repository:
url: https://github.com/cvxgrp/cov_pred_finance
Expand Down
2 changes: 1 addition & 1 deletion book/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

## Dependencies

{{ '[Dependencies]({url}/artifacts/build/show.txt)'.format(url=book_url) }}
{{ '[Dependencies]({url}/artifacts/build/show.txt)'.format(url=book_url) }}
6 changes: 3 additions & 3 deletions book/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ 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 combinator
# Define the covariance combinator
combinator = from_ewmas(returns,
halflife_pairs,
min_periods_vola=n, # min periods for volatility estimation
min_periods_cov=3 * n) # min periods for correlation estimation (must be at least n)

# Solve combination problem and loop through combination results to get predictors
# Solve combination problem and loop through combination results to get predictors
covariance_predictors = {}
for predictor in combinator.solve(window=10): # lookback window in convex optimization problem
# From predictor we can access predictor.time, predictor.mean (=0 here), predictor.covariance, and predictor.weights
Expand Down Expand Up @@ -78,7 +78,7 @@ expert2 = {time: ewma63.loc[time] for time in ewma63.index.get_level_values(0).u
# Create expert dictionary
experts = {1: expert1, 2: expert2}

# Define the covariance combinator
# Define the covariance combinator
combinator = from_sigmas(sigmas=experts, returns=returns)

# Solve combination problem and loop through combination results to get predictors
Expand Down
2 changes: 1 addition & 1 deletion book/docs/reports.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

## Test report

{{ '[Report]({url}/artifacts/tests/html-report/report.html)'.format(url=book_url) }}
{{ '[Report]({url}/artifacts/tests/html-report/report.html)'.format(url=book_url) }}
17 changes: 8 additions & 9 deletions cvx/covariance/combination.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import annotations

import warnings
from collections import namedtuple

import cvxpy as cvx
import numpy as np
import pandas as pd
import warnings

from cvx.covariance.ewma import iterated_ewma

# Mute specific warning
warnings.filterwarnings("ignore", message="Solution may be inaccurate.*")


def _map_nested_dicts(ob, func):
"""
Recursively applies a function to a nested dictionary
Expand Down Expand Up @@ -100,12 +105,7 @@ def weights(self):


def from_ewmas(
returns,
pairs,
min_periods_vola=20,
min_periods_cov=20,
clip_at=None,
mean=False
returns, pairs, min_periods_vola=20, min_periods_cov=20, clip_at=None, mean=False
):
"""
Estimate a series of covariance matrices using the iterated EWMA method
Expand Down Expand Up @@ -144,6 +144,7 @@ def from_ewmas(
# combination of covariance matrix valued time series
return _CovarianceCombination(sigmas=sigmas, returns=returns, means=means)


def from_sigmas(sigmas, returns, means=None):
return _CovarianceCombination(sigmas=sigmas, returns=returns, means=means)

Expand Down Expand Up @@ -198,7 +199,6 @@ def means(self):
def returns(self):
return self.__returns


@property
def K(self):
"""
Expand Down Expand Up @@ -282,4 +282,3 @@ def _solve(self, time, problem, **kwargs):
index=self.assets, columns=self.assets, data=np.linalg.inv(L @ L.T)
)
return Result(time=time, mean=mean, covariance=sigma, weights=weights)

5 changes: 4 additions & 1 deletion cvx/covariance/ewma.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# -*- coding: utf-8 -*-
from __future__ import annotations

from collections import namedtuple
from typing import Union

import numpy as np
import pandas as pd
from pandas._typing import TimedeltaConvertibleTypes
from typing import Union

IEWMA = namedtuple("IEWMA", ["time", "mean", "covariance", "volatility"])

Expand Down
5 changes: 4 additions & 1 deletion cvx/covariance/regularization.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# -*- coding: utf-8 -*-
from __future__ import annotations

from collections import namedtuple

import pandas as pd
import numpy as np
import pandas as pd
import scipy as sc

LowRank = namedtuple("LowRank", ["Loading", "Cov", "D", "Approximation"])
Expand Down
2 changes: 1 addition & 1 deletion experiments/data/lt_reversal.csv
Original file line number Diff line number Diff line change
Expand Up @@ -24335,4 +24335,4 @@
20230223, -0.07
20230224, 0.24
20230227, -0.11
20230228, -0.05
20230228, -0.05
4 changes: 2 additions & 2 deletions experiments/data/momentum.CSV
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
,Mom
,Mom
19261103, 0.56
19261104, -0.50
19261105, 1.17
Expand Down Expand Up @@ -25335,4 +25335,4 @@
20230223, 0.06
20230224, 1.21
20230227, -0.24
20230228, -0.41
20230228, -0.41
2 changes: 1 addition & 1 deletion experiments/data/st_reversal.csv
Original file line number Diff line number Diff line change
Expand Up @@ -25565,4 +25565,4 @@
20230223, -0.47
20230224, -0.10
20230227, -0.09
20230228, 0.17
20230228, 0.17
3 changes: 3 additions & 0 deletions experiments/utils/experiment_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import annotations

import numpy as np
import pandas as pd

Expand Down
Loading

0 comments on commit 29e479f

Please sign in to comment.