deepdow.nn module¶
Module containing neural networks.
- class BachelierNet(n_input_channels, n_assets, hidden_size=32, max_weight=1, shrinkage_strategy='diagonal', p=0.5)[source]¶
Bases:
Module
,Benchmark
Combination of recurrent neural networks and convex optimization.
- Parameters:
n_input_channels (int) – Number of input channels of the dataset.
n_assets (int) – Number of assets in our dataset. Note that this network is shuffle invariant along this dimension.
hidden_size (int) – Hidden state size. Alternatively one can see it as number of output channels.
max_weight (float) – Maximum weight for a single asset.
shrinkage_strategy (str, {'diagonal', 'identity', 'scaled_identity'}) – Strategy of estimating the covariance matrix.
p (float) – Dropout rate - probability of an element to be zeroed during dropout.
- norm_layer¶
Instance normalization (per channel).
- Type:
torch.nn.Module
- transform_layer¶
RNN layer that transforms (n_samples, n_channels, lookback, n_assets) to (n_samples, hidden_size, lookback, n_assets) where the first (sample) and the last dimension (assets) is shuffle invariant.
- Type:
deepdow.layers.RNN
- time_collapse_layer¶
Attention pooling layer that turns (n_samples, hidden_size, lookback, n_assets) into (n_samples, hidden_size, n_assets) by assigning each timestep in the lookback dimension a weight and then performing a weighted average.
- Type:
deepdow.layers.AttentionCollapse
- dropout_layer¶
Dropout layer where the probability is controled by the parameter p.
- Type:
torch.nn.Module
- covariance_layer¶
Estimate square root of a covariance metric for the optimization. Turns (n_samples, lookback, n_assets) to (n_samples, n_assets, n_assets).
- Type:
deepdow.layers.CovarianceMatrix
- channel_collapse_layer¶
Averaging layer turning (n_samples, hidden_size, n_assets) to (n_samples, n_assets) where the output serves as estimate of expected returns in the optimization.
- Type:
deepdow.layers.AverageCollapse
- gamma¶
A single learnable parameter that will be used for all samples. It represents the tradoff between risk and return. If equal to zero only expected returns are considered.
- Type:
torch.nn.Parameter
- alpha¶
A single learnable parameter that will be used for all samples. It represents the regularization strength of portfolio weights. If zero then no effect if high then encourages weights to be closer to zero.
- Type:
torch.nn.Parameter
- portfolio_opt_layer¶
Markowitz optimizer that inputs expected returns, square root of a covariance matrix and a gamma
- Type:
deepdow.layers.NumericalMarkowitz
- forward(x)[source]¶
Perform forward pass.
- Parameters:
x (torch.Tensor) – Of shape (n_samples, n_channels, lookback, n_assets).
- Returns:
weights – Tensor of shape (n_samples, n_assets).
- Return type:
torch.Torch
- property hparams¶
Hyperparamters relevant to construction of the model.
- training: bool¶
- class DummyNet(n_channels=1)[source]¶
Bases:
Module
,Benchmark
Minimal trainable network achieving the task.
- Parameters:
n_channels (int) – Number of input channels. We learn one constant per channel. Therefore n_channels=n_trainable_parameters.
- forward(x)[source]¶
Perform forward pass.
- Parameters:
x (torch.Tensor) – Of shape (n_samples, n_channels, lookback, n_assets).
- Returns:
weights – Tensor of shape (n_samples, n_assets).
- Return type:
torch.Torch
- property hparams¶
Hyperparamters relevant to construction of the model.
- training: bool¶
- class KeynesNet(n_input_channels, hidden_size=32, transform_type='RNN', n_groups=4)[source]¶
Bases:
Module
,Benchmark
Connection of multiple different modules.
- Parameters:
n_input_channels (int) – Number of input channels.
hidden_size (int) – Number of features the transform layer will create.
transform_type (str, {'RNN', 'Conv'}) – If ‘RNN’ then one directional LSTM that is shared across all assets. If Conv then 1D convolution that is shared among all assets.
n_groups (int) – Number of groups to split the hidden_size channels into. This is used in the Group Normalization. Note that hidden_size % n_groups == 0 needs to hold.
- norm_layer_1¶
Instance normalization layer with learnable parameters (2 per channel). Applied to the input.
- Type:
torch.nn.InstanceNorm2d
- transform_layer¶
Depends on the transform_type. The goal is two exctract features from the input tensor by considering the time dimension.
- Type:
torch.nn.Module
- norm_layer_2¶
Group normalization with n_groups groups. It is applied to the features extracted by time_collapse_layer.
- Type:
torch.nn.GroupNorm
- time_collapse_layer, channel_collapse_layer
Removing of respective dimensions by the means of averaging.
- Type:
deepdow.layers.AverageCollapse
- temperature¶
Learnable parameter representing the temperature for the softmax applied to all inputs.
- Type:
torch.Tensor
- portfolio_opt_layer¶
Portfolio allocation layer. Uses learned temperature.
- Type:
deepdow.layers.SoftmaxAllocator
- __call__(x)[source]¶
Perform forward pass.
- Parameters:
x (torch.Tensor) – Of shape (n_samples, n_channels, lookback, n_assets).
- Returns:
weights – Tensor of shape (n_samples, n_assets).
- Return type:
torch.Torch
- property hparams¶
Hyperparameters relevant to construction of the model.
- training: bool¶
- class LinearNet(n_channels, lookback, n_assets, p=0.5)[source]¶
Bases:
Module
,Benchmark
Network with one layer.
- Parameters:
n_channels (int) – Number of channels, needs to be fixed for each input tensor.
lookback (int) – Lookback, needs to be fixed for each input tensor.
n_assets (int) – Number of assets, needs to be fixed for each input tensor.
p (float) – Dropout probability.
- norm_layer¶
Batch normalization with learnable parameters.
- Type:
torch.nn.BatchNorm1d
- dropout_layer¶
Dropout layer with probability p.
- Type:
torch.nn.Dropout
- linear¶
One dense layer with n_assets outputs and the flattened input tensor (n_channels, lookback, n_assets).
- Type:
torch.nn.Linear
- temperature¶
Learnable parameter for representing the final softmax allocator temperature.
- Type:
torch.Parameter
- allocate_layer¶
Softmax allocator with a per sample temperature.
- Type:
- forward(x)[source]¶
Perform forward pass.
- Parameters:
x (torch.Tensor) – Of shape (n_samples, n_channels, lookback, n_assets). The last 3 dimensions need to be of the same size as specified in the constructor. They cannot vary.
- Returns:
weights – Tensor of shape (n_samples, n_assets).
- Return type:
torch.Torch
- property hparams¶
Hyperparameters relevant to construction of the model.
- training: bool¶
- class MinimalNet(n_assets)[source]¶
Bases:
Module
,Benchmark
Minimal network that learns per asset weights.
- Parameters:
n_assets (int) – Number of assets.
- allocate_layer¶
Layer whose goal is to learn each weight and make sure they sum up to one via normalization.
- Type:
deepdow.allocate.WeightNorm
- forward(x)[source]¶
Perform forward pass.
- Parameters:
x (torch.Tensor) – Tensor of shape (n_samples, dim_1, …., dim_N).
- Returns:
weights – Tensor of shape (n_samples, n_assets).
- Return type:
torch.Tensor
- property hparams¶
Hyperparameters relevant to construction of the model.
- training: bool¶
- class ThorpNet(n_assets, max_weight=1, force_symmetric=True)[source]¶
Bases:
Module
,Benchmark
All inputs of convex optimization are learnable but do not depend on the input.
- Parameters:
n_assets (int) – Number of assets in our dataset. Note that this network is shuffle invariant along this dimension.
force_symmetric (bool) – If True, then the square root of the covariance matrix will be always by construction symmetric. The resulting array will be \(M^T M\) where \(M\) is the learnable parameter. If False then no guarantee of the matrix being symmetric.
max_weight (float) – Maximum weight for a single asset.
- matrix¶
A learnable matrix of shape (n_assets, n_assets).
- Type:
torch.nn.Parameter
- exp_returns¶
A learnable vector of shape (n_assets,).
- Type:
torch.nn.Parameter
- gamma_sqrt¶
A single learnable parameter that will be used for all samples. It represents the tradoff between risk and return. If equal to zero only expected returns are considered.
- Type:
torch.nn.Parameter
- alpha¶
A single learnable parameter that will be used for all samples. It represents the regularization strength of portfolio weights. If zero then no effect if high then encourages weights to be closer to zero.
- Type:
torch.nn.Parameter
- forward(x)[source]¶
Perform forward pass.
- Parameters:
x (torch.Tensor) – Of shape (n_samples, n_channels, lookback, n_assets).
- Returns:
weights – Tensor of shape (n_samples, n_assets).
- Return type:
torch.Torch
- property hparams¶
Hyperparameters relevant to construction of the model.
- training: bool¶