forked from alaineiturria/otsad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
159 lines (111 loc) · 4.27 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
---
output:
md_document:
variant: markdown_github
always_allow_html: yes
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# otsad
*Online Time Series Anomaly Detectors*
This package provides anomaly detectors in the context of online time series and their evaluation with the Numenta score.
## Installation
### Dependencies
CAD-OSE algorithm is implemented in Python. It uses [bencode](https://pypi.org/project/bencode-python3/) library in the hashing step. This dependency can be installed with the Python package manager *pip*.
```sh
$ sudo pip install bencode-python3
```
### otsad package
You can install the released version of otsad from [CRAN](https://CRAN.R-project.org) with:
```{r installation, eval=FALSE}
# Get the released version from CRAN
install.packages("otsad")
# Get the latest development version from GitHub
devtools::install_github("alaineiturria/otsad")
```
## Most useful functions
### Detectors
* **PEWMA**
+ Offline: `CpPewma`
+ Online: `IpPewma`
* **SD-EWMA**
+ Offline: `CpSdEwma`
+ Online: `IpSdEwma`
* **TSSD-EWMA**
+ Offline: `CpTsSdEwma`
+ Online: `IpTsSdEwma`
* **KNN-ICAD**
+ Offline: `CpKnnCad(ncm.type = "ICAD")`
+ Online: `IpKnnCad(ncm.type = "ICAD")`
* **KNN-LDCD**
+ Offline `CpKnnCad(ncm.type = "LDCD")`
+ Online: `IpKnnCad(ncm.type = "LDCD")`
* **CAD-OSE**
+ Offline and Online: `ContextualAnomalyDetector`
* **EORELM-AD**
+ Offline and Online: `EorelmAD`
### NAB score
* Get score: GetDetectorScore
* Normalize score: `NormalizeScore` + `GetNullAndPerfectScores`
### False Positve Reduction
* Offline and Online: `ReduceAnomalies`
### Static or interactive visualizations
* Offline: `PlotDetections`
<div style="background-color: #cceeff; padding: 10px;">
## From prediction to anomaly detection framework
It is developed a framework that eases the adoption of any online time series prediction algorithm into an anomaly detection algorithm.
The framework is composed of two main components, one for online data normalization and the other for streaming anomaly scoring based on prediction error. The procedure to adapt an online prediction model into anomaly detection using this framework is shown in the following Figure. First, if the prediction model requires it, the current data point is normalized incrementally. Then, the normalized data point is used to train and predict the expected value using the chosen prediction model. After that, to compute the outlierness, the prediction error is calculated and passed to the outlier scoring function.
![alt text](https://github.com/alaineiturria/otsad/blob/master/vignettes/AD-FRAMEWORK.jpg)
### Online normalization
* **Dynamic normalization**
+ Online: `DinamycNormalizer`
* **Window normalization**
+ Online: `WindowNormalizer`
* **Adaptive normalization**
+ Online: `AdaptiveNormalizer`
* **Adaptive normalization2**
+ Online: `AdaptiveNormalizer2`
### Outlier scoring
* **Anomaly likelihood**
+ Online: `AnomalyLikelihoodScorer`
* **Dynamic threshold**
+ Online: `DynamicThresholdScorer`
* **Sigma scoring**
+ Online: `SigmaScorer`
* **Dynamic sigma scoring**
+ Online: `DynamicSigmaScorer`
***NOTE:*** *As usual in R, the documentation pages for each function can be loaded from the command line with the commands ? or help:*
``` r
?CpSdEwma
help(CpSdEwma)
```
</div>
## Example
This is a basic example of the use of otsad package:
```{r example}
library(otsad)
## basic example code
# Generate data
set.seed(100)
n <- 500
x <- sample(1:100, n, replace = TRUE)
x[70:90] <- sample(110:115, 21, replace = TRUE) # distributional shift
x[25] <- 200 # abrupt transient anomaly
x[320] <- 170 # abrupt transient anomaly
df <- data.frame(timestamp = 1:n, value = x)
# Apply classic processing SD-EWMA detector
result <- CpSdEwma(data = df$value, n.train = 5, threshold = 0.01, l = 3)
```
```{r plot.Anomaly}
res <- cbind(df, result)
PlotDetections(res, title = "SD-EWMA ANOMALY DETECTOR", return.ggplot = TRUE)
```
See plotly [interactive graph](https://alaineiturria.github.io/otsad/)
For more details, see otsad documentation and vignettes.