准则集合¶
LogWealth¶
- 描述:
LogWealth准则计算投资组合每日收益的负均值对数。该准则旨在最大化投资组合财富的期望值。- 函数调用:
- 代码:
>>> def compute_log_wealth_loss(self, portfolios: torch.Tensor, labels: torch.Tensor) -> torch.Tensor:
>>> """
>>> Compute the ``LogWealth`` loss, which calculates the negative mean logarithm of the portfolio's daily returns.
>>> :param portfolios: Portfolio weights tensor of shape (batch_size, num_assets).
>>> :param labels: Label tensor representing asset returns of shape (batch_size, num_assets).
>>> :return: ``LogWealth`` loss tensor, representing the negative mean logarithm of the portfolio's daily returns.
>>> """
>>> daily_returns = torch.sum(portfolios * labels, dim=-1)
>>> log_returns = torch.log(torch.clamp(daily_returns, min=1e-6))
>>> loss = - torch.mean(log_returns)
>>> return loss
LogWealthL2Diversification¶
- 描述:
LogWealthL2Diversification准则通过引入基于连续投资组合权重向量之间差异的 L2 范数的多样化惩罚,扩展了LogWealth准则。该惩罚鼓励投资组合实现多样化,降低集中风险。- 函数调用:
- 代码:
>>> def compute_log_wealth_l2_diversification_loss(self, portfolios: torch.Tensor, labels: torch.Tensor) -> torch.Tensor:
>>> """
>>> Compute the ``LogWealthL2Diversification`` loss, which extends the ``LogWealth`` loss by incorporating diversification measures.
>>>
>>> :param portfolios: Portfolio weights tensor of shape (batch_size, num_assets).
>>> :param labels: Label tensor representing asset returns of shape (batch_size, num_assets).
>>> :return: ``LogWealthL2Diversification`` loss tensor, a modified version of ``LogWealth`` loss with diversification metrics included.
>>> """
>>> daily_returns = torch.sum(portfolios * labels, dim=-1)
>>> log_returns = torch.log(torch.clamp(daily_returns, min=1e-6))
>>>
>>> # Calculate the similarity loss
>>> diff = portfolios[1:, :] - portfolios[:-1, :]
>>> diff_loss = torch.norm(diff, p=2, dim=-1)
>>>
>>> # The final loss combines the log wealth and the L2 norm of the differences
>>> loss = - torch.mean(log_returns) + self.config["LAMBDA_L2"] * torch.mean(diff_loss)
>>> return loss
LogWealthL2Concentration¶
- 描述:
LogWealthL2Concentration准则通过引入基于连续投资组合权重向量之间差异的负 L2 范数的集中惩罚,扩展了LogWealth准则。该惩罚鼓励投资组合集中,从而增加更高收益的潜力。- 函数调用:
- 代码:
>>> def compute_log_wealth_l2_concentration_loss(self, portfolios: torch.Tensor, labels: torch.Tensor) -> torch.Tensor:
>>> """
>>> Compute the ``LogWealthL2Concentration`` loss, which extends the ``LogWealth`` loss by incorporating concentration measures.
>>>
>>> :param portfolios: Portfolio weights tensor of shape (batch_size, num_assets).
>>> :param labels: Label tensor representing asset returns of shape (batch_size, num_assets).
>>> :return: ``LogWealthL2Concentration`` loss tensor, a modified version of ``LogWealth`` loss with concentration metrics included.
>>> """
>>> daily_returns = torch.sum(portfolios * labels, dim=-1)
>>> log_returns = torch.log(torch.clamp(daily_returns, min=1e-6))
>>>
>>> # Calculate the similarity loss
>>> diff = portfolios[1:, :] - portfolios[:-1, :]
>>> diff_loss = torch.norm(diff, p=2, dim=-1)
>>>
>>> # The final loss combines the log wealth and the L2 norm of the differences
>>> loss = - torch.mean(log_returns) - self.config["LAMBDA_L2"] * torch.mean(diff_loss)
>>> return loss
L2Diversification¶
- 描述:
L2Diversification准则基于连续投资组合权重向量之间差异的 L2 范数来衡量投资组合的多样化。该准则鼓励投资组合实现多样化,降低集中风险。- 函数调用:
- 代码:
>>> def compute_l2_diversification_loss(self, portfolios: torch.Tensor, labels: torch.Tensor) -> torch.Tensor:
>>> """
>>> Compute the ``L2Diversification`` loss, which measures the diversification of a portfolio based on the L2 norm of consecutive portfolio weight differences.
>>> This loss function calculates the mean L2 norm of the differences between consecutive portfolio weight vectors.
>>> :param portfolios: Portfolio weights tensor of shape (batch_size, num_assets).
>>> :param labels: Label tensor representing asset returns of shape (batch_size, num_assets).
>>> :return: ``L2Diversification`` loss tensor, indicating the degree of diversification based on the L2 norm of portfolio weight differences.
>>> """
>>> # Calculate the similarity loss
>>> # We use the L2 norm to measure the difference between consecutive predictions
>>> diff = portfolios[1:, :] - portfolios[:-1, :]
>>> diff_loss = torch.norm(diff, p=2, dim=-1)
>>> # By minimizing the L2 norm of the difference vectors, you will tend to get two vectors that are similar, since the
>>> # L2 norm measures the Euclidean distance between vectors. When the L2 norm is small, it means that the difference
>>> # between two vectors is small.
>>> loss = torch.mean(diff_loss)
>>> return loss
L2Concentration¶
- 描述:
L2Concentration准则基于连续投资组合权重向量之间差异的负 L2 范数来衡量投资组合的集中程度。该准则鼓励投资组合集中,从而增加更高收益的潜力。- 函数调用:
- 代码:
>>> def compute_l2_concentration_loss(self, portfolios: torch.Tensor, labels: torch.Tensor) -> torch.Tensor:
>>> """
>>> Compute the ``L2Concentration`` loss, which measures the concentration of a portfolio based on the L2 norm of consecutive portfolio weight differences.
>>> This loss function calculates the negative mean L2 norm of the differences between consecutive portfolio weight vectors.
>>> :param portfolios: Portfolio weights tensor of shape (batch_size, num_assets).
>>> :param labels: Label tensor representing asset returns of shape (batch_size, num_assets).
>>> :return: ``L2Concentration`` loss tensor, indicating the degree of concentration based on the negative mean L2 norm of portfolio weight differences.
>>> """
>>> # Calculate the similarity loss
>>> # We use the L2 norm to measure the difference between consecutive predictions
>>> diff = portfolios[1:, :] - portfolios[:-1, :]
>>> diff_loss = torch.norm(diff, p=2, dim=-1)
>>> # By minimizing the L2 norm of the difference vectors, you will tend to get two vectors that are similar, since the
>>> # L2 norm measures the Euclidean distance between vectors. When the L2 norm is small, it means that the difference
>>> # between two vectors is small.
>>> loss = - torch.mean(diff_loss)
>>> return loss
Volatility¶
- 描述:
Volatility准则基于每日收益的标准差来衡量投资组合的波动性。该准则旨在最小化模型的波动性,从而降低大额损失的风险。- 函数调用:
- 代码:
>>> def compute_volatility_loss(self, portfolios: torch.Tensor, labels: torch.Tensor) -> torch.Tensor:
>>> """
>>> Compute the ``Volatility`` loss, which measures the volatility of the portfolios based on the standard deviation of daily returns.
>>> This loss function calculates the standard deviation of the daily portfolio returns.
>>> :param portfolios: Portfolio weights tensor of shape (batch_size, num_assets).
>>> :param labels: Label tensor representing asset returns of shape (batch_size, num_assets).
>>> :return: ``Volatility`` loss tensor, representing the volatility of the portfolios based on the standard deviation of daily returns.
>>> """
>>> daily_returns = torch.sum(portfolios * labels, dim=-1)
>>> volatility = torch.std(daily_returns)
>>> loss = volatility
>>> return loss
CustomCriterion¶
- 描述:
CustomCriterion准则是用户定义自定义损失准则的占位符。该准则允许用户实现任何他们所需的自定义损失函数,以满足具体需求。- 函数调用:
- 代码:
>>> def compute_custom_criterion_loss(self, portfolios: torch.Tensor, labels: torch.Tensor) -> torch.Tensor:
>>> """
>>> Compute the ``CustomCriterion`` loss, which is left for the user to define.
>>> This loss function is a placeholder for the user to implement their own custom loss criterion.
>>> :param portfolios: Portfolio weights tensor of shape (batch_size, num_assets).
>>> :param labels: Label tensor representing asset returns of shape (batch_size, num_assets).
>>> :return: ``CustomCriteria`` loss tensor, representing the user-defined loss criterion.
>>> """
>>> # This is a placeholder for the user to implement their own custom loss function.
>>> # The implementation of the custom loss function is left to the user.
>>> loss = torch.tensor(0.0, requires_grad=True)
>>> return loss