alaineiturria / otsad

Online Time Series Anomaly Detectors
28 stars 4 forks source link

Suggest adding a zero variance check on input #1

Closed leungi closed 4 years ago

leungi commented 5 years ago

Issue: when data has zero variance, CpSdEwma() output has more rows than input

Suggestion:

if ( var(data) == 0 ) {  
      res <- data.frame(is.anomaly = rep(0, length(data)), lcl = data, ucl = data, 
                                  stringsAsFactors = FALSE)
   }
library(otsad)
#> Warning: package 'otsad' was built under R version 3.5.3
# when data has non-zero variance, it's OK
vec <- rep(1:5, times = 5)
res <- CpSdEwma(data = vec, n.train = 5, threshold = 0.01, l = 3)
nrow(res) == length(vec)
#> [1] TRUE

# when data has zero variance, CpSdEwma() output has more rows than input
vec <- rep(0, 25)
res <- CpSdEwma(data = vec, n.train = 5, threshold = 0.01, l = 3)
nrow(res) == length(vec)
#> [1] FALSE

# adding just one different value to make data non-zero variance works
vec <- c(0.1, rep(0, 25))
res <- CpSdEwma(data = vec, n.train = 5, threshold = 0.01, l = 3)
nrow(res) == length(vec)
#> [1] TRUE

Created on 2019-06-20 by the reprex package (v0.2.1)

leungi commented 5 years ago

Thanks for the useful and light package! 👍

alaineiturria commented 4 years ago

Thanks for your observation and suggestion. The bug has been fixed and a new version is already available in CRAN.