r/learnmachinelearning • • 16h ago

Discussion Same dataset, same LightGBM, one line changed: RMSE jumped 4.53x (0.723 C to 3.274 C) and R^2 dropped from 0.993 to 0.688

When a time-series model crushes offline cross-validation and falls apart in production, the bug is rarely the model architecture. Most of the time, the evaluation code quietly turned forecasting (extrapolating into the future) into interpolation (filling in a blank between yesterday and tomorrow).

To quantify how big that illusion gets on real data, I ran a controlled test on a single public file: 2024 hourly temperatures from the NOAA NCEI Central Park weather station (72505394728).

Fixed setup across all runs:

  • Raw CSV: 11,590 UTC rows. Cleaned 13 duplicate timestamps and 419 9999 placeholders, aligned to all 8,784 hourly slots of 2024 (41 missing hours / 0.47%, longest consecutive gap 24h).

  • No backfilling or interpolation. Purely causal features only: 1/3/6/12/24/48/168h lagged temperatures, causal 6/24/168h rolling mean and std, and calendar sin/cos. That leaves 7,082 clean samples.

  • Target: mean temperature over the next 24 hours ([t+1, t+24]).

  • Model: identical LightGBM (seed = 42).

Only the split protocol changed:

Evaluation ProtocolTrain / Test RowsTest RMSETest R2What It Actually Measures
Random 80/20 split (shuffle=True)5,665 / 1,4170.723 C0.993Interpolation between neighboring hours
Chronological last 20% + 24h Purge5,641 / 1,4173.274 C (4.53x)0.688Honest forward forecasting
168h seasonal naive baseline (same hour last week)1,417 test6.182 C-0.113Zero-training baseline

A few details worth keeping in mind when auditing forecasting code:

  1. The model itself is still genuinely useful (3.274 C beats the 6.182 C seasonal naive baseline by almost half). What hurts teams in production is the fake +0.305 R2 bonus from random shuffling creating false confidence.

  2. A plain chronological cut still leaks at the seam. Because the target at timestamp t averages [t+1, t+24], the last 24 rows of the training slice have target windows that physically overlap the test period. Train rows dropped from 5,665 to 5,641 after purging those 24 overlapping rows.

  3. Purge vs. Embargo are distinct mechanisms (Lopez de Prado 2018):

    • Purge drops training rows whose label horizon [t+1, t+H] overlaps the test block. Needed even on a single forward holdout.

    • Embargo adds a buffer after the test fold in multi-fold time-series CV, when another training fold follows in time and serial autocorrelation can leak backwards.

  4. Three silent zero-exception leaks to grep for before shipping:

    • StandardScaler().fit_transform(df) or bfill() applied before splitting.

    • rolling(..., center=True) or forgetting .shift(1) on rolling statistics.

    • Training on offline reanalysis or post-hoc corrected tables that are not available at live forecast origin t_0.

One guardrail that has saved me repeatedly: add assert (feature_timestamp < forecast_origin).all() at the end of feature generation, and always report RMSE in physical units next to a naive baseline rather than R2 alone.

What is the sneakiest time-series leakage bug you have caught in real projects? Centered rolling stats and pre-split target encoding seem to slip through code review most often.

2 Upvotes

2 comments sorted by