Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Anomaly detection

  • In contrast to statistical outliers, anomaly detection is more often concerned with local deviations than deviations from a common distribution.

  • We will cover two density based methods and one tree based method.

  • Density based methods are highly dependent on scaling.

    • The amount and type of scaling is problem dependent and can be considered part of the tuning.

DBSCAN

  • Density-Based Spatial Clustering of Applications with Noise.

  • No assumptions about distributions.

  • Definitions:

    • ‘Core point’ if >= MinPts within radius ϵ\epsilon.

    • ‘Border point’ if < MinPts within radius ϵ\epsilon, but within radius of a ‘core point’.

    • Else a ‘Noise points’.

  • Clustering:

    • Cluster ‘core points’ that lay within each others radii.

    • Assign ‘border points’ to their respective ‘core point’ clusters.

  • Our main interest is in detecting ‘noise points’, i.e., outliers.

    • Also, small clusters may be indicative of series of outliers.

DBSCAN in 2D

DBSCAN in 1D

  • To avoid a pure vertical clustering in the charts, we need to use the observation/time dimension actively.

  • The horizontal spacing between points in the chart will be an extra parameter to tune.

  • More than one variable can be included in DBSCAN, but these must be matched by some form of scaling, e.g., standardisation.

  • DBSCAN does not care about drift in mean values, only local density (pro and con).

<Figure size 1000x300 with 1 Axes>
array([ 28, 957, 4, 3, 5, 3])
<Figure size 1000x300 with 1 Axes>
Loading...
Loading...

Exercise

  • Look at the DEWP measurements in the Beijing pollution data (2000 timepoints).

  • Try to tune a DBSCAN on the data.

  • Does it work as advertised?

Local Outlier Factor - LOF

  • LOF is a method that tries to determine if an observation is an outlier based on the density of observations around itself compared to the density of observations around its k nearest neighbours.

  • The general assumption is that LOFk(A)<1LOF_k(A) < 1 is an outlier, but this is problem dependent.

Definitions for LOF

For an observation A and a neighbour B:

  • k-distance(A) = distance(A, k-th nearest neighbour)

  • k-neighbourhood:
    Nk(A)={B∣distance(A, B)≤k-distance(A)}N_k(A) = \{\text{B} | \text{distance(A, B)} \leq \text{k-distance(A)}\},
    i.e., all observations no farther away than the k-th nearest point.

  • Reachability distance:
    RDk(A, B)=max{k-distance(B),distance(A, B)}RD_k(\text{A, B}) = max\{\text{k-distance(B)}, \text{distance(A, B)}\},
    i.e., the maximum of observation B’s k-distance and the actual distance between A and B.
    This is maybe the least intuitive definition here as it is called a distance but is based on density and thus is unsymmetric if you change around A and B. In the figure below, RD3(A, B)<RD3(A, C)RD_3(\text{A, B}) < RD_3(\text{A, C}) even though A is closer to B than to C.

  • Local reachability density:
    lrdk(A)=1/∑B∈Nk(A)RDk(A,B)∣Nk(A)∣lrd_k(A) = 1 / \frac{\sum_{B \in N_k(A)} RD_k(A, B)}{|N_k(A)|},
    i.e., the inverse of the average reachability distance of A from its neighbours.

  • Local Outlier Factor:
    LOFk(A)=∑B∈Nk(A)lrdk(B)lrdk(A)∣Nk(A)∣=∑B∈Nk(A)lrdk(B)∣Nk(A)∣lrdk(A)LOF_k(A) = \frac{\sum_{B \in N_k(A)}\frac{lrd_k(B)}{lrd_k(A)}}{|N_k(A)|} = \frac{\sum_{B \in N_k(A)}lrd_k(B)}{|N_k(A)| lrd_k(A)},
    i.e., the average local reachability density of the neighbors divided by the object’s own local reachability density.

LOF in scikit-learn

  • As nearest neighbour searches can be time and memory intensive, the implementation lets the user choose between NN algorithms.

  • The type of distance can be chosen.

  • A “contamination” parameter is used to score samples by scikit-learn. This is the proportion of outliers, which can be set to “auto” for automatic estimation. Otherwise the proportion is used to find the threshold of outlyingness.

[ 19   0 981]
0.019
<Figure size 700x300 with 1 Axes>
array([599, 0, 401])
1.5302463890590219
<Figure size 1000x300 with 1 Axes>

Isolation Forest

  • Isolation Forests use binary trees with random splits along variables/features to isolate outlying samles.

  • Basically, outlying observations are easier to split from inlying observation, thus having a shorter path through the tree before becomming a leaf node.

  • In contrast to DBSCAN and LOF, no density estimation is performed.

Isolation Forest algorithm

  • An Isolation Tree (iTree) is built on a subset of the samples as follows:

    1. Randomly choose a feature.

    2. Randomly split the (remaining) data along the feature.

    3. If no more splits are possible (all observations are leafs (alone) or all samples are equal in the end nodes): terminate,
      otherwise, repeat from 1.

  • The forest is created by building many iTrees (100 is default in scikit-learn).

  • Samples are deemed outliers if they on average become leaf nodes after few splits (low path-lengths in the trees).

  • In scikit-learn the cut-off for being an outlier is estimated automatically, but can be changed using the “contamination” parameter.

[ 50   0 950]
0.05
<Figure size 700x300 with 1 Axes>
array([ 66, 0, 934])
<Figure size 1000x300 with 1 Axes>

Exercise

  • Compare the three mentioned anomaly detection methods (DBSCAN, LOF and Isolation Forest) with regard to the samples seen as outliers.

    • Fix the parameters at min_samples = 3, n_neighbors = 20 and n_estimators = 100 and use eps and contamination to force 1, 2, ..., 10 outliers detected (if possible).

    • Are the three sets of 10 samples equal.

    • Do the samples appear in the same order for the different techniques?
      Spearman correlation is one way of putting this into numbers.

Notebook Cell