PineForge HPO 1.11.0
Native hyperparameter optimization for PineForge strategies
Loading...
Searching...
No Matches
pineforge_hpo.pbo Namespace Reference

Classes

class  MomentTable
 
class  PboError
 

Functions

MomentTable load_moment_table (Any parts, *int S, Any roster=None)
 
Triple merge_moments (Triple a, Triple b)
 
Triple pooled_moments (Iterable[Triple] blocks)
 
float|None ranking_statistic (int n, float s, float m2)
 
dict[str, int] estimate_work (int S, int N)
 
str roster_label (Any sampler, *Any early_stop=_UNSET)
 
Iterator[tuple[tuple[int,...], tuple[int,...]]] iter_ordered_splits (int S)
 
dict[str, Any] pbo (Any parts, *int S, Any roster=None, int|float|None max_work=None, str|None sampler=None, Any early_stop=_UNSET, bool with_lambdas=False)
 

Variables

str CONTRACT = "pineforge-hpo-pbo/v1"
 
str ALGORITHM = "cscv-all-ordered-splits/v1"
 
str STATISTIC = "pooled-mean-over-sample-sigma/v1"
 
str CONSTRAINT_VIOLATION = "constraint_violation"
 
str NON_POSITIVE_EQUITY = "non_positive_equity"
 
tuple ROSTER_ROW_STATUSES = (OK, CONSTRAINT_VIOLATION)
 
tuple ERROR_ROW_STATUSES
 
str PRUNED_ROW_STATUS = "pruned"
 
str PARTIAL_ROW_STATUS = "partial"
 
str RETURN_BLOCKS_CONTRACT = "pineforge-hpo-return-blocks/v1"
 
str RETURN_BLOCKS_SERIES = "bar"
 
tuple RETURN_BLOCKS_KEYS
 
str DATASET_IDENTITY_KEY = "dataset_identity"
 
tuple DECLARED_OBJECT_KEYS = RETURN_BLOCKS_KEYS + (DATASET_IDENTITY_KEY,)
 
dict TIE_RULES
 
tuple INTERPRETATION
 
tuple PAPER_CONFORMANT_SAMPLERS = ("grid", "random", "sobol")
 
tuple DIAGNOSTIC_SAMPLERS = ("tpe", "dlib_global", "candidates")
 
tuple CODES
 

Detailed Description

Probability of Backtest Overfitting (CSCV) over per-block moments: the exact stdlib reference.

Post hoc and pure. The input is an N x S table of block moments, one record per trial
(``{trial_id, status, block_returns: {status, count[S], sum[S], m2[S]}}``), never the T x N
return matrix. ``count`` is the number of intervals in a block, ``sum`` the sum of the
per-interval returns and ``m2`` the sum of squared deviations from the block mean. A row with
status ``ok`` or ``constraint_violation`` is computed (default D5); a failed, pruned or partial
row, or a payload that reports a non-positive equity, refuses with a code of its own. The rows
come in parts, each part with the result-level ``return_blocks`` object its rows were reduced
under plus a ``dataset_identity`` that the caller states (a call with one part passes a list of
one pair), and the caller declares the roster (the declared trial set). The module refuses
unless the parts declare one and the same object, every row's counts equal the ``n_b`` of its
own part's object, and the rows of all the parts together are exactly the roster. It checks what
it is handed: the grouping of the rows into parts and the object declared for each part are the
caller's. The module enumerates all C(S, S/2) ordered train/test splits of the S blocks, ranks
every trial on the training blocks, and reports how the training winner ranks on the
complementary blocks.

Every number is plain binary64 arithmetic in a fixed operation order, with no fused
operations and no tolerance. Counts and the sign of the logit are exact integer arithmetic; only
the logarithm of a logit value depends on the platform math library. Contract text and defaults:
``docs/pbo.md``.
Status: the native-proof PBO run uses synthetic data only; PBO on real data is not yet proven.

Function Documentation

◆ estimate_work()

dict[str, int] pineforge_hpo.pbo.estimate_work ( int  S,
int  N 
)
The admission figure of contract clause 3.7, exact integers, before any split is run.

``n_splits = C(S, S/2)``. Each complementary pair is evaluated once, so the merges are
``(n_splits/2) * N * (S - 2)``; the comparisons are ``2 * n_splits * N`` (a count of the
selected trial's rank, not a sort). ``work`` is their sum.

Definition at line 996 of file pbo.py.

Referenced by pineforge_hpo.pbo.pbo().

◆ iter_ordered_splits()

Iterator[tuple[tuple[int, ...], tuple[int, ...]]] pineforge_hpo.pbo.iter_ordered_splits ( int  S)
Yield every ordered ``(train, test)`` split: C(S, S/2) of them.

Train is a size-S/2 subset of the blocks in original order and test is its complement.
Order: the half that holds block 0, in lexicographic order, as train first and then as test.

Definition at line 1055 of file pbo.py.

◆ load_moment_table()

MomentTable pineforge_hpo.pbo.load_moment_table ( Any  parts,
*int  S,
Any   roster = None 
)
Validate the parts (contract clause 3.5) and return the merged table sorted by trial id.

``parts`` is a list of ``(declared object, rows)`` pairs, one for each part of the lineage (a
call with one part passes a list of one pair): the declared object is the result-level
``return_blocks`` object the rows of that part were reduced under, plus the
``dataset_identity`` that the caller states. ``roster`` is the declared trial set of the
lineage. Both are required. The declared objects of all the parts must be
one and the same, every row's counts must equal the ``n_b`` of its own part's object, and the
rows of all the parts together must be exactly the roster. Refuses with ``PboError`` and
never subsets: one bad row refuses the whole roster.

Definition at line 813 of file pbo.py.

◆ merge_moments()

Triple pineforge_hpo.pbo.merge_moments ( Triple  a,
Triple  b 
)
Merge two ``(count, sum, m2)`` triples, ``a`` before ``b``, by the Chan/Pebay rule.

``M2 = M2a + M2b + d*d*na*nb/n`` with ``d = sum_b/nb - sum_a/na``. The operation order is
pinned: ``(((d*d)*na)*nb)/n`` is formed first and added to ``(M2a + M2b)``; each operation
is a separate binary64 step, so nothing is fused. The term added is never negative, hence
neither is the result.

Definition at line 941 of file pbo.py.

Referenced by pineforge_hpo.pbo.pooled_moments().

◆ pbo()

dict[str, Any] pineforge_hpo.pbo.pbo ( Any  parts,
*int  S,
Any   roster = None,
int | float | None   max_work = None,
str | None   sampler = None,
Any   early_stop = _UNSET,
bool   with_lambdas = False 
)
Run CSCV over every ordered split and return the result object (contract clause 3.6).

``parts`` is a list of ``(declared object, rows)`` pairs, one for each part of the lineage (a
call with one part passes a list of one pair): the declared object is the result-level
``return_blocks`` object the rows of that part were reduced under, plus the
``dataset_identity`` that the caller states, and the rows are those that
``load_moment_table`` takes. A table is not accepted: it has lost its parts.
``roster`` (the declared trial set of the lineage) is required: the call refuses unless the
declared objects of all the parts are one and the same, every row's counts equal the ``n_b``
of its own part's object, and the rows of all the parts together are exactly the roster
(contract clause 3.5). Block sizes may differ by at most one interval (default D2); the
result then says ``equal_blocks: false`` and names ``block_counts``, and a larger spread
refuses.
``sampler`` and ``early_stop`` (the result's ``early_stop`` object, or None when it has none)
are both required and decide the roster label of default D6.
Per split, the training winner is the trial with the largest ranking statistic; k tied
winners (exact binary64 equality) each carry weight 1/k. Its out-of-sample rank is the
mid-rank among the N trials, ``omega = rank / (N + 1)`` and
``lambda = ln(omega / (1 - omega))``. The primary rate ``pbo`` is the share of splits with
lambda <= 0 (default D3); the share with lambda < 0 is reported next to it. A refusal raises
``PboError``; nothing partial is returned.

Definition at line 1112 of file pbo.py.

References pineforge_hpo.pbo.estimate_work(), and pineforge_hpo.pbo.roster_label().

◆ pooled_moments()

Triple pineforge_hpo.pbo.pooled_moments ( Iterable[Triple]  blocks)
Merge the triples left to right in the order given (block order); the fold is pinned.

Definition at line 960 of file pbo.py.

References pineforge_hpo.pbo.merge_moments().

◆ ranking_statistic()

float | None pineforge_hpo.pbo.ranking_statistic ( int  n,
float  s,
float  m2 
)
Pooled mean over sample sigma: ``(s/n) / sqrt(m2/(n-1))``; risk-free 0, unannualised.

Returns None when the variance is exactly zero in binary64 (the caller refuses; a flat
trial is never imputed). Refuses a count below 2 or a non-finite value.

Definition at line 973 of file pbo.py.

◆ roster_label()

str pineforge_hpo.pbo.roster_label ( Any  sampler,
*Any   early_stop = _UNSET 
)
The roster label of default D6; an unknown or missing sampler refuses.

``diagnostic_adaptive_roster`` for an adaptive sampler and for any sampler whose study had a
no-improvement early stop; ``paper_conformant`` for ``grid``, ``random`` and ``sobol`` without
one. The early stop is not a sampler: it is a separate object of the result, so it is its own
argument and ``"early_stop"`` is not a sampler name. As for ``pbo``, the caller must say
whether the study had one (the result's ``early_stop`` object or ``True``; ``None`` or
``False`` when it had none): leaving it out refuses with ``pbo_parameter_missing``, so a study
with an early stop cannot get ``paper_conformant`` by a default.

Definition at line 1017 of file pbo.py.

Referenced by pineforge_hpo.pbo.pbo().

Variable Documentation

◆ ALGORITHM

str pineforge_hpo.pbo.ALGORITHM = "cscv-all-ordered-splits/v1"

Definition at line 38 of file pbo.py.

◆ CODES

tuple pineforge_hpo.pbo.CODES
Initial value:
1= (
2 "pbo_input_invalid",
3 "pbo_parameter_missing",
4 "pbo_roster_mismatch",
5 "pbo_return_blocks_mismatch",
6 "pbo_block_sizes_uneven",
7 "pbo_dataset_mismatch",
8 "pbo_window_mismatch",
9 "pbo_identity_mismatch",
10 "pbo_rows_incomplete",
11 "pbo_status_error",
12 "pbo_status_pruned",
13 "pbo_status_partial",
14 "pbo_non_positive_equity",
15 "pbo_odd_s",
16 "pbo_too_few_trials",
17 "pbo_zero_variance",
18 "pbo_block_mismatch",
19 "pbo_resource_exceeded",
20 "pbo_sampler_unknown",
21)

Definition at line 98 of file pbo.py.

◆ CONSTRAINT_VIOLATION

str pineforge_hpo.pbo.CONSTRAINT_VIOLATION = "constraint_violation"

Definition at line 41 of file pbo.py.

◆ CONTRACT

str pineforge_hpo.pbo.CONTRACT = "pineforge-hpo-pbo/v1"

Definition at line 37 of file pbo.py.

◆ DATASET_IDENTITY_KEY

str pineforge_hpo.pbo.DATASET_IDENTITY_KEY = "dataset_identity"

Definition at line 77 of file pbo.py.

◆ DECLARED_OBJECT_KEYS

tuple pineforge_hpo.pbo.DECLARED_OBJECT_KEYS = RETURN_BLOCKS_KEYS + (DATASET_IDENTITY_KEY,)

Definition at line 78 of file pbo.py.

◆ DIAGNOSTIC_SAMPLERS

tuple pineforge_hpo.pbo.DIAGNOSTIC_SAMPLERS = ("tpe", "dlib_global", "candidates")

Definition at line 93 of file pbo.py.

◆ ERROR_ROW_STATUSES

tuple pineforge_hpo.pbo.ERROR_ROW_STATUSES
Initial value:
1= (
2 "error",
3 "objective_error",
4 "constraint_error",
5 "engine_error",
6 "trial_error",
7 "trial_timeout",
8)

Definition at line 48 of file pbo.py.

◆ INTERPRETATION

tuple pineforge_hpo.pbo.INTERPRETATION
Initial value:
1= (
2 "A deterministic frequency over the re-labellings of one dataset into training and testing "
3 "halves. The composition of the trial roster moves it. For adaptive or correlated trials it "
4 "is a diagnostic, not a probability under independent strategy draws."
5)

Definition at line 86 of file pbo.py.

◆ NON_POSITIVE_EQUITY

str pineforge_hpo.pbo.NON_POSITIVE_EQUITY = "non_positive_equity"

Definition at line 42 of file pbo.py.

◆ PAPER_CONFORMANT_SAMPLERS

tuple pineforge_hpo.pbo.PAPER_CONFORMANT_SAMPLERS = ("grid", "random", "sobol")

Definition at line 92 of file pbo.py.

◆ PARTIAL_ROW_STATUS

str pineforge_hpo.pbo.PARTIAL_ROW_STATUS = "partial"

Definition at line 57 of file pbo.py.

◆ PRUNED_ROW_STATUS

str pineforge_hpo.pbo.PRUNED_ROW_STATUS = "pruned"

Definition at line 56 of file pbo.py.

◆ RETURN_BLOCKS_CONTRACT

str pineforge_hpo.pbo.RETURN_BLOCKS_CONTRACT = "pineforge-hpo-return-blocks/v1"

Definition at line 61 of file pbo.py.

◆ RETURN_BLOCKS_KEYS

tuple pineforge_hpo.pbo.RETURN_BLOCKS_KEYS
Initial value:
1= (
2 "contract",
3 "series",
4 "S",
5 "K",
6 "n_b",
7 "boundary_time_ms",
8 "numeric_build_identity",
9)

Definition at line 63 of file pbo.py.

◆ RETURN_BLOCKS_SERIES

str pineforge_hpo.pbo.RETURN_BLOCKS_SERIES = "bar"

Definition at line 62 of file pbo.py.

◆ ROSTER_ROW_STATUSES

tuple pineforge_hpo.pbo.ROSTER_ROW_STATUSES = (OK, CONSTRAINT_VIOLATION)

Definition at line 47 of file pbo.py.

◆ STATISTIC

str pineforge_hpo.pbo.STATISTIC = "pooled-mean-over-sample-sigma/v1"

Definition at line 39 of file pbo.py.

◆ TIE_RULES

dict pineforge_hpo.pbo.TIE_RULES
Initial value:
1= {
2 "equality": "exact_binary64",
3 "train_best": "mixture_weight_1_over_k",
4 "oos_rank": "mid_rank",
5}

Definition at line 80 of file pbo.py.