Class: Fluent::ChangeFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/change_finder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log, term, r) ⇒ ChangeFinder

Returns a new instance of ChangeFinder.



9
10
11
12
13
14
15
16
17
# File 'lib/fluent/plugin/change_finder.rb', line 9

def initialize(log, term, r)
  @log = log
  @term = term
  @r = r
  @data = []
  @mu = 0
  @sigma = 0
  @c = (0..@term - 1).map { |i| rand }
end

Instance Attribute Details

#logObject

Returns the value of attribute log.



7
8
9
# File 'lib/fluent/plugin/change_finder.rb', line 7

def log
  @log
end

#muObject (readonly)

Returns the value of attribute mu.



6
7
8
# File 'lib/fluent/plugin/change_finder.rb', line 6

def mu
  @mu
end

Instance Method Details

#marshal_dumpObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/fluent/plugin/change_finder.rb', line 19

def marshal_dump
  struct = OpenStruct.new
  struct.term = @term
  struct.r = @r
  struct.data = @data
  struct.mu = @mu
  struct.sigma = @sigma
  struct.c = @c
  struct
end

#marshal_load(struct) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/fluent/plugin/change_finder.rb', line 30

def marshal_load(struct)
  @term = struct.term
  @r = struct.r
  @data = struct.data
  @mu = struct.mu
  @sigma = struct.sigma
  @c = struct.c
end

#next(x) ⇒ Object



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/fluent/plugin/change_finder.rb', line 39

def next(x)
  len = @data.size

  # update @mu
  @mu = (1 - @r) * @mu + @r * x

  # update @c
  c = @sigma
  for j in 0..(@term - 1)
    if @data[len - 1 - j]
      @c[j] = (1 - @r) * @c[j] + @r * (x - @mu) * (@data[len - 1 - j] - @mu)
    end
  end

  cc = Matrix.zero(@term).to_a
  for j in 0..(@term - 1)
    for i in j..(@term - 1)
      cc[j][i] = cc[i][j] = @c[i - j]
    end
  end
  w = (Matrix.rows(cc).inv * Vector.elements(@c)).to_a
  xt = @data.each.with_index.inject(@mu) do |sum, (v, idx)|
    sum += w[idx] * (v - @mu)
  end
  @sigma = (1 - @r) * @sigma + @r * (x - xt) * (x - xt)

  @data.push x
  if @data.size > @term
    @data.shift
  end

  p = prob(xt, @sigma, x)
  s = score(p)
  log.debug "change_finder:#{Thread.current.object_id} x:#{x} xt:#{xt} p:#{p} s:#{s} term:#{@term} r:#{@r} data:#{@data} mu:#{@mu} sigma:#{@sigma} c:#{@c}"
  s
end

#prob(mu, sigma, v) ⇒ Object



76
77
78
79
80
# File 'lib/fluent/plugin/change_finder.rb', line 76

def prob(mu, sigma, v)
  return 0 if sigma.zero?

  Math.exp( - 0.5 * (v - mu) ** 2 / sigma) / ((2 * Math::PI) ** 0.5 * sigma ** 0.5)
end

#score(p) ⇒ Object



82
83
84
85
# File 'lib/fluent/plugin/change_finder.rb', line 82

def score(p)
  return 0 if p <= 0
  -Math.log(p)
end

#show_statusObject



93
94
95
# File 'lib/fluent/plugin/change_finder.rb', line 93

def show_status
  {:sigma => @sigma, :mu => @mu, :data => @data, :c => @c}
end

#smooth(size) ⇒ Object



87
88
89
90
91
# File 'lib/fluent/plugin/change_finder.rb', line 87

def smooth(size)
  _end = @data.size
  _begin = [_end - size, 0].max
  (_size = (_end - _begin)) == 0 ? 0.0 : @data.slice(_begin, _end).inject(:+).to_f / _size
end