deepdow.benchmarks module

Collection of benchmarks.

class Benchmark[source]

Bases: ABC

Abstract benchmark class.

The idea is to create some benchmarks that we can use for comparison to our neural networks. Note that we assume that benchmarks are not trainable - one can only use them for inference.

abstract __call__(x)[source]

Prediction of the model.

property hparams

Hyperparamters relevant to construction of the model.

class InverseVolatility(use_std=False, returns_channel=0)[source]

Bases: Benchmark

Allocation only considering volatility of individual assets.

Parameters:
  • use_std (bool) – If True, then we use standard deviation as a measure of volatility. Otherwise variance is used.

  • returns_channel (int) – Which channel in the x feature matrix to consider (the 2nd dimension) as returns.

__call__(x)[source]

Predict weights.

Parameters:

x (torch.Tensor) – Tensor of shape (n_samples, n_channels, lookback, n_assets).

Returns:

weights – Tensor of shape (n_samples, n_assets) representing the predicted weights.

Return type:

torch.Tensor

property hparams

Hyperparamters relevant to construction of the model.

class MaximumReturn(max_weight=1, n_assets=None, returns_channel=0)[source]

Bases: Benchmark

Markowitz portfolio optimization - maximum return.

Parameters:
  • max_weight (float) – A number in (0, 1] representing the maximum weight per asset.

  • n_assets (None or int) – If specifed the benchmark will always have to be provided with n_assets of assets in the __call__. This way one can achieve major speedups since the optimization problem is canonicalized only once in the constructor. However, when n_assets is None the optimization problem is canonicalized before each inside of __call__ which results in overhead but allows for variable number of assets.

  • returns_channel (int) – Which channel in the x feature matrix to consider (the 2nd dimension) as returns.

optlayer

Equal to None if n_assets not provided in the constructor. In this case optimization problem is constructed with each forward pass. This allows for variable number of assets but is slower. If n_assets provided than constructed once and for all in the constructor.

Type:

cvxpylayers.torch.CvxpyLayer or None

__call__(x)[source]

Predict weights.

Parameters:

x (torch.Tensor) – Tensor of shape (n_samples, n_channels, lookback, n_assets).

Returns:

weights – Tensor of shape (n_samples, n_assets) representing the predicted weights.

Return type:

torch.Tensor

property hparams

Hyperparamters relevant to construction of the model.

class MinimumVariance(max_weight=1, returns_channel=0, n_assets=None)[source]

Bases: Benchmark

Markowitz portfolio optimization - minimum variance.

Parameters:
  • max_weight (float) – A number in (0, 1] representing the maximum weight per asset.

  • n_assets (None or int) – If specifed the benchmark will always have to be provided with n_assets of assets in the __call__. This way one can achieve major speedups since the optimization problem is canonicalized only once in the constructor. However, when n_assets is None the optimization problem is canonicalized before each inside of __call__ which results in overhead but allows for variable number of assets.

  • returns_channel (int) – Which channel in the x feature matrix to consider (the 2nd dimension) as returns.

optlayer

Equal to None if n_assets not provided in the constructor. In this case optimization problem is constructed with each forward pass. This allows for variable number of assets but is slower. If n_assets provided than constructed once and for all in the constructor.

Type:

cvxpylayers.torch.CvxpyLayer or None

__call__(x)[source]

Predict weights.

Parameters:

x (torch.Tensor) – Tensor of shape (n_samples, n_channels, lookback, n_assets).

Returns:

weights – Tensor of shape (n_samples, n_assets) representing the predicted weights.

Return type:

torch.Tensor

property hparams

Hyperparamters relevant to construction of the model.

class OneOverN[source]

Bases: Benchmark

Equally weighted portfolio.

__call__(x)[source]

Predict weights.

Parameters:

x (torch.Tensor) – Tensor of shape (n_samples, n_channels, lookback, n_assets).

Returns:

weights – Tensor of shape (n_samples, n_assets) representing the predicted weights.

Return type:

torch.Tensor

class Random[source]

Bases: Benchmark

Random allocation for each prediction.

__call__(x)[source]

Predict weights.

Parameters:

x (torch.Tensor) – Tensor of shape (n_samples, n_channels, lookback, n_assets).

Returns:

weights – Tensor of shape (n_samples, n_assets) representing the predicted weights.

Return type:

torch.Tensor

class Singleton(asset_ix)[source]

Bases: Benchmark

Predict a single asset.

Parameters:

asset_ix (int) – Index of the asset to predict.

__call__(x)[source]

Predict weights.

Parameters:

x (torch.Tensor) – Tensor of shape (n_samples, n_channels, lookback, n_assets).

Returns:

weights – Tensor of shape (n_samples, n_assets) representing the predicted weights.

Return type:

torch.Tensor

property hparams

Hyperparamters relevant to construction of the model.