Skip to main content

Walk-forward validation utilities for time-series ML: splits, purge/embargo, and evaluation helpers.

Project description

wfv-toolkit (wfvkit)

A tiny Python toolkit for walk‑forward validation of time‑ordered data with purge + embargo utilities to reduce label leakage (useful for trading/finance ML and any temporal prediction setup).

What you get

  • Naive time split (baseline): naive_time_split
  • Walk‑forward splits (rolling windows): walk_forward_splits
  • Leakage guards
    • purge_overlap(train_idx, test_idx) — removes train indices that overlap test
    • embargo_after(test_idx, embargo) — blocks samples immediately after the test window
  • A runnable example: examples/demo_naive_vs_purged.py
  • Tests: pytest

The core idea is common in financial ML: if labels use a forward horizon, nearby samples can “bleed” information between train/test. Purge and embargo help.


Install

Option A: editable install (recommended for development)

python -m venv .venv
.\.venv\Scripts\python.exe -m pip install -U pip
.\.venv\Scripts\python.exe -m pip install -e ".[dev]"

Option B: install from GitHub (once you start tagging releases)

pip install "git+https://github.com/Mohsentinal/wfv-toolkit.git@v0.1.0"

Quickstart

1) Run tests

.\.venv\Scripts\python.exe -m pytest -q

2) Run the demo

.\.venv\Scripts\python.exe examples\demo_naive_vs_purged.py

Usage

Import the public API

from wfvkit import (
    naive_time_split,
    walk_forward_splits,
    purge_overlap,
    embargo_after,
)

Naive split (baseline)

import datetime as dt

times = [dt.datetime(2025, 1, 1) + dt.timedelta(minutes=i) for i in range(10)]
train_end = times[6]

train_idx, test_idx = naive_time_split(times, train_end=train_end)
# train_idx -> [0,1,2,3,4,5]
# test_idx  -> [6,7,8,9]

Walk‑forward splits + purge + embargo

import datetime as dt

times = [dt.datetime(2025, 1, 1) + dt.timedelta(minutes=i) for i in range(50)]

for train_idx, test_idx in walk_forward_splits(
    times,
    train_size=20,
    test_size=5,
    step=5,
    embargo=2,
):
    train_purged = purge_overlap(train_idx, test_idx)
    embargo_idx = embargo_after(test_idx, embargo=2)

    # Fit on `train_purged`, evaluate on `test_idx`,
    # and avoid using indices in `embargo_idx` for training.

Concepts (plain English)

Purge

If a sample in train overlaps the test interval (or shares a window that touches the test range), it can leak information. Purging removes those training indices.

Embargo

Even after the test window ends, samples immediately after can still be “contaminated” if labels depend on future returns/horizons. Embargo blocks a small number of samples after test.


Project layout

wfv-toolkit/
  src/wfvkit/
    __init__.py
    splits.py
    leakage.py
    metrics.py
    evaluate.py
  tests/
  examples/

Roadmap (next nice upgrades)

  • Add purged k‑fold / combinatorial purged CV
  • Add utilities for event‑based labels (start/end times per sample)
  • Add richer evaluation helpers (rolling metrics and robustness checks)
  • Provide a small CLI (optional)

License

MIT (see LICENSE).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wfvkit-0.1.3.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wfvkit-0.1.3-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file wfvkit-0.1.3.tar.gz.

File metadata

  • Download URL: wfvkit-0.1.3.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for wfvkit-0.1.3.tar.gz
Algorithm Hash digest
SHA256 b20bafcde88833d07c699c21598e0c503ee58e96fe319286504307771530c04a
MD5 4d14ee44ba2f900bfa378376fbd151d2
BLAKE2b-256 181471c6cf5bc2192924eceac15cf33482e825d07239a5211bbf093d85a3a11b

See more details on using hashes here.

File details

Details for the file wfvkit-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: wfvkit-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for wfvkit-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1399cd1e6436ca05938b5353882b6ba92f6a04f4ed2326f1daa3fd762b784684
MD5 c7cee44edc5801b93206867b32497bcc
BLAKE2b-256 9a480cab657b262f3376fc7aaa944b782e1238864b475d8627bb05259c418293

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page