openquant.sampling
Compiled from Rust (crates/pyopenquant). Signatures and types are from the shipped .pyi stubs; descriptions are the functions’ own docstrings, as help() shows them. All Python modules.
Functions
bootstrap_loop_run
bootstrap_loop_run(
ind_mat: Sequence[Sequence[int]],
prev_concurrency: Sequence[float],
) -> list[float]One step of the sequential bootstrap (the inner loop of AFML Snippet 4.5).
Returns the average uniqueness each label would have if it were drawn next: for label j, the mean over the bars t it spans of 1 / (1 + prev_concurrency[t]). A label that spans no bar gets 0. Normalising the result to sum to one gives the draw probabilities.
Parameters
ind_mat: list[list[int]]Indicator matrix, one row per bar and one 0/1 column per label (see
get_ind_matrix).prev_concurrency: list[float]How many times each bar is already covered by the labels drawn so far, one entry per row of
ind_mat.
Returns
- list[float]
One average uniqueness per label.
Raises
- ValueError
If the rows of
ind_matdiffer in length, orprev_concurrencydoes not have one entry per row.
get_av_uniqueness_from_triple_barrier
get_av_uniqueness_from_triple_barrier(
samples_info: Sequence[tuple[int, int]],
price_bars_len: int,
) -> list[float]Average uniqueness of each label over its lifespan, from label spans (AFML Snippet 4.4).
samples_info[i] = (start, end) is label i's inclusive span in bar positions, over bars 0..price_bars_len. Each label's value is sum_t u[t][i] / sum_t 1[t][i] with u[t][i] = 1 / c_t on the bars it spans and c_t the number of labels alive at bar t. Spans running past the last bar are truncated.
Parameters
samples_info: list[tuple[int, int]]Inclusive
(start, end)bar positions of each label.price_bars_len: intNumber of bars.
Returns
- list[float]
One value per label: 1.0 for a label that overlaps no other, 0.0 for one that spans no bar in range.
Raises
- ValueError
If any label has
start > end.
get_ind_mat_average_uniqueness
get_ind_mat_average_uniqueness(ind_mat: Sequence[Sequence[int]]) -> floatMean over labels of each label's average uniqueness (AFML Snippet 4.4, whole sample).
For each label i the average uniqueness is sum_t u[t][i] / sum_t ind_mat[t][i], with u[t][i] = ind_mat[t][i] / c_t and c_t the row sum at bar t. The result is the plain mean over labels that span at least one bar; all-zero columns are left out. An empty matrix, or one where no label spans any bar, gives 0.0. Pass a 0/1 matrix. Repeated columns (a bootstrap sample) are allowed and lower each other's uniqueness.
Parameters
ind_mat: list[list[int]]Indicator matrix, one row per bar and one 0/1 column per label (see
get_ind_matrix).
Returns
- float
The mean average uniqueness, in
[0, 1].
Raises
- ValueError
If the rows of
ind_matdiffer in length.
get_ind_mat_label_uniqueness
get_ind_mat_label_uniqueness(ind_mat: Sequence[Sequence[int]]) -> list[list[float]]Per-bar uniqueness u[t][i] = ind_mat[t][i] / c_t of every label (AFML section 4.4).
c_t is the row sum of ind_mat at bar t, the number of labels alive there. The result is indexed label first. An empty matrix gives an empty result.
Parameters
ind_mat: list[list[int]]Indicator matrix, one row per bar and one 0/1 column per label (see
get_ind_matrix).
Returns
- list[list[float]]
result[i][t]is labeli's uniqueness at bar (row)t, and 0 at bars the label does not span.
Raises
- ValueError
If the rows of
ind_matdiffer in length.
get_ind_matrix
get_ind_matrix(
label_endtime: Sequence[tuple[int, int]],
bar_index: Sequence[int],
) -> list[list[int]]Indicator matrix of which labels span which bars (AFML Snippet 4.3).
Works in bar positions, not timestamps: label_endtime[i] = (start, end) is label i's inclusive span. Entry [r][i] is 1 when start <= bar_index[r] <= end and 0 otherwise. A span reaching past the bars listed is truncated silently, and bar_index need not be contiguous. The matrix is dense (bars x labels).
Parameters
label_endtime: list[tuple[int, int]]Inclusive
(start, end)bar positions of each label.bar_index: list[int]Bar positions to report, in the order the rows should appear.
Returns
- list[list[int]]
A 0/1 matrix with one row per entry of
bar_indexand one column per label.
Raises
- ValueError
If any label has
start > end.
num_concurrent_events
num_concurrent_events(
price_index_len: int,
t1: Sequence[tuple[int, int]],
t_events: Sequence[int],
) -> list[int]Number of labels alive at each bar, c_t (AFML Snippet 4.1).
t1[i] = (start, end) is label i's inclusive span in bar positions. Spans are truncated at the last bar, and a span with start > end is skipped silently (where get_ind_matrix rejects it).
Parameters
price_index_len: intNumber of bars.
t1: list[tuple[int, int]]Inclusive
(start, end)bar positions of each label.t_events: list[int]Ignored; kept for signature compatibility with the snippet. Pass an empty list.
Returns
- list[int]
The concurrency count of each of the
price_index_lenbars.
seq_bootstrap
seq_bootstrap(
ind_mat: Sequence[Sequence[int]],
sample_length: int | None = None,
warmup_samples: Sequence[int] | None = None,
random_state: int | None = None,
) -> list[int]Sequential bootstrap of label indices (AFML Snippet 4.5).
Labels are drawn one at a time, each with probability proportional to the average uniqueness it would have given the draws so far (see bootstrap_loop_run). Repeats are possible, only less likely than under a uniform bootstrap. warmup_samples forces the first draws and is consumed from the end of the list (so [2, 0] draws label 0 first). If no label spans any bar, the later draws are uniform. The work is of order bars x labels^2, so bootstrap within blocks for large label sets.
Parameters
ind_mat: list[list[int]]Indicator matrix, one row per bar and one 0/1 column per label (see
get_ind_matrix).sample_length: int | None, default NoneNumber of draws; None draws as many as there are labels. 0 returns an empty list.
warmup_samples: list[int] | None, default NoneLabel indices forced as the first draws, taken from the end of the list.
random_state: int | None, default NoneSeed for a reproducible sample; None draws from the thread-local generator, so repeated calls differ.
Returns
- list[int]
Drawn label (column) indices, in draw order.
Raises
- ValueError
If the rows of
ind_matdiffer in length,ind_mathas no labels while a non-zerosample_lengthis requested, or awarmup_samplesindex is not a valid label.