Module: AnomalyDetection

Defined in:
lib/anomaly_detection.rb,
lib/anomaly_detection/version.rb

Constant Summary collapse

VERSION =
"0.2.2"

Class Method Summary collapse

Class Method Details

.detect(series, period:, max_anoms: 0.1, alpha: 0.05, direction: "both", plot: false, verbose: false) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/anomaly_detection.rb', line 9

def detect(series, period:, max_anoms: 0.1, alpha: 0.05, direction: "both", plot: false, verbose: false)
  if period == :auto
    period = determine_period(series)
    puts "Set period to #{period}" if verbose
  elsif period.nil?
    period = 1
  end

  raise ArgumentError, "series must contain at least 2 periods" if series.size < period * 2

  if series.is_a?(Hash)
    sorted = series.sort_by { |k, _| k }
    x = sorted.map(&:last)
  else
    x = series
  end

  # flush Ruby output since std::endl flushes C++ output
  $stdout.flush if verbose

  res = _detect(x, period, max_anoms, alpha, direction, verbose)
  res.map! { |i| sorted[i][0] } if series.is_a?(Hash)
  res
end

.determine_period(series) ⇒ Object

determine period based on time keys (experimental) in future, could use an approach that looks at values like stats.stackexchange.com/a/1214



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
# File 'lib/anomaly_detection.rb', line 79

def determine_period(series)
  unless series.is_a?(Hash)
    raise ArgumentError, "series must be a hash for :auto period"
  end

  times = series.keys.map(&:to_time)

  second = times.all? { |t| t.nsec == 0 }
  minute = second && times.all? { |t| t.sec == 0 }
  hour = minute && times.all? { |t| t.min == 0 }
  day = hour && times.all? { |t| t.hour == 0 }
  week = day && times.map { |k| k.wday }.uniq.size == 1
  month = day && times.all? { |k| k.day == 1 }
  quarter = month && times.all? { |k| k.month % 3 == 1 }
  year = quarter && times.all? { |k| k.month == 1 }

  period =
    if year
      1
    elsif quarter
      4
    elsif month
      12
    elsif week
      52
    elsif day
      7
    elsif hour
      24 # or 24 * 7
    elsif minute
      60 # or 60 * 24
    elsif second
      60 # or 60 * 60
    end

  if series.size < period * 2
    1
  else
    period
  end
end

.plot(series, anomalies) ⇒ Object

TODO add tooltips



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
# File 'lib/anomaly_detection.rb', line 35

def plot(series, anomalies)
  require "vega"

  data =
    if series.is_a?(Hash)
      series.map { |k, v| {x: iso8601(k), y: v, anomaly: anomalies.include?(k)} }
    else
      series.map.with_index { |v, i| {x: i, y: v, anomaly: anomalies.include?(i)} }
    end

  if series.is_a?(Hash)
    x = {field: "x", type: "temporal"}
    x["scale"] = {type: "utc"} if series.keys.first.is_a?(Date)
  else
    x = {field: "x", type: "quantitative"}
  end

  Vega.lite
    .data(data)
    .layer([
      {
        mark: {type: "line"},
        encoding: {
          x: x,
          y: {field: "y", type: "quantitative", scale: {zero: false}},
          color: {value: "#fa9088"}
        }
      },
      {
        transform: [{"filter": "datum.anomaly == true"}],
        mark: {type: "point", size: 200},
        encoding: {
          x: x,
          y: {field: "y", type: "quantitative"},
          color: {value: "#19c7ca"}
        }
      }
    ])
    .config(axis: {title: nil, labelFontSize: 12})
end