Skip to content

Commit 29e479f

Browse files
authored
add pre-commit (#50)
1 parent af5d2b4 commit 29e479f

24 files changed

+248
-149
lines changed

.github/workflows/pre-commit.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: pre-commit
2+
3+
on:
4+
pull_request:
5+
push:
6+
7+
jobs:
8+
pre-commit:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v3
12+
- uses: actions/setup-python@v3
13+
- uses: pre-commit/action@v3.0.0

.pre-commit-config.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v2.3.0
4+
hooks:
5+
- id: check-toml
6+
- id: end-of-file-fixer
7+
- id: trailing-whitespace
8+
- id: fix-encoding-pragma
9+
10+
- repo: https://github.com/psf/black
11+
rev: 23.1.0
12+
hooks:
13+
- id: black
14+
15+
- repo: https://github.com/asottile/reorder_python_imports
16+
rev: v3.9.0
17+
hooks:
18+
- id: reorder-python-imports
19+
args: [--py37-plus, --add-import, 'from __future__ import annotations']

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ n = returns.shape[1]
4646
# Define half-life pairs for K=3 experts, (halflife_vola, halflife_cov)
4747
halflife_pairs = [(10, 21), (21, 63), (63, 125)]
4848

49-
# Define the covariance combinator
49+
# Define the covariance combinator
5050
combinator = from_ewmas(returns,
5151
halflife_pairs,
5252
min_periods_vola=n, # min periods for volatility estimation
5353
min_periods_cov=3 * n) # min periods for correlation estimation (must be at least n)
5454

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

86-
# Define the covariance combinator
86+
# Define the covariance combinator
8787
combinator = from_sigmas(sigmas=experts, returns=returns)
8888

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

book/_config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ sphinx:
2323
config:
2424
html_js_files:
2525
- https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js
26-
27-
26+
27+
2828
# Information about where the book exists on the web
2929
repository:
3030
url: https://github.com/cvxgrp/cov_pred_finance

book/docs/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
## Dependencies
88

9-
{{ '[Dependencies]({url}/artifacts/build/show.txt)'.format(url=book_url) }}
9+
{{ '[Dependencies]({url}/artifacts/build/show.txt)'.format(url=book_url) }}

book/docs/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ n = returns.shape[1]
4141
# Define half-life pairs for K=3 experts, (halflife_vola, halflife_cov)
4242
halflife_pairs = [(10, 21), (21, 63), (63, 125)]
4343

44-
# Define the covariance combinator
44+
# Define the covariance combinator
4545
combinator = from_ewmas(returns,
4646
halflife_pairs,
4747
min_periods_vola=n, # min periods for volatility estimation
4848
min_periods_cov=3 * n) # min periods for correlation estimation (must be at least n)
4949

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

81-
# Define the covariance combinator
81+
# Define the covariance combinator
8282
combinator = from_sigmas(sigmas=experts, returns=returns)
8383

8484
# Solve combination problem and loop through combination results to get predictors

book/docs/reports.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
## Test report
88

9-
{{ '[Report]({url}/artifacts/tests/html-report/report.html)'.format(url=book_url) }}
9+
{{ '[Report]({url}/artifacts/tests/html-report/report.html)'.format(url=book_url) }}

cvx/covariance/combination.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import annotations
3+
4+
import warnings
15
from collections import namedtuple
6+
27
import cvxpy as cvx
38
import numpy as np
49
import pandas as pd
5-
import warnings
610

711
from cvx.covariance.ewma import iterated_ewma
812

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

16+
1217
def _map_nested_dicts(ob, func):
1318
"""
1419
Recursively applies a function to a nested dictionary
@@ -100,12 +105,7 @@ def weights(self):
100105

101106

102107
def from_ewmas(
103-
returns,
104-
pairs,
105-
min_periods_vola=20,
106-
min_periods_cov=20,
107-
clip_at=None,
108-
mean=False
108+
returns, pairs, min_periods_vola=20, min_periods_cov=20, clip_at=None, mean=False
109109
):
110110
"""
111111
Estimate a series of covariance matrices using the iterated EWMA method
@@ -144,6 +144,7 @@ def from_ewmas(
144144
# combination of covariance matrix valued time series
145145
return _CovarianceCombination(sigmas=sigmas, returns=returns, means=means)
146146

147+
147148
def from_sigmas(sigmas, returns, means=None):
148149
return _CovarianceCombination(sigmas=sigmas, returns=returns, means=means)
149150

@@ -198,7 +199,6 @@ def means(self):
198199
def returns(self):
199200
return self.__returns
200201

201-
202202
@property
203203
def K(self):
204204
"""
@@ -282,4 +282,3 @@ def _solve(self, time, problem, **kwargs):
282282
index=self.assets, columns=self.assets, data=np.linalg.inv(L @ L.T)
283283
)
284284
return Result(time=time, mean=mean, covariance=sigma, weights=weights)
285-

cvx/covariance/ewma.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import annotations
3+
14
from collections import namedtuple
5+
from typing import Union
26

37
import numpy as np
48
import pandas as pd
59
from pandas._typing import TimedeltaConvertibleTypes
6-
from typing import Union
710

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

cvx/covariance/regularization.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import annotations
3+
14
from collections import namedtuple
25

3-
import pandas as pd
46
import numpy as np
7+
import pandas as pd
58
import scipy as sc
69

710
LowRank = namedtuple("LowRank", ["Loading", "Cov", "D", "Approximation"])

experiments/data/lt_reversal.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24335,4 +24335,4 @@
2433524335
20230223, -0.07
2433624336
20230224, 0.24
2433724337
20230227, -0.11
24338-
20230228, -0.05
24338+
20230228, -0.05

experiments/data/momentum.CSV

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
,Mom
1+
,Mom
22
19261103, 0.56
33
19261104, -0.50
44
19261105, 1.17
@@ -25335,4 +25335,4 @@
2533525335
20230223, 0.06
2533625336
20230224, 1.21
2533725337
20230227, -0.24
25338-
20230228, -0.41
25338+
20230228, -0.41

experiments/data/st_reversal.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25565,4 +25565,4 @@
2556525565
20230223, -0.47
2556625566
20230224, -0.10
2556725567
20230227, -0.09
25568-
20230228, 0.17
25568+
20230228, 0.17

experiments/utils/experiment_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import annotations
3+
14
import numpy as np
25
import pandas as pd
36

0 commit comments

Comments
 (0)