Build a forward-looking label, see which training labels use prices from the test period, and compare the accuracy of folds with and without a gap in scikit-learn’s TimeSeriesSplit.
After this lesson, you can find training labels that use prices from the test period and remove them with a gap.
Which training labels use prices from the test period?
A forward label uses prices after its own row, so a training row shortly before the test period can have a label that depends on test prices. With a label over three sessions and a test period starting at row 7, the labels of training rows 4, 5 and 6 use the closes of rows 7, 8 and 9.
\[
y_t = \mathbb{1}\big[P_{t+3} > P_t\big]
\]
Here \(P_t\) is the close of session \(t\) and \(y_t\) is 1 when the close three sessions later is higher, and 0 otherwise.
The example has twelve sessions of one made-up stock, whose close rises from 100 to 110 kronor at row 9, and its volume in shares. As in the lesson on shifting rows, .shift(-3) puts the close of three sessions later on each row. .dropna(subset=["future_close"]) drops the last three rows, whose labels are unknown, before the comparison, so that they do not become labels of 0.
The following box builds the sessions and the labels.
The rise at row 9 makes the labels of rows 6, 7 and 8 equal to 1, so training row 6 and test rows 7 and 8 share it.
How does a gap change the folds and the score?
TimeSeriesSplit(..., gap=3) leaves out the three rows before each test fold, the rows whose labels reach into the test period. On the last fold, a one-neighbour classifier on volume scores an accuracy of 1.0 without the gap and 0.0 with it, because without the gap the nearest neighbour of both test rows is row 6, whose label already knows the rise.
fold_accuracy fits KNeighborsClassifier(n_neighbors=1), which predicts the label of the training row with the closest volume, and returns the share of correct test predictions, which accuracy_score computes. The loop prints each fold’s training rows, test rows and accuracy for both gaps, as in the lesson on TimeSeriesSplit.
The following box scores both folds with and without the gap.
def fold_accuracy(train_positions, test_positions): train = rows.iloc[train_positions] test = rows.iloc[test_positions] model = KNeighborsClassifier(n_neighbors=1).fit(train[["volume"]], train["up"])return accuracy_score(test["up"], model.predict(test[["volume"]]))for gap in [0, 3]:for train_positions, test_positions in TimeSeriesSplit(n_splits=2, test_size=2, gap=gap).split(rows):print(gap, train_positions.tolist(), test_positions.tolist(), fold_accuracy(train_positions, test_positions))
The twelve sessions are built to make the leak visible. In real data, a gap can raise or lower the measured accuracy, and it makes the test use only labels known at the time. A gap equal to the label’s horizon works when the rows are consecutive sessions of one stock.
Your turn
Count the training rows of the last fold without a gap, rows 0 to 6, whose labels use a close from row 7 or later. You should see 3.
TipShow answer
A row’s label uses the close three rows later, so row \(t\) reaches row 7 or later when \(t + 3 \ge 7\).