KatInterview

Panel Data Cross-Validation with Temporal Embargo

Machine LearningModel answer
MediumModel answerMachine Learningcross-validationtime-seriespanel-data

Last updated

Design a time-series CV splitter for panel data that prevents entity leakage across train/test sets using an embargo period and supports geographic holdout folds.

You're building a cross-validation strategy for a financial forecasting model that uses panel data with loans tracked across multiple metropolitan areas over time.

Your task is to design and implement a cross-validation splitter with these specifications:

Input: A DataFrame with columns loan_id, msa (metropolitan statistical area), and month.

Requirements:

  1. Expanding-window folds: Create 5 folds with increasing time cutoffs. For fold k, training data should include all months up to cutoff k minus an embargo period, and test data should span from the embargo boundary to the next cutoff.

  2. Embargo mechanism: Use a 90-day embargo to prevent information leakage. Any loan appearing in the test set must be completely removed from training data within that embargo window—this handles the same entity appearing at different times.

  3. Geographic holdout: In at least one fold, hold out an entire MSA's data for testing while maintaining temporal ordering within each region.

  4. Determinism: The splitter must produce identical results regardless of input row order.

  5. Algorithm specification: Provide the complete algorithm with time/space complexity analysis in terms of N (rows), G (number of MSAs), and K (folds).

  6. Testing strategy: Describe comprehensive unit tests covering adversarial scenarios: duplicate timestamps, gaps in the time series, loans that move between MSAs, and heavily imbalanced geographic distributions.

What matters: preventing both temporal leakage (future information bleeding into training) and entity leakage (the same loan in both sets during overlapping periods).

Hints

Hint 1

Think about how to achieve determinism when the input might arrive in any order—what canonical ordering would work?

Hint 2

The embargo operates on entities (loan_id), not just time windows. How do you ensure a loan in the test set is fully purged from nearby training periods?

Hint 3

For the geographic holdout fold, consider which MSA to select deterministically and how to exclude it from training while still respecting the time boundaries.