Sub topic 1
Why a Model Can Look Good Offline and Fail in Production
Prerequisites
This chapter assumes you know supervised machine learning terms (features, labels, train/validation split), SQL joins, pandas DataFrames, and timestamps with time zones.
How leakage inflates offline metrics
Temporal leakage happens when a training row contains information that did not exist at the moment the prediction would have been made. The model learns from that future information, so validation scores rise. In production, the future information is missing, and accuracy drops.
A common cause is a feature table that stores only the latest value per customer. When you join that table to historical labels, every row receives today's value, including rows from months ago.
Three clocks
Each record can carry up to three timestamps. Keeping them separate prevents most leakage.
| Clock | Question it answers | Example |
|---|---|---|
| Event time | When did it happen in the world? | A purchase at 09:14 on Monday |
| Ingestion time | When did our system receive it? | The batch load at 02:00 on Tuesday |
| Prediction time | When would the model have run? | Monday 12:00 |
Event time and ingestion time often differ. A record can describe Monday but arrive on Tuesday. A row is safe to use only if its ingestion time is at or before the prediction time.
Entities, labels, and the prediction timestamp
Each training row has three parts: an entity (for example, a customer ID), a label (for example, churned within 30 days), and a prediction timestamp. The prediction timestamp fixes the moment the features must reflect. Every feature for that row must have been known at that moment.
Storing feature history
Store feature values as an append-only history. Each row keeps its entity, value, event time, and ingestion time. Then you can ask "what was the value at time T?" instead of "what is the value now?" Overwriting the latest value destroys the history you need to rebuild past training sets.
Exercise
A customer's feature row has event time Jan 20 and ingestion time Feb 3. Can it feed a prediction made on Feb 1? Why?
Show answer
No. Its ingestion time is after the prediction time, so the system did not have the value on Feb 1, and using it leaks future information.