Which features are safe to use when predicting future events — temporal leakage in practice
Written by the mlprep team · Updated
You're predicting if a user will convert within 7 days. Your training pipeline runs at midnight. Which features are safe to use and which might leak future information?
The core question for every feature#
For each feature, ask: "At the moment I make this prediction (midnight tonight), does this value already exist in the world?"
If yes: safe. If the value requires events that happen after the prediction time: leakage.
Safe features (available at prediction time)#
Historical behavior: page views in the last 30 days, purchases in the last 90 days, last login date, total session count. All computed from events that already happened before midnight.
User attributes: account age, geographic region, device type, membership tier. Static or slowly-changing; exists before prediction time.
Time-windowed aggregates with a gap: "number of product views in the last 7 days, where 'last 7 days' ends at midnight yesterday." Safe as long as the window closes before the prediction timestamp.
External signals: weather forecast, stock price, trending topics at midnight. Exist at prediction time.
Leaky features — the subtle ones#
Post-prediction activity: "user added item to wishlist in the next 24 hours" — obvious, but variations appear in practice. Any feature that includes events from day 0 to day 7 when you're predicting on day 0 is leaky.
Target-correlated aggregates: "average purchase value of users who converted in the same cohort." This encodes information about who converts — directly leaking the label.
Rolling windows that extend past prediction time: "7-day moving average as of the end of the week" — if the week isn't over at prediction time, this includes future values. Always ensure rolling windows are computed relative to the prediction timestamp, not a later anchor.
Aggregations computed in the same batch: if your pipeline at midnight computes "session count in the last 7 days including today," and "today" covers 0-24 hours from midnight, any sessions that happen during the prediction day are included. For midnight predictions, safe. For predictions made at noon, leaky.
External join with future data: joining on "user's conversion outcome in the training dataset" as a feature — sounds absurd but happens in notebook pipelines where leakage is implicit.
Temporal cross-validation#
Standard k-fold CV randomizes train/test splits — users who converted in the future are in training, their pre-conversion behavior is in test. This lets the model learn from the future and produces catastrophically optimistic offline metrics.
Always use walk-forward validation for temporal data:
- Train on days 1-90
- Validate on days 91-97 (7-day horizon)
- Test on days 98-104
- Never shuffle
The midnight pipeline specific case#
Training pipeline runs at midnight. Prediction is for the next 7 days (days 1-7). Safe features: all aggregates computed up to midnight of day 0. Leaky: any feature that includes events from day 1 onward.
Common mistake: training pipeline runs at midnight on day 30 and computes "number of sessions in last 7 days" — this includes sessions from day 24-30. If you're predicting on day 23 at midnight, the feature isn't available yet. Point-in-time correctness requires computing features as they would have looked at the prediction timestamp, not the training pipeline run timestamp.
Production safeguard#
Log the feature values actually served at prediction time. Compare their distribution to training data distributions. A divergence often signals a temporal alignment bug — features computed at different times than when predictions are made.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
Temporal leakage: any feature that includes information from after the prediction timestamp. Safe: historical aggregates with windows that close before prediction time, static attributes. Leaky: post-prediction activity, rolling windows that extend past the prediction point, batch aggregates that accidentally include future events. Use walk-forward CV — never shuffle temporal data. Log serving features and compare to training distributions to catch misalignment in production.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- What is point-in-time correctness in a feature store and how does it prevent temporal leakage at scale?
- You discover your model has temporal leakage after it's already deployed and has been making predictions for a month. How do you handle this?
- How do you design a training dataset where labels are known with a lag — for example, returns that may arrive 30 days after purchase?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions