Skip to content

Drift and retraining

limen_ml.drift

Has the world moved away from the data the model learnt from?

Three signals, each on plain sequences of numbers:

  • :func:population_stability_index: the drift of one feature between the training distribution and a recent one, in deciles of the reference;
  • :func:ks_distance: the two-sample Kolmogorov-Smirnov statistic;
  • :func:prediction_drift: the change of the mean predicted probability.

:func:make_report checks them against thresholds, :func:worst picks the feature that drifts most, and :class:RetrainingTrigger says whether to train again: on drift, or when enough new events have arrived. It never promotes a model.

DriftReport dataclass

The three drift values and whether each crosses its threshold.

RetrainingTrigger dataclass

Decision result for one monitoring tick.

population_stability_index

population_stability_index(reference: Sequence[float], candidate: Sequence[float], *, n_buckets: int = 10) -> float

PSI between two univariate distributions.

Buckets are quantile-based on the reference. Empty buckets get a small floor to keep the log finite.

ks_distance

ks_distance(reference: Sequence[float], candidate: Sequence[float]) -> float

Two-sample Kolmogorov-Smirnov statistic ∈ [0, 1].

Computes max_x |F_a(x) - F_b(x)| on the empirical CDFs. Ties between the two samples advance both pointers — important so the statistic is exactly zero on identical inputs.

prediction_drift

prediction_drift(reference_probs: Sequence[float], candidate_probs: Sequence[float]) -> float

Absolute change in mean predicted probability.

worst

worst(reports: Mapping[str, DriftReport]) -> tuple[str, DriftReport]

The feature that drifts most: first one over a threshold, then the highest PSI.

A retraining trigger looks at one feature, the worst: summing the alerts of seventeen columns would retrain on noise.