deepdow.losses module¶
Collection of losses.
All losses are designed for minimization.
- class Alpha(benchmark_weights=None, returns_channel=0, input_type='log')[source]¶
Bases:
Loss
Negative alpha with respect to a selected portfolio.
- Parameters:
benchmark_weights (torch.tensor or None) – Weights of the benchmark portfolio of shape (n_assets,). Note that this loss assumes it will be always located under this index in the `y tensor. If None then equally weighted portfolio.
returns_channel (int) – Which channel of the y target represents returns.
input_type (str, {'log', 'simple'}) – What type of returns are we dealing with in y.
- __call__(weights, y)[source]¶
Compute negative alpha with respect to the benchmark portfolio.
- 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:
Tensor of shape (n_samples,) representing the per sample negative alpha.
- Return type:
torch.Tensor
- class CumulativeReturn(returns_channel=0, input_type='log')[source]¶
Bases:
Loss
Negative cumulative returns.
- Parameters:
returns_channel (int) – Which channel of the y target represents returns.
input_type (str, {'log', 'simple'}) – What type of returns are we dealing with in y.
- __call__(weights, y)[source]¶
Compute negative simple cumulative returns.
- 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:
Tensor of shape (n_samples,) representing the per sample negative simple cumulative returns.
- Return type:
torch.Tensor
- class DownsideRisk(beta=2, returns_channel=0, input_type='log', output_type='simple')[source]¶
Bases:
Loss
Downside Risk.
- __call__(weights, y)[source]¶
Compute the downside risk.
- 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:
Tensor of shape (n_samples,) representing the per sample downside risk.
- Return type:
torch.Tensor
- class LargestWeight[source]¶
Bases:
Loss
Largest weight loss.
Loss function representing the largest weight among all the assets. It is supposed to encourage diversification since its minimal value is 1/n_asssets for the equally weighted portfolio (assuming full investment).
- __call__(weights, *args)[source]¶
Compute largest weight.
- Parameters:
weights (torch.Tensor) – Tensor of shape (n_samples, n_assets) representing the predicted weights by our portfolio optimizer.
args (list) – Additional arguments. Just used for compatibility. Not used.
- Returns:
Tensor of shape (n_samples,) representing the per sample largest weight.
- Return type:
torch.Tensor
- class Loss[source]¶
Bases:
object
Parent class for all losses.
Additionally it implement +, -, * and / operation between losses.
- __call__(weights, y)[source]¶
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_input_channels, horizon, n_assets) representing ground truth labels over the horizon of steps. The idea is that the channel dimensions can be given a specific meaning in the constructor.
- Returns:
Tensor of shape (n_samples,) representing the per sample loss.
- Return type:
torch.Tensor
- class MaximumDrawdown(returns_channel=0, input_type='log')[source]¶
Bases:
Loss
Negative of the maximum drawdown.
- __call__(weights, y)[source]¶
Compute maximum drawdown.
- 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:
Tensor of shape (n_samples,) representing the per sample maximum drawdown.
- Return type:
torch.Tensor
- class MeanReturns(returns_channel=0, input_type='log', output_type='simple')[source]¶
Bases:
Loss
Negative mean returns.
- __call__(weights, y)[source]¶
Compute negative mean returns.
- 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:
Tensor of shape (n_samples,) representing the per sample negative mean returns.
- Return type:
torch.Tensor
- class Quantile(returns_channel=0, q=0.1)[source]¶
Bases:
Loss
Compute negative percentile.
- Parameters:
q (float) – Number from (0, 1) representing the quantile.
- __call__(weights, y)[source]¶
Compute negative quantile.
- 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:
Tensor of shape (n_samples,) representing the per sample negative quantile.
- Return type:
torch.Tensor
- class RiskParity(returns_channel=0)[source]¶
Bases:
Loss
Risk Parity Portfolio.
- Parameters:
returns_channel (int) – Which channel of the y target represents returns.
- covariance_layer¶
Covarioance matrix layer.
- Type:
deepdow.layers.CoverianceMatrix
References
https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2297383
- __call__(weights, y)[source]¶
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:
Tensor of shape (n_samples,) representing the per sample risk parity.
- Return type:
torch.Tensor
- class SharpeRatio(rf=0, returns_channel=0, input_type='log', output_type='simple', eps=0.0001)[source]¶
Bases:
Loss
Negative Sharpe ratio.
- Parameters:
rf (float) – Risk-free rate.
returns_channel (int) – Which channel of the y target represents returns.
input_type (str, {'log', 'simple'}) – What type of returns are we dealing with in y.
output_type (str, {'log', 'simple'}) – What type of returns are we dealing with in the output.
eps (float) – Additional constant added to the denominator to avoid division by zero.
- __call__(weights, y)[source]¶
Compute negative sharpe ratio.
- 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:
Tensor of shape (n_samples,) representing the per sample negative sharpe ratio.
- Return type:
torch.Tensor
- class Softmax(returns_channel=0)[source]¶
Bases:
Loss
Softmax of per asset cumulative returns as the target.
- __call__(weights, y)[source]¶
Compute softmax 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:
Tensor of shape (n_samples,) representing the per sample negative worst return over the horizon.
- Return type:
torch.Tensor
- class SortinoRatio(rf=0, returns_channel=0, input_type='log', output_type='simple', eps=0.0001)[source]¶
Bases:
Loss
Negative Sortino ratio.
- Parameters:
rf (float) – Risk-free rate.
returns_channel (int) – Which channel of the y target represents returns.
input_type (str, {'log', 'simple'}) – What type of returns are we dealing with in y.
output_type (str, {'log', 'simple'}) – What type of returns are we dealing with in the output.
eps (float) – Additional constant added to the denominator to avoid division by zero.
- __call__(weights, y)[source]¶
Compute negative Sortino ratio of portfolio return over the horizon.
- 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:
Tensor of shape (n_samples,) representing the per sample negative worst return over the horizon.
- Return type:
torch.Tensor
- class SquaredWeights[source]¶
Bases:
Loss
Sum of squared weights.
Diversification loss. The equally weighted portfolio has a loss of 1 / n_assets, the lowest possible. The single asset portfolio has a loss of 1.
- __call__(weights, *args)[source]¶
Compute sum of squared weights.
- Parameters:
weights (torch.Tensor) – Tensor of shape (n_samples, n_assets) representing the predicted weights by our portfolio optimizer.
args (list) – Additional arguments. Just used for compatibility. Not used.
- Returns:
Tensor of shape (n_samples,) representing the per sample sum of squared weights.
- Return type:
torch.Tensor
Notes
If single asset then equal to 1. If equally weighted portfolio then 1/N.
- class StandardDeviation(returns_channel=0, input_type='log', output_type='simple')[source]¶
Bases:
Loss
Standard deviation.
- __call__(weights, y)[source]¶
Compute standard deviation.
- 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:
Tensor of shape (n_samples,) representing the per sample standard deviation.
- Return type:
torch.Tensor
- class TargetMeanReturn(target=0.01, p=2, returns_channel=0, input_type='log', output_type='simple')[source]¶
Bases:
Loss
Target mean return.
Difference between some desired mean return and the realized one.
- __call__(weights, y)[source]¶
Compute distance from the target return.
- 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:
Tensor of shape (n_samples,) representing the per sample negative mean returns.
- Return type:
torch.Tensor
- class TargetStandardDeviation(target=0.01, p=2, returns_channel=0, input_type='log', output_type='simple')[source]¶
Bases:
Loss
Target standard deviation return.
Difference between some desired standard deviation and the realized one.
- __call__(weights, y)[source]¶
Compute distance from the target return.
- 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:
Tensor of shape (n_samples,) representing the per sample negative mean returns.
- Return type:
torch.Tensor
- class WorstReturn(returns_channel=0, input_type='log', output_type='simple')[source]¶
Bases:
Loss
Negative of the worst return.
This loss is designed to discourage outliers - extremely low returns.
- __call__(weights, y)[source]¶
Compute negative of the worst return of the portfolio return over the horizon.
- 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:
Tensor of shape (n_samples,) representing the per sample negative worst return over the horizon.
- Return type:
torch.Tensor
- covariance(x, y)[source]¶
Compute covariance between two 2D tensors.
- Parameters:
x (torch.tensor) – Torch tensor of shape (n_samples, horizon)
y (torch.tensor) – Tensor of shape (n_samples, horizon)
- Returns:
cov – Torch tensor of shape (n_samples,).
- Return type:
torch.tensor
- log2simple(x)[source]¶
Turn log returns into simple returns.
r_simple = exp(r_log) - 1.
- Parameters:
x (torch.Tensor) – Tensor of any shape where each entry represents a logarithmic return.
- Returns:
Simple returns.
- Return type:
torch.Tensor
- portfolio_cumulative_returns(weights, y, input_type='log', output_type='simple', rebalance=False)[source]¶
Compute cumulative portfolio returns.
- 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, horizon, n_assets) representing the log return evolution over the next horizon timesteps.
input_type (str, {'log', 'simple'}) – What type of returns are we dealing with in y.
output_type (str, {'log', 'simple'}) – What type of returns are we dealing with in the output.
rebalance (bool) – If True, each timestep the weights are adjusted to be equal to be equal to the original ones. Note that this assumes that we tinker with the portfolio. If False, the portfolio evolves untouched.
- Returns:
Tensor of shape (n_samples, horizon).
- Return type:
torch.Tensor
- portfolio_returns(weights, y, input_type='log', output_type='simple', rebalance=False)[source]¶
Compute portfolio returns.
- Parameters:
weights (torch.Tensor) – Tensor of shape (n_samples, n_assets) representing the simple buy and hold strategy over the horizon.
y (torch.Tensor) – Tensor of shape (n_samples, horizon, n_assets) representing single period non-cumulative returns.
input_type (str, {'log', 'simple'}) – What type of returns are we dealing with in y.
output_type (str, {'log', 'simple'}) – What type of returns are we dealing with in the output.
rebalance (bool) – If True, each timestep the weights are adjusted to be equal to the original ones. Note that this assumes that we tinker with the portfolio. If False, the portfolio evolves untouched.
- Returns:
portfolio_returns – Of shape (n_samples, horizon) representing per timestep portfolio returns.
- Return type:
torch.Tensor