LIVE
connecting to the live quote stream…
--:--:--UTC
mediumML○
#22

Purged & embargoed K-fold

mediumMLacceptance —~30 min

Implement purged_folds(n, k, purge, embargo) — K-fold splits for a time series of n observations. Cut the index into k contiguous test blocks (the remainder goes to the last block). For each block, the training set is every index NOT in [test_start - purge, test_end + embargo]. Return one row per fold: [test_start, test_end, train_count] with test_end inclusive.

Random K-fold on a time series lets tomorrow leak into today's training set; purging removes the overlap before the test block and the embargo removes the echo after it. The direction models in the ai quants folder are evaluated exactly this way.

example 1
input
n=100, k=5, purge=2, embargo=0
output
[[0, 19, 80], [20, 39, 78], [40, 59, 78], [60, 79, 78], [80, 99, 78]]
example 2
input
n=100, k=5, purge=2, embargo=3
output
[[0, 19, 77], [20, 39, 75], [40, 59, 75], [60, 79, 75], [80, 99, 78]]
constraints
  • k ≥ 2
  • n ≥ 4·k
  • 0 ≤ purge, embargo ≤ n // k
teacher · 2 hints · the first is free
↗ read the theory
the chapter this problem is cut from
▶ open in the workbench
run the same math on a real tape
●solution.py· utf-8 · spaces:4
ln 1, col 1tab indent · esc leave editor
python 3.13 · pyodide
+ 6 hidden on submit
input · keyword arguments to purged_folds()
n=100, k=5, purge=2, embargo=0
expected
[[0, 19, 80], [20, 39, 78], [40, 59, 78], [60, 79, 78], [80, 99, 78]]

the judge calls purged_folds() with exactly these inputs · outputs are compared to 6 decimal places

● standing bypython 3.13 in your browser · pyodide 0.28.3 · judged on the server to 6 dp⌘↵ run · ⌘⇧↵ submit
paper only · educational · not advice