Purged and Embargoed K-Fold Splitter
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
In financial machine learning a label often spans a time interval, for example the return from an entry time until a barrier is hit. Plain K-fold leaks information when a training label's interval overlaps the period a test fold covers, so the splitter has to remove those samples.
Given label start times `t0` and end times `t1`, implement `purged_kfold_split(t0, t1, n_splits=5, embargo_pct=0.0)` returning a list of `(train_idx, test_idx)` pairs.
- Test folds are contiguous blocks in input order, and the first `n % n_splits` folds get one extra sample.
- For a fold at positions `[a, b)`, the test window is `[min(t0[a:b]), max(t1[a:b])]`. Purge any other sample whose closed interval `[t0[i], t1[i]]` intersects it (touching counts).
- Embargo positions `b . b+h-1` (clipped to `n`), where `h = floor(embargo_pct * n)`.
Example:
```python
t0 = [0, 1, 2, 3, 4, 5]
t1 = [1, 2, 3, 4, 5, 6]
purged_kfold_split(t0, t1, n_splits=2)
# [([4, 5], [0, 1, 2]), # window [0, 3]: sample 3 touches at 3, purged
# ([0, 1], [3, 4, 5])] # window [3, 6]: sample 2 touches at 3, purged
```
```hint The test window
The inputs need not be sorted by time, so the last sample of a block does not have to carry the block's latest end time.
```
```hint Overlap of closed intervals
Two closed intervals intersect unless one of them ends strictly before the other starts. That condition can be evaluated for every sample at once with NumPy.
```
```hint Assembling the training set
Start from a mask over all positions and switch off each group of excluded samples in turn.
```
### Constraints & Assumptions
- Both index arrays are sorted ascending with dtype `int64`.
- Raise `ValueError` if `t0` and `t1` are not 1-D arrays of equal length, if `n_splits` is not an integer with `2 <= n_splits <= n`, if any `t1[i] < t0[i]`, or if `embargo_pct` is outside `[0, 1)`.
- Times are finite floats. Inputs need not be sorted by time.
### What a Strong Answer Covers
- Fold bounds taken in input order, with the remainder samples in the first folds.
- A test window built from the whole block (smallest start, largest end), and a closed-interval overlap test in which touching endpoints count.
- An embargo of `h` positions right after the test block, clipped at `n`, and the reason it only follows the block.
- Validation that raises `ValueError` for every listed bad input.
- The cost of building one mask per fold and how it scales with `n` and `n_splits`.
### Follow-up Questions
- How would you make this efficient for millions of samples?
- Why embargo only after the test fold and not before it?
- How would you extend this to combinatorial purged cross-validation?
Overview: Implement a K-fold splitter for samples whose labels span time intervals, purging training samples that overlap each test window and embargoing the positions right after it. It tests leakage control in time-dependent cross-validation, closed-interval overlap logic, and careful input validation.