(m)
return (v * s.sqrt().unsqueeze(-2)) @ v.transpose(-2, -1)
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 : torch.Tensor
            Tensor of shape `(n_assets, n_assets)` representing the square root of the covariance matrix.
Compute the square root of a single positive definite matrix.
Compute the square root of a single positive definite matrix .
def compute_sqrt(m):
        """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 : torch.Tensor
            Tensor of shape `(n_assets, n_assets)` representing the square root of the covariance matrix.

        """
        _, s, v = m.svd()

        good = s > s.max(-1, True).values * s.size(-1) * torch.finfo(s.dtype).eps
        components = good.sum(-1)
        common = components.max()
        unbalanced = common != components.min()
        if common < s.size(-1):
            s = s[..., :common]  # pragma: no cover
            v = v[..., :common]  # pragma: no cover
            if unbalanced:  # pragma: no cover
                good = good[..., :common]  # pragma: no cover
        if unbalanced:
            s = s.where(good, torch.zeros((), device=s.device, dtype=s.dtype))  # pragma: no cover

        return (v * s.sqrt().unsqueeze(-2)) @ v.transpose(-2, -1)
def compute_sqrt ( m ) : _ , s , v = m . svd ( ) good = s > s . max ( - 1 , True ) . values * s . size ( - 1 ) * torch . finfo ( s . dtype ) . eps components = good . sum ( - 1 ) common = components . max ( ) unbalanced = common != components . min ( ) if common < s . size ( - 1 ) : s = s [ ... , : common ] # pragma: no cover v = v [ ... , : common ] # pragma: no cover if unbalanced : # pragma: no cover good = good [ ... , : common ] # pragma: no cover if unbalanced : s = s . where ( good , torch . zeros ( ( ) , device = s . device , dtype = s . dtype ) ) # pragma: no cover return ( v * s . sqrt ( ) . unsqueeze ( - 2 ) ) @ v . transpose ( - 2 , - 1 )