ContextualAnomalyDetector: Contextual Anomaly Detector - Open Source (CAD)

View source: R/ContextualAD.R

ContextualAnomalyDetectorR Documentation

Contextual Anomaly Detector - Open Source (CAD)

Description

ContextualAnomalyDetector calculates the anomaly score of a dataset using the notion of contexts conformed by facts and provides probabilistic abnormality scores.

Usage

ContextualAnomalyDetector(
  data,
  rest.period = max(min(150, round(length(data) * 0.03), 1)),
  max.left.semicontexts = 7,
  max.active.neurons = 15,
  num.norm.value.bits = 3,
  base.threshold = 0.75,
  min.value = min(data, na.rm = T),
  max.value = max(data, na.rm = T),
  python.object = NULL,
  lib = 0
)

Arguments

data

Numerical vector with training and test dataset.

rest.period

Training period after an anomaly.

max.left.semicontexts

Number of semicontexts that should be maintained in memory.

max.active.neurons

Number of neurons of the model.

num.norm.value.bits

Granularity of the transformation into discrete values

base.threshold

Threshold to be considered an anomaly.

min.value

Minimum expected value.

max.value

Maximum expected value.

python.object

Python object for incremental processing.

lib

0 to run the original python script, 1 to get the same results on all operating systems.

Details

data must be a numerical vector without NA values. threshold must be a numeric value between 0 and 1. If the anomaly score obtained for an observation is greater than the threshold, the observation will be considered abnormal. Requires hashlib (included in python installation) and bencode-python3 (which can be installed using pip) python libraries.

Value

List

result

Data frame with anomaly.score and is.anomaly comparing the anomaly score with base.threshold.

python.object

ContextualAnomalyDetector Python object used in online anomaly detection

References

Smirnov, M. (2018). CAD: Contextual Anomaly Detector. https://github.com/smirmik/CAD

Examples

## Generate data
set.seed(100)
n <- 200
x <- sample(1:100, n, replace = TRUE)
x[70:90] <- sample(110:115, 21, replace = TRUE)
x[25] <- 200
x[150] <- 170
df <- data.frame(timestamp = 1:n, value = x)

## Calculate anomalies

   result <- ContextualAnomalyDetector(data = df$value, rest.period = 10, base.threshold = 0.9)

   ## Plot results
   res <- cbind(df, result$result)
   PlotDetections(res, title = "CAD_OSE ANOMALY DETECTOR")




alaineiturria/otsad documentation built on Jan. 12, 2023, 12:26 p.m.