Skip to content
Johan Peña

Published · 2 min read

Temporal validation without leakage

SismoNexo estimates, per zone and period, the probability of recording at least one earthquake above a magnitude threshold. In data like this, a random train/test split quietly hands the model the future, and the metrics look better than they are. The pipeline therefore validates chronologically and runs a leakage detector on each of its 45 configurations.

Split by time, and leave a gap

Validation is walk-forward in eight blocks. Each block tests on one year, validates on the two years before it, and trains on everything earlier. Between sets there is a gap equal to the forecast horizon, because a label at a given origin looks that many days ahead: without the gap, a training label would overlap the start of the next set.

The gap is not assumed; it is checked. The detector computes the smallest margin between the end of one set's label window and the first origin of the next, and fails if it is negative.

SismoNexo · pipeline/sismonexo/leakage.py
def check_overlap(df: pd.DataFrame, folds: list[Fold], horizon: int) -> dict:
    """La ventana de etiqueta de un bloque debe terminar antes del primer origen del bloque siguiente."""
    worst = 10**9
    for f in folds:
        o = df["origin"].to_numpy()
        gap_tv = o[f.val].min() - (o[f.train].max() + horizon)
        gap_vt = o[f.test].min() - (o[f.val].max() + horizon)
        worst = min(worst, int(gap_tv), int(gap_vt))
    ok = worst >= 0
    ...

Prove the features only see the past

A feature that accidentally uses events at or after the forecast origin is the hardest leak to spot by reading code. The detector tests it directly: it truncates the catalog at a few sampled origins, rebuilds the features from the truncated catalog, and requires them to equal the originals. If any feature used the future, removing the future would change it.

SismoNexo · pipeline/sismonexo/leakage.py
for o in probe:
    ref = df[df["origin"] == o].sort_values("zone")[FEATURES].to_numpy(np.float64)
    alt = build_matrix(ea.truncated(int(o)), unit, horizon, threshold, origins=np.array([o]))
    alt = alt.sort_values("zone")[FEATURES].to_numpy(np.float64)
    diff = np.abs(np.nan_to_num(ref, nan=-9e9) - np.nan_to_num(alt, nan=-9e9)).max(axis=0)
    ...
ok = worst < 1e-4

Four more checks run alongside: the scaler's statistics must match the training set, no zone-period pair may repeat across sets, no column may behave like the label, and test events with close neighbors just before the boundary are counted.

Tune on validation only

Isotonic calibration, the decision threshold and the weights of the hybrid model are all fitted on the validation years, never on the test year. Accuracy is not reported at all: with rare events it rewards predicting “nothing happens”. The metrics are PR-AUC, Brier score against the baseline, log-loss and calibration error.

The leak that stayed, and the result that didn't improve

One check never comes back clean. Aftershock sequences straddle the boundary between sets, so recent-activity features carry information across it. The split is chronological and the gap holds, but the detector raises this as a warning in all 45 configurations, and the limitations page says so.

The same honesty applies to results. On a 50 km grid for magnitude 4.0 and above, PR-AUC goes from 0.169 for the baseline to 0.350. By department, no model beats historical frequency (0.613 for both), so the application uses the baseline there.

← All writing