(m, shrinkage_strategy=None, shrinkage_coef=0.5)
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 : torch.Tensor
            Covariance matrix of shape (n_assets, n_assets).
Compute covariance matrix for a single sample.
Compute covariance matrix for a single sample .
def compute_covariance(m, shrinkage_strategy=None, shrinkage_coef=0.5):
        """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 : torch.Tensor
            Covariance matrix of shape (n_assets, n_assets).

        """
        fact = 1.0 / (m.size(1) - 1)
        m -= torch.mean(m, dim=1, keepdim=True)  # !!!!!!!!!!! INPLACE
        mt = m.t()

        s = fact * m.matmul(mt)  # sample covariance matrix

        if shrinkage_strategy is None:
            return s

        elif shrinkage_strategy == 'identity':
            identity = torch.eye(len(s), device=s.device, dtype=s.dtype)

            return shrinkage_coef * s + (1 - shrinkage_coef) * identity

        elif shrinkage_strategy == 'scaled_identity':
            identity = torch.eye(len(s), device=s.device, dtype=s.dtype)
            scaled_identity = identity * torch.diag(s).mean()

            return shrinkage_coef * s + (1 - shrinkage_coef) * scaled_identity

        elif shrinkage_strategy == 'diagonal':
            diagonal = torch.diag(torch.diag(s))

            return shrinkage_coef * s + (1 - shrinkage_coef) * diagonal
def compute_covariance ( m , shrinkage_strategy = None , shrinkage_coef = 0.5 ) : fact = 1.0 / ( m . size ( 1 ) - 1 ) m -= torch . mean ( m , dim = 1 , keepdim = True ) # !!!!!!!!!!! INPLACE mt = m . t ( ) s = fact * m . matmul ( mt ) # sample covariance matrix if shrinkage_strategy is None : return s elif shrinkage_strategy == 'identity' : identity = torch . eye ( len ( s ) , device = s . device , dtype = s . dtype ) return shrinkage_coef * s + ( 1 - shrinkage_coef ) * identity elif shrinkage_strategy == 'scaled_identity' : identity = torch . eye ( len ( s ) , device = s . device , dtype = s . dtype ) scaled_identity = identity * torch . diag ( s ) . mean ( ) return shrinkage_coef * s + ( 1 - shrinkage_coef ) * scaled_identity elif shrinkage_strategy == 'diagonal' : diagonal = torch . diag ( torch . diag ( s ) ) return shrinkage_coef * s + ( 1 - shrinkage_coef ) * diagonal