Skip to Content

lacuna.cv

Purged k-fold, combinatorial purged and walk-forward splits.

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

Classes

CombinatorialPurgedKFold

class CombinatorialPurgedKFold(
    n_groups: int = 6,
    n_test_groups: int = 2,
    embargo: int = 0,
    max_combinations: int = 10000,
    use_native: bool = True,
)

Combinatorial purged cross-validation with explicit path reconstruction.

Distinct observation times are divided into contiguous groups. Every n_test_groups combination is held out once, overlapping training-label intervals are purged, and an observation-count embargo is applied after each held-out group. The result includes every split and a deterministic assignment of test-group predictions to complete chronological paths.

FieldTypeDefault
n_groupsint6
n_test_groupsint2
embargoint0
max_combinationsint10000
use_nativeboolTrue

CombinatorialPurgedKFold.split

def split(
    data: object,
    *,
    time: str = 'observation_time',
    label_start: str = 'label_start',
    label_end: str = 'label_end',
) -> CombinatorialSplitResult

Generate purged combinations and reconstruct full test paths.

CombinatorialSplitResult

class CombinatorialSplitResult(
    folds: tuple[Fold, ...],
    paths: tuple[CPCVPath, ...],
    evidence: AnalysisResult,
)

Inspectable CPCV combinations and reconstructed backtest paths.

FieldTypeDefault
foldstuple[Fold, ...]required
pathstuple[CPCVPath, ...]required
evidenceAnalysisResultrequired

CombinatorialSplitResult.fold_table

fold_table: object

Return the JSON-compatible fold table used for visualization.

CombinatorialSplitResult.path_table

path_table: object

Return the group-to-fold assignments for every reconstructed path.

CombinatorialSplitResult.to_json

def to_json(*, indent: int | None = 2) -> str

Serialize split configuration, combinations, and path evidence.

CPCVPath

class CPCVPath(path: int, fold_by_group: tuple[int, ...], test_indices: tuple[int, ...])

One reconstructed CPCV path over every chronological group.

FieldTypeDefault
pathintrequired
fold_by_grouptuple[int, ...]required
test_indicestuple[int, ...]required

Fold

class Fold(
    fold: int,
    train_indices: tuple[int, ...],
    test_indices: tuple[int, ...],
    purged_indices: tuple[int, ...] = (),
    embargoed_indices: tuple[int, ...] = (),
)

One temporal fold represented by stable source-row indices.

FieldTypeDefault
foldintrequired
train_indicestuple[int, ...]required
test_indicestuple[int, ...]required
purged_indicestuple[int, ...]()
embargoed_indicestuple[int, ...]()

PurgedKFold

class PurgedKFold(n_splits: int = 5, embargo: int = 0, use_native: bool = True)

Chronological K-fold splitter with interval purging and optional embargo.

FieldTypeDefault
n_splitsint5
embargoint0
use_nativeboolTrue

PurgedKFold.split

def split(
    data: object,
    *,
    time: str = 'observation_time',
    label_start: str = 'label_start',
    label_end: str = 'label_end',
) -> SplitResult

Split rows, purge overlapping label intervals, then apply embargo.

SplitResult

class SplitResult(folds: tuple[Fold, ...], evidence: AnalysisResult)

Inspectable temporal folds and their structured evidence.

FieldTypeDefault
foldstuple[Fold, ...]required
evidenceAnalysisResultrequired

SplitResult.fold_table

fold_table: object

Return the JSON-compatible fold table used for visualization.

SplitResult.to_json

def to_json(*, indent: int | None = 2) -> str

Serialize split configuration and fold evidence.

WalkForward

class WalkForward(
    train: Duration,
    test: Duration,
    step: Duration,
    mode: Literal['expanding', 'rolling'] = 'expanding',
    allow_incomplete: bool = False,
)

Chronological expanding or rolling walk-forward splitter.

train, test, and step are either all observation counts (int) or all calendar durations ("5D", "2W", "3M", "1Y"). Observation counts advance over sorted unique observation times. A duration string is a *calendar* span: "5D" means five calendar days regardless of how many observations fall in them, which differs from lacuna.labels.forward_returns, where "5D" is counted in observations.

Test windows are half-open [test_start, test_end). Unless allow_incomplete is set, a calendar window is kept only when the data fully cover it. For Date observation times each observation covers its whole day, so a window is complete when test_end <= last_observation + 1 day; this matches the observation-count mode, where a window ending exactly at the last observation is kept. A Datetime observation is an instant with no implied bar length, so coverage is never inferred: such a window must satisfy test_end <= last_observation. Cast daily Datetime bars to Date (or set allow_incomplete=True) to test the final window.

FieldTypeDefault
trainDurationrequired
testDurationrequired
stepDurationrequired
modeLiteral['expanding', 'rolling']'expanding'
allow_incompleteboolFalse

WalkForward.split

def split(data: object, *, time: str = 'time') -> SplitResult

Create deterministic folds over sorted unique observation times.

Types and constants

Duration

Duration = str | int