Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions deepdow/benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ def __call__(self, x):
x_rets = x[:, self.returns_channel, ...]
vols = x_rets.std(dim=1) if self.use_std else x_rets.var(dim=1)
ivols = 1 / (vols + eps)
weights = ivols / ivols.sum(dim=1, keepdim=True)

return weights
return ivols / ivols.sum(dim=1, keepdim=True)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function InverseVolatility.__call__ refactored with the following changes:


@property
def hparams(self):
Expand Down
10 changes: 8 additions & 2 deletions deepdow/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ def on_epoch_end(self, metadata):
epoch = metadata['epoch']
stats = self.run.history.metrics_per_epoch(epoch)

if not (len(stats['lookback'].unique()) == 1 and len(stats['model'].unique()) == 1):
if (
len(stats['lookback'].unique()) != 1
or len(stats['model'].unique()) != 1
):
Comment on lines -220 to +223

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function EarlyStoppingCallback.on_epoch_end refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

raise ValueError('EarlyStoppingCallback needs to have a single lookback and model') # pragma: no cover

stats_formatted = stats.groupby(['dataloader', 'metric'])['value'].mean().unstack(-1)
Expand Down Expand Up @@ -398,7 +401,10 @@ def on_epoch_end(self, metadata):
epoch = metadata['epoch']
stats = self.run.history.metrics_per_epoch(epoch)

if not (len(stats['lookback'].unique()) == 1 and len(stats['model'].unique()) == 1):
if (
len(stats['lookback'].unique()) != 1
or len(stats['model'].unique()) != 1
):
Comment on lines -401 to +407

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ModelCheckpointCallback.on_epoch_end refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

raise ValueError('ModelCheckpointCallback needs to have a single lookback and model') # pragma: no cover

stats_formatted = stats.groupby(['dataloader', 'metric'])['value'].mean().unstack(-1)
Expand Down
3 changes: 0 additions & 3 deletions deepdow/data/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,6 @@ def collate_uniform(batch, n_assets_range=(5, 10), lookback_range=(2, 20), horiz
if asset_ixs is None:
n_assets = torch.randint(low=n_assets_range[0], high=min(n_assets_max + 1, n_assets_range[1]), size=(1,))[0]
asset_ixs = torch.multinomial(torch.ones(n_assets_max), n_assets.item(), replacement=False)
else:
pass

Comment on lines -134 to -136

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function collate_uniform refactored with the following changes:

# sample lookback
lookback = torch.randint(low=lookback_range[0], high=min(lookback_max + 1, lookback_range[1]), size=(1,))[0]

Expand Down
14 changes: 6 additions & 8 deletions deepdow/experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ def pretty_print(self, epoch=None):
If epoch given, then a results only over this epoch. If epoch is `None` then print results over all epochs.

"""
if epoch is None:
df = self.metrics

else:
df = self.metrics_per_epoch(epoch)
df = self.metrics if epoch is None else self.metrics_per_epoch(epoch)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function History.pretty_print refactored with the following changes:

pd.options.display.float_format = '{:,.3f}'.format
print(df.groupby(['model', 'metric', 'epoch', 'dataloader'])['value'].mean().to_string())

Expand Down Expand Up @@ -172,7 +168,7 @@ def __init__(self, network, loss, train_dataloader, val_dataloaders=None, metric
pass

elif isinstance(metrics, dict):
if not all([isinstance(x, Loss) for x in metrics.values()]):
if not all(isinstance(x, Loss) for x in metrics.values()):
Comment on lines -175 to +171

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Run.__init__ refactored with the following changes:

raise TypeError('All values of metrics need to be Loss.')

if 'loss' in metrics:
Expand All @@ -190,7 +186,9 @@ def __init__(self, network, loss, train_dataloader, val_dataloaders=None, metric
pass

elif isinstance(val_dataloaders, dict):
if not all([isinstance(x, RigidDataLoader) for x in val_dataloaders.values()]):
if not all(
isinstance(x, RigidDataLoader) for x in val_dataloaders.values()
):
raise TypeError('All values of val_dataloaders need to be RigidDataLoader.')

self.val_dataloaders.update(val_dataloaders)
Expand All @@ -205,7 +203,7 @@ def __init__(self, network, loss, train_dataloader, val_dataloaders=None, metric
pass

elif isinstance(benchmarks, dict):
if not all([isinstance(x, Benchmark) for x in benchmarks.values()]):
if not all(isinstance(x, Benchmark) for x in benchmarks.values()):
raise TypeError('All values of benchmarks need to be a Benchmark.')

if 'main' in benchmarks:
Expand Down
8 changes: 2 additions & 6 deletions deepdow/layers/allocate.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ def forward(self, covmat, rets=None):

w_l.append(w_final)

res = torch.stack(w_l, dim=0)

return res
return torch.stack(w_l, dim=0)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function NCO.forward refactored with the following changes:



class NumericalMarkowitz(nn.Module):
Expand Down Expand Up @@ -475,9 +473,7 @@ def __init__(self, n_assets, temperature=1, max_weight=1):
x = cp.Parameter(n_assets)
w = cp.Variable(n_assets)
obj = cp.sum_squares(x - w)
cons = [cp.sum(w) == 1,
0. <= w,
w <= max_weight]
cons = [cp.sum(w) == 1, w >= 0., w <= max_weight]
Comment on lines -478 to +476

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SparsemaxAllocator.__init__ refactored with the following changes:

prob = cp.Problem(cp.Minimize(obj), cons)

self.layer = CvxpyLayer(prob, parameters=[x], variables=[w])
Expand Down
13 changes: 7 additions & 6 deletions deepdow/layers/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ def forward(self, covmat):
stds = torch.sqrt(torch.diagonal(covmat, dim1=1, dim2=2))
stds_ = stds.view(n_samples, n_assets, 1)

corr = covmat / torch.matmul(stds_, stds_.permute(0, 2, 1))

return corr
return covmat / torch.matmul(stds_, stds_.permute(0, 2, 1))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Cov2Corr.forward refactored with the following changes:



class CovarianceMatrix(nn.Module):
Expand All @@ -53,9 +51,12 @@ def __init__(self, sqrt=True, shrinkage_strategy='diagonal', shrinkage_coef=0.5)

self.sqrt = sqrt

if shrinkage_strategy is not None:
if shrinkage_strategy not in {'diagonal', 'identity', 'scaled_identity'}:
raise ValueError('Unrecognized shrinkage strategy {}'.format(shrinkage_strategy))
if shrinkage_strategy is not None and shrinkage_strategy not in {
'diagonal',
'identity',
'scaled_identity',
}:
raise ValueError('Unrecognized shrinkage strategy {}'.format(shrinkage_strategy))
Comment on lines -56 to +59

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function CovarianceMatrix.__init__ refactored with the following changes:


self.shrinkage_strategy = shrinkage_strategy
self.shrinkage_coef = shrinkage_coef
Expand Down
8 changes: 2 additions & 6 deletions deepdow/layers/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,13 @@ def forward(self, x, tform):

grid = torch.stack([tx, ty], dim=-1)

x_warped = nn.functional.grid_sample(x,
return nn.functional.grid_sample(x,
grid,
mode=self.mode,
padding_mode=self.padding_mode,
align_corners=True,
)

return x_warped
Comment on lines -174 to -181

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Warp.forward refactored with the following changes:



class Zoom(torch.nn.Module):
"""Zoom in and out.
Expand Down Expand Up @@ -234,11 +232,9 @@ def forward(self, x, scale):
theta = theta.to(device=x.device, dtype=x.dtype)

grid = nn.functional.affine_grid(theta, x.shape, align_corners=True)
x_zoomed = nn.functional.grid_sample(x,
return nn.functional.grid_sample(x,
grid,
mode=self.mode,
padding_mode=self.padding_mode,
align_corners=True,
)

return x_zoomed
Comment on lines -237 to -244

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Zoom.forward refactored with the following changes:

20 changes: 9 additions & 11 deletions deepdow/losses.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ def covariance(x, y):
xm = x - mean_x # (n_samples, horizon)
ym = y - mean_y # (n_samples, horizon)

cov = (xm * ym).sum(dim=1) / horizon

return cov
return (xm * ym).sum(dim=1) / horizon

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function covariance refactored with the following changes:



def log2simple(x):
Expand Down Expand Up @@ -353,16 +351,16 @@ def __pow__(self, power):
new : Loss
Instance of a ``Loss`` representing the `self ** power`.
"""
if isinstance(power, (int, float)):
new_instance = Loss()
new_instance._call = MethodType(lambda inst, weights, y: self(weights, y) ** power, new_instance)
new_instance._repr = MethodType(lambda inst: '({}) ** {}'.format(self.__repr__(), power),
new_instance)

return new_instance
else:
if not isinstance(power, (int, float)):
raise TypeError('Unsupported type: {}'.format(type(power)))

new_instance = Loss()
new_instance._call = MethodType(lambda inst, weights, y: self(weights, y) ** power, new_instance)
new_instance._repr = MethodType(lambda inst: '({}) ** {}'.format(self.__repr__(), power),
new_instance)

return new_instance
Comment on lines -356 to +362

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Loss.__pow__ refactored with the following changes:



class Alpha(Loss):
"""Negative alpha with respect to a selected portfolio.
Expand Down
19 changes: 6 additions & 13 deletions deepdow/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,7 @@ def forward(self, x):
gamma_sqrt_all = torch.ones(len(x)).to(device=x.device, dtype=x.dtype) * self.gamma_sqrt
alpha_all = torch.ones(len(x)).to(device=x.device, dtype=x.dtype) * self.alpha

# weights
weights = self.portfolio_opt_layer(exp_rets, covmat, gamma_sqrt_all, alpha_all)

return weights
return self.portfolio_opt_layer(exp_rets, covmat, gamma_sqrt_all, alpha_all)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BachelierNet.forward refactored with the following changes:

This removes the following comments ( why? ):

# weights


@property
def hparams(self):
Expand Down Expand Up @@ -262,9 +259,7 @@ def __call__(self, x):

temperatures = torch.ones(n_samples).to(device=x.device, dtype=x.dtype) * self.temperature

weights = self.portfolio_opt_layer(x, temperatures)

return weights
return self.portfolio_opt_layer(x, temperatures)
Comment on lines -265 to +262

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function KeynesNet.__call__ refactored with the following changes:


@property
def hparams(self):
Expand Down Expand Up @@ -352,9 +347,7 @@ def forward(self, x):
x = self.linear(x)

temperatures = torch.ones(n_samples).to(device=x.device, dtype=x.dtype) * self.temperature
weights = self.allocate_layer(x, temperatures)

return weights
return self.allocate_layer(x, temperatures)
Comment on lines -355 to +350

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function LinearNet.forward refactored with the following changes:


@property
def hparams(self):
Expand Down Expand Up @@ -474,9 +467,9 @@ def forward(self, x):
gamma_all = torch.ones(len(x)).to(device=x.device, dtype=x.dtype) * self.gamma_sqrt
alpha_all = torch.ones(len(x)).to(device=x.device, dtype=x.dtype) * self.alpha

weights = self.portfolio_opt_layer(exp_returns_all, covariance_all, gamma_all, alpha_all)

return weights
return self.portfolio_opt_layer(
exp_returns_all, covariance_all, gamma_all, alpha_all
)
Comment on lines -477 to +472

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ThorpNet.forward refactored with the following changes:


@property
def hparams(self):
Expand Down
2 changes: 1 addition & 1 deletion deepdow/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def check_indices_agree(*frames):
If indices/colums do not agree.

"""
if not all([isinstance(x, (pd.Series, pd.DataFrame)) for x in frames]):
if not all(isinstance(x, (pd.Series, pd.DataFrame)) for x in frames):

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function PandasChecks.check_indices_agree refactored with the following changes:

raise TypeError('Some elements are not pd.Series or pd.DataFrame')

reference_index = frames[0].index
Expand Down
11 changes: 5 additions & 6 deletions deepdow/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ def generate_metrics_table(benchmarks, dataloader, metrics, device=None, dtype=N
'metric': metric_name,
'value': metric_per_s}))

metrics_table = pd.concat(all_entries)

return metrics_table
return pd.concat(all_entries)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function generate_metrics_table refactored with the following changes:



def generate_cumrets(benchmarks, dataloader, device=None, dtype=None, returns_channel=0,
Expand Down Expand Up @@ -142,9 +140,10 @@ def generate_cumrets(benchmarks, dataloader, device=None, dtype=None, returns_ch
all_entries[bm_name].append(pd.DataFrame(cumrets.detach().cpu().numpy(),
index=timestamps))

cumrets_dict = {bm_name: pd.concat(entries).sort_index() for bm_name, entries in all_entries.items()}

return cumrets_dict
return {
bm_name: pd.concat(entries).sort_index()
for bm_name, entries in all_entries.items()
}
Comment on lines -145 to +146

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function generate_cumrets refactored with the following changes:



def plot_metrics(metrics_table):
Expand Down
4 changes: 1 addition & 3 deletions examples/end_to_end/getting_started.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ def forward(self, x):
x = self.dense_layer(x)

temperatures = torch.ones(n_samples).to(device=x.device, dtype=x.dtype) * self.temperature
weights = self.allocate_layer(x, temperatures)

return weights
return self.allocate_layer(x, temperatures)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GreatNet.forward refactored with the following changes:



# %%
Expand Down
4 changes: 1 addition & 3 deletions examples/end_to_end/iid.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,7 @@ def forward(self, x):
cov_sqrt[None, ...],
self.gamma_sqrt,
torch.zeros(1).to(device=x.device, dtype=x.dtype))
weights_filled = torch.repeat_interleave(weights, n, dim=0)

return weights_filled
return torch.repeat_interleave(weights, n, dim=0)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Net.forward refactored with the following changes:



# %%
Expand Down
22 changes: 18 additions & 4 deletions tests/test_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,12 +481,26 @@ def test_n_parameters(self, n_channels, hidden_size, cell_type, bidirectional):
hidden_size_a = int(hidden_size // n_dir)

if cell_type == 'RNN':
assert n_parameters == n_dir * (
(n_channels * hidden_size_a) + (hidden_size_a * hidden_size_a) + 2 * hidden_size_a)
assert n_parameters == (
n_dir
* (
n_channels * hidden_size_a
+ hidden_size_a ** 2
+ 2 * hidden_size_a
)
)


else:
assert n_parameters == n_dir * 4 * (
(n_channels * hidden_size_a) + (hidden_size_a * hidden_size_a) + 2 * hidden_size_a)
assert n_parameters == (
n_dir
* 4
* (
n_channels * hidden_size_a
+ hidden_size_a ** 2
+ 2 * hidden_size_a
)
)
Comment on lines -484 to +503

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TestRNN.test_n_parameters refactored with the following changes:


def test_error(self):

Expand Down