walk_forwardΒΆ

Defined in fynance.data

walk_forward(n, train, test, step=None, purge=0)[source]

Generate purged walk-forward windows.

Each window trains on [t-train : t-purge] and tests on [t : t+test].

Parameters:
nint

Number of observations.

train, testint

Train and test window lengths.

stepint, optional

Roll step (defaults to test, i.e. non-overlapping test windows).

purgeint

Observations removed at the train/test boundary (embargo).

Yields:
(train_idx, test_idx)tuple of numpy.ndarray

Index arrays with test_idx strictly after train_idx.

Raises:
ValueError

If train <= 0 or purge >= train: either would yield empty train windows ([t-train : t-purge] becomes empty), which silently breaks a downstream fit with an opaque error instead of failing here. Also if step <= 0, which would never advance t and loop forever.