Anomaly Metrics#

Anomaly Detection Metrics#

Early-warning and evaluation utilities for anomaly detection on time series.

Provided metrics#

  • early_roc_auc: ROC-AUC restricted to an early-detection horizon.

  • recall_at_lag: Recall counting predictions made within a lag window.

qmlhc.metrics.anomalies.early_roc_auc(y_true, scores, horizon=1)[source]#

Compute ROC-AUC restricted to an early-detection horizon.

Positive events occurring within the next horizon steps are labeled as positives for the current time index. The metric then estimates the ROC-AUC by pairwise comparisons between positive and negative score sets.

Parameters:
  • y_true (TensorLike) – Ground-truth anomaly indicator (1 for anomaly, 0 otherwise), shape (T,).

  • scores (TensorLike) – Continuous anomaly scores aligned with y_true, shape (T,).

  • horizon (int, optional) – Early-detection lookahead window, by default 1.

Returns:

Early-window ROC-AUC in [0, 1]. Returns 0.5 if positives or negatives are absent (uninformative baseline).

Return type:

float

qmlhc.metrics.anomalies.recall_at_lag(y_true, y_pred, lag=1)[source]#

Fraction of anomalies recalled within a backward lag window.

For each true anomaly at index i, counts a hit if any predicted positive occurs in [i - lag, i]. Suitable for evaluating early alarms that may precede the labeled event by up to lag steps.

Parameters:
  • y_true (TensorLike) – Ground-truth anomaly indicator (1 for anomaly, 0 otherwise), shape (T,).

  • y_pred (TensorLike) – Binary predictions aligned with y_true, shape (T,).

  • lag (int, optional) – Backward window size for crediting early detections, by default 1.

Returns:

Recall in [0, 1] computed with lag tolerance.

Return type:

float