Sample Weights

Calculate the sample weights of labels for training a model: how much each label should count, by its uniqueness, its return or its age, as López de Prado proposes for labels that overlap.

  • Return attribution: the weight by return attribution. Each period’s log return is shared among the labels whose span includes it, and a label’s weight is the absolute sum of its shares. Labels whose spans saw large moves, and that share them with few other labels, count most, while a label over a flat stretch counts little. The weights add up to the number of labels.
  • Time decay: the weight by time decay. The weights fall linearly with the cumulative average uniqueness from 1 for the newest label to decay for the oldest, so that older observations count less, measured in unique information rather than in dates.

The formula is as follows, per method:

\[\text{Return attribution:}\;\; r_{t} = \ln\left(\frac{p_{t}}{p_{t-1}}\right),\;\; w \sim \text{}_{i} = \left|\sum_{t0_i<t \leq t1_i} \frac{r_{t}}{c_{t}}\right|\;\;\text{and}\;\; w_{i} = w \sim \frac{\text{}_{i} \cdot N}{\sum_{j} w} \sim \text{}_{j},\;\; \text{with } c_{t} \text{ the concurrency}\] \[\text{Time decay:}\;\; x_{i} \text{ the cumulative average uniqueness up to label } i \text{ and } X \text{ the total},\;\; \text{slope} = \frac{1 - \text{decay}}{X} \;\text{when decay} \geq 0,\;\; \text{else}\;\; \frac{1}{(\text{decay} + 1) \cdot X},\;\; \text{and}\;\; w_{i} = \max(0,\; 1 - \text{slope} \cdot X + \text{slope}\; x x_{i})\]

The weights look forward by construction, as the spans of the labels do.

Also known as: sample weights, return attribution, time decay, time-decay weights.

Example · AAPL, MSFT · 2024 to 2025

Install the Finance Toolkit with pip install financetoolkit -U, then call get_sample_weights:

from financetoolkit import Toolkit

toolkit = Toolkit(
    ["AAPL", "MSFT"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2024-01-01",
    end_date="2025-12-31",
)

weights = toolkit.technicals.get_sample_weights()

weights.dropna()

Which returns:

Parameters

get_sample_weights accepts the following parameters.

ParameterTypeDescriptionExample
methodstrThe weighting. Options are: “return” (return attribution), “time_decay” (time decay). Defaults to “return”."return"
labelspd.DataFrame | NoneLabels with a Holding Period per date and ticker, such as the result of get_triple_barrier_labels, get_fixed_horizon_labels or get_trend_scanning_labels. Defaults to None, which uses the triple-barrier labels with their defaults.None, which uses the triple-barrier labels with their defaults
decayfloatThe weight of the oldest label of “time_decay”, in (-1, 1]: 1 for no decay, 0 for a decay to zero and a negative value to give the oldest share (-decay) of the labels no weight. Defaults to 1.0.1.0
periodstrThe time period of the labels. Options are: “intraday” (intraday prices), “daily” (daily prices), “weekly” (weekly prices), “monthly” (monthly prices), “quarterly” (quarterly prices), “yearly” (yearly prices). Defaults to “daily”."weekly"
close_columnstrThe column name for closing prices in the historical data. Defaults to “Adj Close”."Close"
roundingint | NoneThe number of decimals to round the results to. Defaults to None, which uses the rounding of the Toolkit (4 unless set otherwise).2
show_columnsstr | list[str] | NoneThe columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column.None, which keeps every column

References

Share