Repository navigation
fix(ml): split cross-validation on dates, not on flattened rows (BUG-23) - #19
Merged
Merged
Conversation
`_prepare_dataset` returns one row per (date, asset) pair, but the splitter was
handed `len(X_full)` -- a row count -- and did `range(train_start, train_end)`
on row positions. On a 50-stock panel that made
train_size = 504 about ten trading days, not two years
gap = 10 less than a single cross-section, so it purged nothing
Measured on 600 dates x 50 assets:
row-based (before) train 6 days / test 2 days / SAME-DATE OVERLAP 1
every fold shared at least one date
date-based (after) train 252 days (2020-01-01..2020-12-17)
test 63 days (2021-01-01..2021-03-30) / overlap 0
Training and test therefore shared real trading days, so identical-date labels
leaked across the split and the purge gap was inert. `test_ic` / `test_icir`
from `main.py ml train` and `POST /api/ml/train` were close to in-sample
numbers. This is why the module's own docstring describing the gap in "trading
days" was misleading: the code counted rows.
Fix: `TimeSeriesCV._folds()` holds the fold arithmetic; `split()` maps it onto
rows as before, and a new `split_by_dates()` maps it onto the unique date axis
and returns row indices, so callers do not change shape. `_prepare_dataset`
now also returns the per-row date, because row counts per date vary (NaN assets
are dropped) and the caller cannot reconstruct the mapping.
One test fixture had to grow: `sample_factors` used 300 dates against a
default `train_window` of 504. It only "worked" because rows were being counted
as samples. It is now 800 dates, which is what the configured window actually
requires.
Tests added, all three checked against the old behaviour:
- test_train_and_test_are_disjoint_on_dates
- test_train_size_is_counted_in_days
- test_train_splits_on_dates_not_rows -- because the first two exercise
split_by_dates directly and would still pass if `train()` reverted to
`split(len(X_full))`; this one fails when it does.
1318 passed, 1 skipped.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
| `train()` went back to calling `split(len(X_full))`. This asserts which | ||
| one the caller actually uses. | ||
| """ | ||
| import quant_platform.alpha.ml_signal as ml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
P0. Second of the four research-integrity defects. The class docstring describes the purge gap in "trading days"; the code counted rows.
What was wrong
_prepare_datasetreturns one row per(date, asset)pair, but the splitter was handed a row count:and
split()doesrange(train_start, train_end)on row positions. On a 50-stock panel:train_size = 504gap = 10Measured on 600 dates × 50 assets:
Training and test therefore shared real trading days. For cross-sectional equity prediction that is the worst case: market state, sector rotation and volatility are all date-level, so the model was being evaluated on "a stock I have not seen on a day I have seen" rather than on genuine out-of-sample time.
test_ic/test_icirfrommain.py ml trainandPOST /api/ml/trainwere close to in-sample numbers.Fix
TimeSeriesCV._folds()holds the fold arithmetic once.split()maps it onto rows, unchanged in behaviour.split_by_dates()maps the same arithmetic onto the unique date axis and returns row indices, so callers keep the same shape._prepare_datasetnow also returns the per-row date. It has to: row counts per date vary when NaN assets are dropped, so the caller cannot reconstruct the mapping.A test fixture had to grow
sample_factorsused 300 dates against a defaulttrain_windowof 504. That only "worked" because rows were counted as samples. It is now 800 dates — what the configured window actually requires. Sizing the window down to fit the fixture would have hidden the same class of mistake.Tests — all three checked against the old behaviour
test_train_and_test_are_disjoint_on_datestest_train_size_is_counted_in_daystest_train_splits_on_dates_not_rowsThe third exists because the first two exercise
split_by_datesdirectly and would still pass iftrain()reverted tosplit(len(X_full)). It fails when it does — verified by reverting the call and watching it fail.1318 passed, 1 skipped.Impact
Any ML signal produced by
train()used date-overlapping folds, so priortest_ic/test_icirfigures need re-running. Those results should be markedaffectedrather than deleted, per the contract's own remediation procedure.🤖 Generated with Claude Code