Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(crossvalidation): align label ends to prior observation

PP1f764d89b79ff
diff --git a/agent/src/quantlib/crossvalidation.py b/agent/src/quantlib/crossvalidation.py
index 9394db071..0ce932bcb 100644
--- a/agent/src/quantlib/crossvalidation.py
+++ b/agent/src/quantlib/crossvalidation.py
@@ -133,9 +133,9 @@ def _as_label_spans(
starts = label_end_times.index
ends = label_end_times.to_numpy()
# searchsorted on the start index converts label end *times* into label
- # end *positions*; 'left' then clipping keeps a label that ends between
- # two observations attached to the earlier one.
- positions = np.searchsorted(starts, ends, side="left")
+ # end *positions*; the right insertion point minus one keeps a label
+ # that ends between two observations attached to the earlier one.
+ positions = np.searchsorted(starts, ends, side="right") - 1
positions = np.clip(positions, np.arange(len(starts)), len(starts) - 1)
span_ends = positions.astype(int)
else:
diff --git a/agent/tests/quantlib/test_crossvalidation.py b/agent/tests/quantlib/test_crossvalidation.py
index da1f0f461..7c7cf1198 100644
--- a/agent/tests/quantlib/test_crossvalidation.py
+++ b/agent/tests/quantlib/test_crossvalidation.py
@@ -225,6 +225,37 @@ def test_label_end_times_accepts_a_timestamp_series():
assert split.purged >= 0
+def test_label_ending_between_observations_maps_to_the_prior_observation():
+ index = pd.date_range("2024-01-01", periods=10, freq="B")
+ ends = pd.Series(index, index=index)
+ # Observation 3 starts on Thursday and resolves on Saturday. The second
+ # fold begins on Monday, so this label does not overlap that test block.
+ ends.iloc[3] = pd.Timestamp("2024-01-06")
+
+ split = list(
+ purged_kfold_splits(len(index), ends, n_folds=2, embargo_fraction=0.0)
+ )[1]
+
+ assert split.test_bounds == (5, 9)
+ assert 3 in split.train
+ assert split.purged == 0
+
+
+def test_label_ending_on_an_observation_keeps_that_exact_position():
+ index = pd.date_range("2024-01-01", periods=10, freq="B")
+ ends = pd.Series(index, index=index)
+ # Observation 3 resolves exactly when the second fold begins, so closed
+ # intervals overlap at that instant and the observation must be purged.
+ ends.iloc[3] = index[5]
+
+ split = list(
+ purged_kfold_splits(len(index), ends, n_folds=2, embargo_fraction=0.0)
+ )[1]
+
+ assert 3 not in split.train
+ assert split.purged == 1
+
+
def test_a_label_ending_before_it_starts_is_rejected():
bad = np.arange(100) - 5
with pytest.raises(ValueError, match="cannot end before"):