Title: | Detect Anomalies Using the Spectral Residual Algorithm |
---|---|
Description: | Apply the spectral residual algorithm to data, such as a time series, to detect anomalies. Anomaly scores can be used to determine outliers based upon a threshold or fed into more sophisticated prediction models. Methods are based upon "Time-Series Anomaly Detection Service at Microsoft", Ren, H., Xu, B., Wang, Y., et al., (2019) <doi:10.48550/arXiv.1906.03821>. |
Authors: | Allen OBrien [aut, cre, cph] |
Maintainer: | Allen OBrien <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.1.9000 |
Built: | 2025-03-10 02:04:23 UTC |
Source: | https://github.com/al-obrien/spectralanomaly |
Add n
anomalies to an existing vector.
add_anomaly(x, n, score_window, spec_window, var_cap_rng = 5:15)
add_anomaly(x, n, score_window, spec_window, var_cap_rng = 5:15)
x |
Numeric vector. |
n |
Integer value for number of anomalies to apply. |
score_window |
Integer value for the window width for scoring. |
spec_window |
Positive integer value for the window to calculate the averaged log spectrum. |
var_cap_rng |
A range of values to cap variances. |
Method to calculate is:
Will exclude the first point to ensure variance calculation has a useful value.
Convert an input of numeric data, typically a time series, into a score for anomaly detection. The data is first extended to improve the detection latency, followed by saliency map creation. The score is calculated using the sliding window average for each point in the saliency map.
anomaly_score(x, score_window, spec_window = 3, m = 5)
anomaly_score(x, score_window, spec_window = 3, m = 5)
x |
Numeric vector. |
score_window |
Integer value for the window width for scoring. |
spec_window |
Positive integer value for the window to calculate the averaged log spectrum. |
m |
Integer value representing the number of preceding points for the estimation. |
A numeric vector of anomaly scores.
tmp <- ts(rnorm(12*6,10,2), start=c(2009, 1), end=c(2014, 12), frequency=12) anomaly_score(tmp, score_window = 25)
tmp <- ts(rnorm(12*6,10,2), start=c(2009, 1), end=c(2014, 12), frequency=12) anomaly_score(tmp, score_window = 25)
A helper function that wraps around quantile
to apply a threshold to anomaly scores.
anomaly_thresh(x, threshold = 0.99, ...)
anomaly_thresh(x, threshold = 0.99, ...)
x |
Numeric vector of anomaly scores (e.g. created by |
threshold |
Numeric value to determine the threshold to flag outliers among the score. |
... |
Additional parameters passed to |
Logical vector referencing which, if any, of the provided values are outliers.
test_data <- c(1,2,3,4,5,100,5,4,3,2,1) anomaly_thresh(test_data, 0.99)
test_data <- c(1,2,3,4,5,100,5,4,3,2,1) anomaly_thresh(test_data, 0.99)
Using the provided numeric input, typically a time series, calculate the spectral residual and output the saliency map for use in anomaly detection.
saliency_map(x, window = 3)
saliency_map(x, window = 3)
x |
Numeric vector. |
window |
Positive integer value. |
Numeric vector representing the saliency map values.
tmp <- ts(rnorm(12*6,10,2), start=c(2009, 1), end=c(2014, 12), frequency=12) saliency_map(tmp)
tmp <- ts(rnorm(12*6,10,2), start=c(2009, 1), end=c(2014, 12), frequency=12) saliency_map(tmp)