-
Notifications
You must be signed in to change notification settings - Fork 1
Sourcery Starbot ⭐ refactored rodrigosnader/deepdow #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| raise ValueError('EarlyStoppingCallback needs to have a single lookback and model') # pragma: no cover | ||
|
|
||
| stats_formatted = stats.groupby(['dataloader', 'metric'])['value'].mean().unstack(-1) | ||
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| raise ValueError('ModelCheckpointCallback needs to have a single lookback and model') # pragma: no cover | ||
|
|
||
| stats_formatted = stats.groupby(['dataloader', 'metric'])['value'].mean().unstack(-1) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| # sample lookback | ||
| lookback = torch.randint(low=lookback_range[0], high=min(lookback_max + 1, lookback_range[1]), size=(1,))[0] | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| pd.options.display.float_format = '{:,.3f}'.format | ||
| print(df.groupby(['model', 'metric', 'epoch', 'dataloader'])['value'].mean().to_string()) | ||
|
|
||
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| raise TypeError('All values of metrics need to be Loss.') | ||
|
|
||
| if 'loss' in metrics: | ||
|
|
@@ -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) | ||
|
|
@@ -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: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| class NumericalMarkowitz(nn.Module): | ||
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| prob = cp.Problem(cp.Minimize(obj), cons) | ||
|
|
||
| self.layer = CvxpyLayer(prob, parameters=[x], variables=[w]) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| class CovarianceMatrix(nn.Module): | ||
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| self.shrinkage_strategy = shrinkage_strategy | ||
| self.shrinkage_coef = shrinkage_coef | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| class Zoom(torch.nn.Module): | ||
| """Zoom in and out. | ||
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| def log2simple(x): | ||
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| class Alpha(Loss): | ||
| """Negative alpha with respect to a selected portfolio. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
|
|
||
| @property | ||
| def hparams(self): | ||
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| @property | ||
| def hparams(self): | ||
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| @property | ||
| def hparams(self): | ||
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| @property | ||
| def hparams(self): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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): | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| raise TypeError('Some elements are not pd.Series or pd.DataFrame') | ||
|
|
||
| reference_index = frames[0].index | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| def generate_cumrets(benchmarks, dataloader, device=None, dtype=None, returns_channel=0, | ||
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| def plot_metrics(metrics_table): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| # %% | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| # %% | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def test_error(self): | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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:inline-immediately-returned-variable)