(self, covmat, rets=None)
return w.squeeze(-1)
Perform forward pass.

        Parameters
        ----------
        covmat : torch.Tensor
            Covariance matrix of shape `(n_samples, n_assets, n_assets)`.

        rets : torch.Tensor or None
            If tensor then of shape `(n_samples, n_assets)` representing expected returns. If provided triggers
            computation of maximum share ratio. Else None triggers computation of minimum variance portfolio.

        Returns
        -------
        weights : torch.Tensor
            Of shape (n_samples, n_assets) representing the optimal weights. If `rets` provided, then it represents
            maximum sharpe ratio portfolio (tangency portfolio). Otherwise minimum variance portfolio.
Perform forward pass.
Perform forward pass .
def forward(self, covmat, rets=None):
        """Perform forward pass.

        Parameters
        ----------
        covmat : torch.Tensor
            Covariance matrix of shape `(n_samples, n_assets, n_assets)`.

        rets : torch.Tensor or None
            If tensor then of shape `(n_samples, n_assets)` representing expected returns. If provided triggers
            computation of maximum share ratio. Else None triggers computation of minimum variance portfolio.

        Returns
        -------
        weights : torch.Tensor
            Of shape (n_samples, n_assets) representing the optimal weights. If `rets` provided, then it represents
            maximum sharpe ratio portfolio (tangency portfolio). Otherwise minimum variance portfolio.
        """
        n_samples, n_assets, _ = covmat.shape
        device = covmat.device
        dtype = covmat.dtype

        covmat_inv = torch.inverse(covmat)
        ones = torch.ones(n_samples, n_assets, 1).to(device=device, dtype=dtype)

        if rets is not None:
            expected_returns = rets.view(n_samples, n_assets, 1)
        else:
            expected_returns = ones

        w_unscaled = torch.matmul(covmat_inv, expected_returns)
        denominator = torch.matmul(ones.permute(0, 2, 1), w_unscaled)
        w = w_unscaled / denominator

        return w.squeeze(-1)
def forward ( self , covmat , rets = None ) : n_samples , n_assets , _ = covmat . shape device = covmat . device dtype = covmat . dtype covmat_inv = torch . inverse ( covmat ) ones = torch . ones ( n_samples , n_assets , 1 ) . to ( device = device , dtype = dtype ) if rets is not None : expected_returns = rets . view ( n_samples , n_assets , 1 ) else : expected_returns = ones w_unscaled = torch . matmul ( covmat_inv , expected_returns ) denominator = torch . matmul ( ones . permute ( 0 , 2 , 1 ) , w_unscaled ) w = w_unscaled / denominator return w . squeeze ( - 1 )