(self, matrix, rets=None, **kwargs)
return portfolios_t.mean(dim=0)
Perform forward pass.

        Only accepts keyword arguments to avoid ambiguity.

        Parameters
        ----------
        matrix : torch.Tensor
            Of shape (n_samples, n_assets, n_assets) representing the square of the covariance matrix if
            `self.square=True` else the covariance matrix itself.

        rets : torch.Tensor or None
            Of shape (n_samples, n_assets) representing expected returns (or whatever the feature extractor decided
            to encode). Note that `NCO` and `AnalyticalMarkowitz` allow for `rets=None` (using only minimum variance).

        kwargs : dict
            All additional input arguments the `self.allocator` needs to perform forward pass.

        Returns
        -------
        weights : torch.Tensor
            Of shape (n_samples, n_assets) representing the optimal weights.
Perform forward pass.
Perform forward pass .
def forward(self, matrix, rets=None, **kwargs):
        """Perform forward pass.

        Only accepts keyword arguments to avoid ambiguity.

        Parameters
        ----------
        matrix : torch.Tensor
            Of shape (n_samples, n_assets, n_assets) representing the square of the covariance matrix if
            `self.square=True` else the covariance matrix itself.

        rets : torch.Tensor or None
            Of shape (n_samples, n_assets) representing expected returns (or whatever the feature extractor decided
            to encode). Note that `NCO` and `AnalyticalMarkowitz` allow for `rets=None` (using only minimum variance).

        kwargs : dict
            All additional input arguments the `self.allocator` needs to perform forward pass.

        Returns
        -------
        weights : torch.Tensor
            Of shape (n_samples, n_assets) representing the optimal weights.

        """
        if self.random_state is not None:
            torch.manual_seed(self.random_state)

        n_samples, n_assets, _ = matrix.shape
        dtype, device = matrix.dtype, matrix.device
        n_draws = self.n_draws or n_assets  # make sure that if None then we have the same N=M

        covmat = matrix @ matrix if self.sqrt else matrix
        dist_rets = torch.zeros(n_samples, n_assets, dtype=dtype, device=device) if rets is None else rets

        dist = MultivariateNormal(loc=dist_rets, covariance_matrix=covmat)

        portfolios = []  # n_portfolios elements of (n_samples, n_assets)

        for _ in range(self.n_portfolios):
            draws = dist.rsample((n_draws,))  # (n_draws, n_samples, n_assets)
            rets_ = draws.mean(dim=0) if rets is not None else None  # (n_samples, n_assets)
            covmat_ = CovarianceMatrix(sqrt=self.uses_sqrt)(draws.permute(1, 0, 2))  # (n_samples, n_assets, ...)

            if isinstance(self.allocator, (AnalyticalMarkowitz, NCO)):
                portfolio = self.allocator(covmat=covmat_, rets=rets_)

            elif isinstance(self.allocator, NumericalMarkowitz):
                gamma = kwargs['gamma']
                alpha = kwargs['alpha']
                portfolio = self.allocator(rets_, covmat_, gamma, alpha)

            portfolios.append(portfolio)

        portfolios_t = torch.stack(portfolios, dim=0)  # (n_portfolios, n_samples, n_assets)

        return portfolios_t.mean(dim=0)
def forward ( self , matrix , rets = None , * * kwargs ) : if self . random_state is not None : torch . manual_seed ( self . random_state ) n_samples , n_assets , _ = matrix . shape dtype , device = matrix . dtype , matrix . device n_draws = self . n_draws or n_assets # make sure that if None then we have the same N=M covmat = matrix @ matrix if self . sqrt else matrix dist_rets = torch . zeros ( n_samples , n_assets , dtype = dtype , device = device ) if rets is None else rets dist = MultivariateNormal ( loc = dist_rets , covariance_matrix = covmat ) portfolios = [ ] # n_portfolios elements of (n_samples, n_assets) for _ in range ( self . n_portfolios ) : draws = dist . rsample ( ( n_draws , ) ) # (n_draws, n_samples, n_assets) rets_ = draws . mean ( dim = 0 ) if rets is not None else None # (n_samples, n_assets) covmat_ = CovarianceMatrix ( sqrt = self . uses_sqrt ) ( draws . permute ( 1 , 0 , 2 ) ) # (n_samples, n_assets, ...) if isinstance ( self . allocator , ( AnalyticalMarkowitz , NCO ) ) : portfolio = self . allocator ( covmat = covmat_ , rets = rets_ ) elif isinstance ( self . allocator , NumericalMarkowitz ) : gamma = kwargs [ 'gamma' ] alpha = kwargs [ 'alpha' ] portfolio = self . allocator ( rets_ , covmat_ , gamma , alpha ) portfolios . append ( portfolio ) portfolios_t = torch . stack ( portfolios , dim = 0 ) # (n_portfolios, n_samples, n_assets) return portfolios_t . mean ( dim = 0 )