Median Method

class dtaianomaly.anomaly_detection.MedianMethod(neighborhood_size_before: int, neighborhood_size_after: int | None = None)[source]

Anomaly detection based on the Two-sided Median Method.

The Median Method [basu2007automatic] computes the deviation of a sample compared to its neighborhood. This neighborhood is computed as a window around the sample. The deviation is consequently measured as the number of standard deviations the observations deviates from the mean of its neighborhood.

In contrast to the original paper, this implementation allows to define a different neighborhood size before and after the sample, to fine tune how much lookahead is allowed. In the ultimate case, if neighborhood_size_after = 0, then the Median Method is a purely online anomaly detector. Note, however, that this case differs from the One-Sided Median Method discussed in the original paper, which also uses the first order difference to detect anomalies.

Parameters:
  • neighborhood_size_before (int) – The number of observations before the sample to include in the neighborhood.

  • neighborhood_size_after (int, default=None) – The number of observations after the sample to include in the neighborhood. If None, the same value as window_size_before will be used.

Examples

>>> from dtaianomaly.anomaly_detection import MedianMethod
>>> from dtaianomaly.data import demonstration_time_series
>>> x, y = demonstration_time_series()
>>> median_method = MedianMethod(10).fit(x)
>>> median_method.decision_function(x)
array([1.1851476 , 0.68191703, 1.05125284, ..., 0.81373386, 1.86097851,
       0.05852008])

References

[basu2007automatic]

Basu, Sabyasachi, and Martin Meckesheimer. “Automatic outlier detection for time series: an application to sensor data.” Knowledge and Information Systems 11 (2007), 137-154, doi: 10.1007/s10115-006-0026-6.

decision_function(X: ndarray) ndarray[source]

Abstract method, compute anomaly scores.

Parameters:

X (array-like of shape (n_samples, n_attributes)) – Input time series.

Returns:

decision_scores – The computed anomaly scores.

Return type:

array-like of shape (n_samples)

fit(X: ndarray, y: ndarray | None = None) BaseDetector[source]

Abstract method, fit this detector to the given data.

Parameters:
  • X (array-like of shape (n_samples, n_attributes)) – Input time series.

  • y (array-like, default=None) – Ground-truth information.

Returns:

self – Returns the instance itself.

Return type:

BaseDetector