AUC-ROC for TSAD

vangle makes it possible to easily create reactive documents containing interactive elements on static websites. Interacting with these elements updates all dependents on the page, giving immediate feedback when playing around with certain components (like manipulating a value in a formula and immediately seeing the changes reflected in its plot, for example). For December Adventure, I wanted to try using vangle to create a short explainer on AUC-ROC for time series anomaly detection.


Anomaly Score Sequences

UCR 170 is a series from the UCR Time Series Anomaly Archive, whose ground-truth anomaly is a peak that is oddly shaped compared to the others:

Time series anomaly detection algorithms assign anomaly scores to time series points. Ideally, we want an algorithm that assigns the maximum score to points corresponding to anomalies and the minimum score to all other points, as shown above. In reality, however, anomaly score sequences can look quite different.

Matrix Profiling

We can use the distance of subsequences in test data to their nearest neighbour in training data as the anomaly score sequence, for example. With stumpy:

training_data = data[:18500]
test_data = data[30000:36000]
window_size = 165

score = stumpy.stump(
  test_data,
  window_size,
  training_data,
  ignore_trivial=False
)[:, 0].astype(float)

This gives us an anomaly score for subsequences of a fixed size. For each point we take the mean score of all subsequences that contain this point, giving us the following anomaly score sequence:

HPM-based Anomaly Detection

Similarly, using anomaly detection based on hierarchical pattern matching, implemented in pbsf-lib:

score = 1 - hpm(
  training_data,
  test_data,
  {
    "segmenter_params": {
      "window_size": window_size,
      "differentiation": True
    },
    "discretiser_params": {
      "node_type": PLANode
    },
    "node_params": {
      "distance_threshold": lambda _: 5.0
    }
  }
)

Receiver Operating Characteristic

Based on an anomaly score sequence, we now want a binary detection: which points are anomalous and which points are normal? We can decide on this using a score threshold: all points with a score higher than or equal to 0.50 are considered anomalousThanks to vangle, you can change this threshold by dragging the highlighted number or the horizontal threshold line in the plots below., and points below this threshold are considered normal.

With a threshold of 0.50, 27.1% of the anomalous points are correctly marked as anomalous (which we call true positives), but 3.2% of the normal points are incorrectly marked (which we call false positives). We plot these rates for all thresholds using ROC curves.

Regular Detector

Random Detector

Perfect Detector

Area Under the Curve

We can use the area under the ROC curve as an evaluation metric:

However, slight shifts in the anomaly score sequence can have a large impact on the ROC curve. Even if detection is near-perfect, this might not be reflected in AUC-ROC, which is why we need other evaluation metrics for time series anomaly detection.

Shifting scores by 20 timesteps, and stretching them by a factor of 1.00, for example:


MonTueWedThuFriSatSun
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

reply to this note