Skip to content

Commit

Permalink
Add black to linting (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
jankrepl committed Aug 2, 2022
1 parent 4ff9d7f commit 341b34d
Show file tree
Hide file tree
Showing 32 changed files with 3,462 additions and 1,582 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ jobs:
- name: Lint
run: |
pip install 'flake8==5.0.3' 'pydocstyle==6.1.1'
pip install 'black==22.6.0' 'flake8==5.0.3' 'pydocstyle==6.1.1'
black -l 79 --check deepdow/ tests
flake8 deepdow tests
pydocstyle deepdow
Expand Down
2 changes: 1 addition & 1 deletion deepdow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
X.Y.Z for bug fixes
"""

__version__ = '0.2.2'
__version__ = "0.2.2"
87 changes: 61 additions & 26 deletions deepdow/benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ def __call__(self, x):
@property
def hparams(self):
"""Hyperparamters relevant to construction of the model."""
return {'use_std': self.use_std,
'returns_channel': self.returns_channel}
return {
"use_std": self.use_std,
"returns_channel": self.returns_channel,
}


class MaximumReturn(Benchmark):
Expand Down Expand Up @@ -103,7 +105,11 @@ def __init__(self, max_weight=1, n_assets=None, returns_channel=0):
self.n_assets = n_assets
self.returns_channel = returns_channel

self.optlayer = self._construct_problem(n_assets, max_weight) if self.n_assets is not None else None
self.optlayer = (
self._construct_problem(n_assets, max_weight)
if self.n_assets is not None
else None
)

@staticmethod
def _construct_problem(n_assets, max_weight):
Expand All @@ -112,9 +118,9 @@ def _construct_problem(n_assets, max_weight):
w = cp.Variable(n_assets)

ret = rets @ w
prob = cp.Problem(cp.Maximize(ret), [cp.sum(w) == 1,
w >= 0,
w <= max_weight])
prob = cp.Problem(
cp.Maximize(ret), [cp.sum(w) == 1, w >= 0, w <= max_weight]
)

return CvxpyLayer(prob, parameters=[rets], variables=[w])

Expand All @@ -137,22 +143,30 @@ def __call__(self, x):
# Problem setup
if self.optlayer is not None:
if self.n_assets != n_assets:
raise ValueError('Incorrect number of assets: {}, expected: {}'.format(n_assets, self.n_assets))
raise ValueError(
"Incorrect number of assets: {}, expected: {}".format(
n_assets, self.n_assets
)
)

optlayer = self.optlayer
else:
optlayer = self._construct_problem(n_assets, self.max_weight)

rets_estimate = x[:, self.returns_channel, :, :].mean(dim=1) # (n_samples, n_assets)
rets_estimate = x[:, self.returns_channel, :, :].mean(
dim=1
) # (n_samples, n_assets)

return optlayer(rets_estimate)[0]

@property
def hparams(self):
"""Hyperparamters relevant to construction of the model."""
return {'max_weight': self.max_weight,
'returns_channel': self.returns_channel,
'n_assets': self.n_assets}
return {
"max_weight": self.max_weight,
"returns_channel": self.returns_channel,
"n_assets": self.n_assets,
}


class MinimumVariance(Benchmark):
Expand Down Expand Up @@ -186,7 +200,11 @@ def __init__(self, max_weight=1, returns_channel=0, n_assets=None):
self.returns_channel = returns_channel
self.max_weight = max_weight

self.optlayer = self._construct_problem(n_assets, max_weight) if self.n_assets is not None else None
self.optlayer = (
self._construct_problem(n_assets, max_weight)
if self.n_assets is not None
else None
)

@staticmethod
def _construct_problem(n_assets, max_weight):
Expand All @@ -195,9 +213,9 @@ def _construct_problem(n_assets, max_weight):
w = cp.Variable(n_assets)

risk = cp.sum_squares(covmat_sqrt @ w)
prob = cp.Problem(cp.Minimize(risk), [cp.sum(w) == 1,
w >= 0,
w <= max_weight])
prob = cp.Problem(
cp.Minimize(risk), [cp.sum(w) == 1, w >= 0, w <= max_weight]
)

return CvxpyLayer(prob, parameters=[covmat_sqrt], variables=[w])

Expand All @@ -220,23 +238,31 @@ def __call__(self, x):
# Problem setup
if self.optlayer is not None:
if self.n_assets != n_assets:
raise ValueError('Incorrect number of assets: {}, expected: {}'.format(n_assets, self.n_assets))
raise ValueError(
"Incorrect number of assets: {}, expected: {}".format(
n_assets, self.n_assets
)
)

optlayer = self.optlayer
else:
optlayer = self._construct_problem(n_assets, self.max_weight)

# problem solver
covmat_sqrt_estimates = CovarianceMatrix(sqrt=True)(x[:, self.returns_channel, :, :])
covmat_sqrt_estimates = CovarianceMatrix(sqrt=True)(
x[:, self.returns_channel, :, :]
)

return optlayer(covmat_sqrt_estimates)[0]

@property
def hparams(self):
"""Hyperparamters relevant to construction of the model."""
return {'max_weight': self.max_weight,
'returns_channel': self.returns_channel,
'n_assets': self.n_assets}
return {
"max_weight": self.max_weight,
"returns_channel": self.returns_channel,
"n_assets": self.n_assets,
}


class OneOverN(Benchmark):
Expand All @@ -258,7 +284,10 @@ def __call__(self, x):
"""
n_samples, n_channels, lookback, n_assets = x.shape

return torch.ones((n_samples, n_assets), dtype=x.dtype, device=x.device) / n_assets
return (
torch.ones((n_samples, n_assets), dtype=x.dtype, device=x.device)
/ n_assets
)


class Random(Benchmark):
Expand All @@ -280,8 +309,12 @@ def __call__(self, x):
"""
n_samples, n_channels, lookback, n_assets = x.shape

weights_unscaled = torch.rand((n_samples, n_assets), dtype=x.dtype, device=x.device)
weights_sums = weights_unscaled.sum(dim=1, keepdim=True).repeat(1, n_assets)
weights_unscaled = torch.rand(
(n_samples, n_assets), dtype=x.dtype, device=x.device
)
weights_sums = weights_unscaled.sum(dim=1, keepdim=True).repeat(
1, n_assets
)

return weights_unscaled / weights_sums

Expand Down Expand Up @@ -316,14 +349,16 @@ def __call__(self, x):
n_samples, n_channels, lookback, n_assets = x.shape

if self.asset_ix not in set(range(n_assets)):
raise IndexError('The selected asset index is out of range.')
raise IndexError("The selected asset index is out of range.")

weights = torch.zeros((n_samples, n_assets), dtype=x.dtype, device=x.device)
weights = torch.zeros(
(n_samples, n_assets), dtype=x.dtype, device=x.device
)
weights[:, self.asset_ix] = 1

return weights

@property
def hparams(self):
"""Hyperparamters relevant to construction of the model."""
return {'asset_ix': self.asset_ix}
return {"asset_ix": self.asset_ix}

0 comments on commit 341b34d

Please sign in to comment.