準則集合¶
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