Skip to content

Instantly share code, notes, and snippets.

@cyrilou242
Last active February 27, 2023 20:11
Show Gist options
  • Save cyrilou242/a979dd8d01fca62b14b1fced36ea6311 to your computer and use it in GitHub Desktop.
Save cyrilou242/a979dd8d01fca62b14b1fced36ea6311 to your computer and use it in GitHub Desktop.
Online detection with tsoutliers. Does not work?
# R version: 4.2.2
# forecast version 8.20
library(forecast)
# create repeated pattern of 1 2 3 4 5 6 7
values <- c()
for (i in seq(0,62) ) {
values <- c(values, 1+ i%%7)
}
# add anomalies
# 10 instead of 2
values[30] <- 10
# 1 instead of 6
values[48] <- 1;
x <- ts(values, frequency=7)
print(x)
# OFFLINE detection
res <- tsoutliers(x, iterate=1)
print(res)
# --> returns 30 and 48 as anomalies --> works fine
# LETS SIMULATE AN ONLINE PROCESS
# in a (simple) online process, each new element is processed when it becomes available
# at each new point, anomaly detection is run
# before the first anomaly happens
tsoutliers(ts(x[1:29], frequency=7), iterate=1)
# --> returns no anomaly - correct
# anomaly appears at index 30
tsoutliers(ts(x[1:30], frequency=7), iterate=1)
# --> returns no anomaly --> missed the true anomaly
tsoutliers(ts(x[1:31], frequency=7), iterate=1)
# --> returns 24 25 26 27 28 29 30 31 --> find the anomaly but lots of false positives, unstable
tsoutliers(ts(x[1:32], frequency=7), iterate=1)
# --> returns 25 26 27 28 29 30 31 32 --> find the anomaly but lots of false positives, unstable
tsoutliers(ts(x[1:33], frequency=7), iterate=1)
# --> returns no anomaly --> suddenly became blind?? unstable
tsoutliers(ts(x[1:34], frequency=7), iterate=1)
# --> returns 29 30 31 --> still 2 false positives
tsoutliers(ts(x[1:35], frequency=7), iterate=1)
# --> returns 30 --> finally correct
tsoutliers(ts(x[1:36], frequency=7), iterate=1)
# --> returns 30 --> still correct
tsoutliers(ts(x[1:37], frequency=7), iterate=1)
# --> returns 23 --> ?? very unstable
tsoutliers(ts(x[1:38], frequency=7), iterate=1)
# --> returns 30 --> back to correct
# seems correct for 39, 40, etc... second anomaly not tested
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment