Skip to Content

lacuna.validation

Bootstrap, Sharpe inference, permutation tests, multiple testing and backtest-overfitting checks.

Generated from Lacuna 0.14.3. For what these do and when to use them, read the guide.

Functions

bootstrap

def bootstrap(
    data: object,
    *,
    value: str = 'value',
    statistic: Statistic = 'mean',
    method: BootstrapMethod = 'moving',
    block_length: int | None = None,
    expected_block_length: int | None = None,
    resamples: int = 1000,
    confidence_level: float = 0.95,
    interval: IntervalMethod = 'percentile',
    seed: int | None = None,
    null_policy: Literal['drop', 'raise'] = 'drop',
    store_distribution: bool = False,
    use_native: bool = True,
    batch_memory_bytes: int = 16777216,
) -> AnalysisResult

Estimate a statistic and confidence interval with deterministic resampling.

Moving, circular, and stationary methods preserve local dependence. Each replicate derives its random stream from (seed, method_version, index).

joint_stationary_bootstrap

def joint_stationary_bootstrap(
    data: object,
    *,
    strategy_columns: Sequence[str] | None = None,
    expected_block_length: int | None = None,
    resamples: int = 1000,
    seed: int | None = None,
    store_distribution: bool = False,
) -> AnalysisResult

Jointly bootstrap synchronous strategy means with random-length blocks.

One stationary-bootstrap index path is shared by every strategy in each replicate. This preserves both within-series time dependence and cross-strategy dependence.

multiple_testing

def multiple_testing(
    data: object,
    *,
    p_value: str = 'p_value',
    trial: str = 'trial_id',
    method: MultipleTestingMethod = 'holm',
    alpha: float = 0.05,
    effective_trials: float | None = None,
    null_policy: Literal['drop', 'raise'] = 'raise',
) -> AnalysisResult

Adjust a declared family of p-values while preserving every trial identity.

Benjamini-Hochberg controls false discovery rate under independence or positive dependence. Benjamini-Yekutieli is the conservative dependence-robust variant.

parameter_surface

def parameter_surface(
    evaluate: Callable[[Mapping[str, JsonValue]], AnalysisResult],
    *,
    grid: Mapping[str, Sequence[JsonValue]],
    objective: str,
    evaluator_name: str,
    sample_id: str,
    code_id: str,
    evaluator_version: int = 1,
    direction: ObjectiveDirection = 'maximize',
    selected_parameters: Mapping[str, JsonValue] | None = None,
    selection_sample_id: str | None = None,
    failure_policy: SurfaceFailurePolicy = 'record',
    neighborhood_radius: int = 1,
    isolation_threshold: float = 3.0,
    plateau_tolerance: float = 0.1,
    evidence_threshold: float | None = None,
    max_evaluations: int = 10000,
    registry: ExperimentRegistry | None = None,
) -> AnalysisResult

Evaluate a complete parameter grid and quantify local optimum stability.

Adjacency uses Manhattan distance over each parameter’s declared ordered grid. Failed points remain in the surface and in an optional experiment registry.

permutation_test

def permutation_test(
    data: object,
    *,
    value: str = 'value',
    paired_with: str | None = None,
    statistic: PermutationStatistic = 'mean',
    scheme: PermutationScheme = 'sign_flip',
    time: str = 'time',
    group: str = 'group',
    block_length: int | None = None,
    permutations: int = 1000,
    alternative: PermutationAlternative = 'two_sided',
    seed: int | None = None,
    null_policy: Literal['drop', 'raise'] = 'raise',
    store_distribution: bool = False,
) -> AnalysisResult

Test a statistic under an explicit exchangeability transformation.

sign_flip tests a zero-centered one-sample null. Other schemes permute value relative to paired_with and therefore require the paired column. Date/group schemes permute only within the named strata; block permutation reorders non-overlapping chronological blocks.

probability_of_backtest_overfitting

def probability_of_backtest_overfitting(
    data: object,
    *,
    strategy_columns: Sequence[str] | None = None,
    partitions: int = 8,
    statistic: PBOStatistic = 'sharpe',
    partition_sensitivity: Sequence[int] | None = None,
    tie_break: PBOTieBreak = 'raise',
    max_combinations: int = 20000,
) -> AnalysisResult

Estimate PBO with combinatorially symmetric cross-validation (CSCV).

This consumes a synchronous T x N matrix of already-computed strategy performance, partitions its rows into equal chronological groups, performs every symmetric IS/OOS selection, and reports the selected strategy’s OOS relative rank and logit for every combination. It is deliberately distinct from model-fitting CPCV in :mod:lacuna.cv.

reality_check

def reality_check(
    data: object,
    *,
    strategy_columns: Sequence[str] | None = None,
    expected_block_length: int | None = None,
    resamples: int = 1000,
    seed: int | None = None,
    store_distribution: bool = False,
) -> AnalysisResult

Run White’s Reality Check over a declared strategy family.

Input cells are synchronous performance differentials versus one common benchmark, with positive values meaning that a candidate is better. The full matrix is resampled jointly with the stationary bootstrap.

sharpe_inference

def sharpe_inference(
    data: object,
    *,
    value: str = 'value',
    benchmark: float = 0.0,
    confidence_level: float = 0.95,
    annualization: float = 1.0,
    trial_sharpes: Sequence[float] | None = None,
    independent_trials: float | None = None,
    null_policy: Literal['drop', 'raise'] = 'raise',
) -> AnalysisResult

Compute Sharpe uncertainty, PSR, DSR, and minimum track-record length.

Sharpe values are reported at annualization scale. Skewness and Pearson kurtosis are empirical standardized central moments. The asymptotic standard error follows Bailey and López de Prado’s non-Normal-return formulation.

superior_predictive_ability

def superior_predictive_ability(
    data: object,
    *,
    strategy_columns: Sequence[str] | None = None,
    expected_block_length: int | None = None,
    resamples: int = 1000,
    seed: int | None = None,
    store_distribution: bool = False,
) -> AnalysisResult

Run Hansen’s studentized Superior Predictive Ability test.

Returns the lower, consistent, and upper bootstrap p-values. Input cells are synchronous performance differentials versus one common benchmark; positive values mean that a candidate is better.

Types and constants

BootstrapMethod

BootstrapMethod = Literal['iid', 'moving', 'circular', 'stationary']

IntervalMethod

IntervalMethod = Literal['percentile', 'basic']

MultipleTestingMethod

MultipleTestingMethod = Literal['bonferroni', 'holm', 'benjamini_hochberg', 'benjamini_yekutieli']

ObjectiveDirection

ObjectiveDirection = Literal['maximize', 'minimize']

PBOStatistic

PBOStatistic = Literal['mean', 'sharpe']

PBOTieBreak

PBOTieBreak = Literal['raise', 'first']

PermutationAlternative

PermutationAlternative = Literal['two_sided', 'greater', 'less']

PermutationScheme

PermutationScheme = Literal['unrestricted', 'within_date', 'within_group', 'block', 'sign_flip']

PermutationStatistic

PermutationStatistic = Union[Literal['mean', 'pearson'], collections.abc.Callable[[NDArray[numpy.float64], NDArray[numpy.float64] | None], float]]

Statistic

Statistic = Union[Literal['mean', 'median', 'sharpe'], collections.abc.Callable[[NDArray[numpy.float64]], float]]

SurfaceFailurePolicy

SurfaceFailurePolicy = Literal['record', 'raise']