(self, weights, y)
return rp
Compute loss.

        Parameters
        ----------
        weights : torch.Tensor
            Tensor of shape `(n_samples, n_assets)` representing the predicted
            weights by our portfolio optimizer.

        y : torch.Tensor
            Tensor of shape `(n_samples, n_channels, horizon, n_assets)`
            representing the evolution over the next `horizon` timesteps.

        Returns
        -------
         torch.Tensor
            Tensor of shape `(n_samples,)` representing the per sample risk parity.
Compute loss.
Compute loss .
def __call__(self, weights, y):
        """Compute loss.

        Parameters
        ----------
        weights : torch.Tensor
            Tensor of shape `(n_samples, n_assets)` representing the predicted
            weights by our portfolio optimizer.

        y : torch.Tensor
            Tensor of shape `(n_samples, n_channels, horizon, n_assets)`
            representing the evolution over the next `horizon` timesteps.

        Returns
        -------
         torch.Tensor
            Tensor of shape `(n_samples,)` representing the per sample risk parity.
        """
        n_assets = weights.shape[-1]
        covar = self.covariance_layer(y[:, self.returns_channel, ...])  # (n_samples, n_assets, n_assets)

        weights = weights.unsqueeze(dim=1)
        volatility = torch.sqrt(torch.matmul(weights,
                                             torch.matmul(covar,
                                                          weights.permute((0, 2, 1)))))  # (n_samples, 1, 1)
        c = (covar * weights) / volatility  # (n_samples, n_assets, n_assets)
        risk = volatility / n_assets  # (n_samples, 1, 1)

        budget = torch.matmul(weights, c)  # (n_samples, n_assets, n_assets)
        rp = torch.sum((risk - budget)**2, dim=-1).view(-1)  # (n_samples,)

        return rp
def __call__ ( self , weights , y ) : n_assets = weights . shape [ - 1 ] covar = self . covariance_layer ( y [ : , self . returns_channel , ... ] ) # (n_samples, n_assets, n_assets) weights = weights . unsqueeze ( dim = 1 ) volatility = torch . sqrt ( torch . matmul ( weights , torch . matmul ( covar , weights . permute ( ( 0 , 2 , 1 ) ) ) ) ) # (n_samples, 1, 1) c = ( covar * weights ) / volatility # (n_samples, n_assets, n_assets) risk = volatility / n_assets # (n_samples, 1, 1) budget = torch . matmul ( weights , c ) # (n_samples, n_assets, n_assets) rp = torch . sum ( ( risk - budget ) ** 2 , dim = - 1 ) . view ( - 1 ) # (n_samples,) return rp