deepdow.layers.misc module

miscellaneous layers.

class Cov2Corr[source]

Bases: Module

Conversion from covariance matrix to correlation matrix.

forward(covmat)[source]

Convert.

Parameters:

covmat (torch.Tensor) – Covariance matrix of shape (n_samples, n_assets, n_assets).

Returns:

corrmat – Correlation matrix of shape (n_samples, n_assets, n_assets).

Return type:

torch.Tensor

training: bool
class CovarianceMatrix(sqrt=True, shrinkage_strategy='diagonal', shrinkage_coef=0.5)[source]

Bases: Module

Covariance matrix or its square root.

Parameters:
  • sqrt (bool) – If True, then returning the square root.

  • shrinkage_strategy (None or {'diagonal', 'identity', 'scaled_identity'}) – Strategy of combining the sample covariance matrix with some more stable matrix.

  • shrinkage_coef (float or None) – If float then in the range [0, 1] representing the weight of the convex combination. If shrinkage_coef=1 then using purely the sample covariance matrix. If shrinkage_coef=0 then using purely the stable matrix. If None then needs to be provided dynamically when performing forward pass.

static compute_covariance(m, shrinkage_strategy=None, shrinkage_coef=0.5)[source]

Compute covariance matrix for a single sample.

Parameters:
  • m (torch.Tensor) – Of shape (n_assets, n_channels).

  • shrinkage_strategy (None or {'diagonal', 'identity', 'scaled_identity'}) – Strategy of combining the sample covariance matrix with some more stable matrix.

  • shrinkage_coef (torch.Tensor) – A torch.Tensor scalar (probably in the range [0, 1]) representing the weight of the convex combination.

Returns:

covmat_single – Covariance matrix of shape (n_assets, n_assets).

Return type:

torch.Tensor

static compute_sqrt(m)[source]

Compute the square root of a single positive definite matrix.

Parameters:

m (torch.Tensor) – Tensor of shape (n_assets, n_assets) representing the covariance matrix - needs to be PSD.

Returns:

m_sqrt – Tensor of shape (n_assets, n_assets) representing the square root of the covariance matrix.

Return type:

torch.Tensor

forward(x, shrinkage_coef=None)[source]

Perform forward pass.

Parameters:
  • x (torch.Tensor) – Of shape (n_samples, dim, n_assets). The middle dimension dim represents the observations we compute the covariance matrix over.

  • shrinkage_coef (None or torch.Tensor) – If None then using the self.shrinkage_coef supplied at construction for each sample. Otherwise a tensor of shape (n_shapes,).

Returns:

covmat – Of shape (n_samples, n_assets, n_assets).

Return type:

torch.Tensor

training: bool
class KMeans(n_clusters=5, init='random', n_init=1, max_iter=30, tol=1e-05, random_state=None, verbose=False)[source]

Bases: Module

K-means algorithm.

Parameters:
  • n_clusters (int) – Number of clusters to look for.

  • init (str, {'random, 'k-means++', 'manual'}) – How to initialize the clusters at the beginning of the algorithm.

  • n_init (int) – Number of times the algorithm is run. The best clustering is determined based on the potential (sum of distances of all points to the centroids).

  • max_iter (int) – Maximum number of iterations of the algorithm. Note that if norm(new_potential - old_potential) < tol then stop prematurely.

  • tol (float) – If abs(new_potential - old_potential) < tol then algorithm stopped irrespective of the max_iter.

  • random_state (int or None) – Setting randomness.

  • verbose (bool) – Control level of verbosity.

static compute_distances(x, cluster_centers)[source]

Compute squared distances of samples to cluster centers.

Parameters:
  • x (torch.tensor) – Tensor of shape (n_samples, n_features).

  • cluster_centers (torch.tensor) – Tensor of shape (n_clusters, n_features).

Returns:

distances – Tensor of shape (n_samples, n_clusters) that provides for each sample (row) the squared distance to a given cluster center (column).

Return type:

torch.tensor

forward(x, manual_init=None)[source]

Perform clustering.

Parameters:
  • x (torch.Tensor) – Feature matrix of shape (n_samples, n_features).

  • manual_init (None or torch.Tensor) – If not None then expecting a tensor of shape (n_clusters, n_features). Note that for this feature to be used one needs to set init=’manual’ in the constructor.

Returns:

  • cluster_ixs (torch.Tensor) – 1D array of lenght n_samples representing to what cluster each sample belongs.

  • cluster_centers (torch.tensor) – Tensor of shape (n_clusters, n_features) representing the cluster centers.

initialize(x, manual_init=None)[source]

Initialize the k-means algorithm.

Parameters:
  • x (torch.Tensor) – Feature matrix of shape (n_samples, n_features).

  • manual_init (None or torch.Tensor) – If not None then expecting a tensor of shape (n_clusters, n_features). Note that for this feature to be used one needs to set init=’manual’ in the constructor.

Returns:

cluster_centers – Tensor of shape (n_clusters, n_features) representing the initial cluster centers.

Return type:

torch.Tensor

training: bool
class MultiplyByConstant(dim_size=1, dim_ix=1)[source]

Bases: Module

Multiplying constant.

Parameters:
  • dim_size (int) – Number of input channels. We learn one constant per channel. Therefore dim_size=n_trainable_parameters.

  • dim_ix (int) – Which dimension to apply the multiplication to.

forward(x)[source]

Perform forward pass.

Parameters:

x (torch.Tensor) – N-dimensional tensor of shape (d_0, d_1, …, d_{N-1})

Returns:

weights – Tensor of shape (d_0, d_1, …, d_{N-1}).

Return type:

torch.Torch

training: bool