Class: Fluent::ChangeFinder
- Inherits:
-
Object
- Object
- Fluent::ChangeFinder
- Defined in:
- lib/fluent/plugin/change_finder.rb
Instance Method Summary collapse
-
#initialize(term, r) ⇒ ChangeFinder
constructor
A new instance of ChangeFinder.
- #next(x) ⇒ Object
- #prob(mu, sigma, v) ⇒ Object
- #score(p) ⇒ Object
- #show_status ⇒ Object
- #smooth(size) ⇒ Object
Constructor Details
#initialize(term, r) ⇒ ChangeFinder
Returns a new instance of ChangeFinder.
6 7 8 9 10 11 12 13 |
# File 'lib/fluent/plugin/change_finder.rb', line 6 def initialize(term, r) @term = term @r = r @data = [] @mu = 0 @sigma = 0 @c = (0..@term - 1).map { |i| rand } end |
Instance Method Details
#next(x) ⇒ Object
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 |
# File 'lib/fluent/plugin/change_finder.rb', line 15 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
52 53 54 55 56 |
# File 'lib/fluent/plugin/change_finder.rb', line 52 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
58 59 60 61 |
# File 'lib/fluent/plugin/change_finder.rb', line 58 def score(p) return 0 if p <= 0 -Math.log(p) end |
#show_status ⇒ Object
69 70 71 |
# File 'lib/fluent/plugin/change_finder.rb', line 69 def show_status {:sigma => @sigma, :mu => @mu, :data => @data, :c => @c} end |
#smooth(size) ⇒ Object
63 64 65 66 67 |
# File 'lib/fluent/plugin/change_finder.rb', line 63 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 |