Returns and losses
The returns module separates historical aggregation from forward target construction. This distinction is crucial for forecasting experiments.
price_returns
from capacities_ml_fin.finance import price_returns
log_returns = price_returns(prices, method="log")
simple_returns = price_returns(prices, method="simple")
For prices \(P_t\):
- log return:
- simple return:
The first periods observations are NaN because there is no previous price at the requested distance.
Log returns require strictly positive finite prices.
Historical aggregation
aggregate_returns forms a trailing return ending at time \(t\).
from capacities_ml_fin.finance import aggregate_returns
monthly_like = aggregate_returns(
daily_returns,
horizon=21,
method="log",
)
For log returns:
For simple returns:
This is a feature-like historical transformation: it contains no observations after \(t\).
Forward targets
forward_returns instead creates a target at position \(t\) from the next horizon observations:
from capacities_ml_fin.finance import forward_returns
target = forward_returns(
daily_returns,
horizon=5,
method="log",
)
The final horizon rows are NaN because the required future observations do not exist.
This convention is useful for supervised forecasting datasets:
The future information belongs in y, not in predictors.
Loss convention
The package uses
from capacities_ml_fin.finance import to_losses, forward_losses
losses = to_losses(returns)
future_loss = forward_losses(returns, horizon=5)
This means larger values represent larger losses in the risk module.
Excess returns
from capacities_ml_fin.finance import excess_returns
excess = excess_returns(asset_returns, risk_free)
This computes arithmetic subtraction
When pandas objects are supplied, labels are aligned before subtraction.
Wealth index
from capacities_ml_fin.finance import wealth_index
wealth = wealth_index(simple_returns, method="simple")
For simple returns:
For log returns:
The initial level must be positive and finite.
Example: feature and target timing
from capacities_ml_fin.finance import (
price_returns,
aggregate_returns,
forward_losses,
)
returns = price_returns(prices, method="log")
trailing_20d = aggregate_returns(returns, horizon=20, method="log")
y_5d_loss = forward_losses(returns, horizon=5, method="log")
At a given date \(t\):
trailing_20d[t]uses data up to and including \(t\);y_5d_loss[t]intentionally uses \(t+1\) through \(t+5\) and should therefore only be used as a supervised target.
API
See the Finance API for generated signatures and parameter documentation.