Class: StreamStat::V

Inherits:
Object
  • Object
show all
Defined in:
lib/stream_stat.rb

Overview

Accumrator.

This holds :avg, :variance & :sd.

Instance Method Summary collapse

Constructor Details

#initialize(length = 0, sum = 0, variance_sum = 0.0) ⇒ V

Returns a new instance of V.



27
28
29
30
31
# File 'lib/stream_stat.rb', line 27

def initialize(length = 0, sum = 0, variance_sum = 0.0)
  @length = length
  @sum = sum
  @variance_sum = variance_sum
end

Instance Method Details

#avgObject



41
42
43
# File 'lib/stream_stat.rb', line 41

def avg
  @length.zero? ? 0.0 : @sum.to_f / @length
end

#next(item) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/stream_stat.rb', line 33

def next(item)
  self.class.new(
    @length + 1,
    @sum + item,
    next_variance_sum(avg, (@sum + item).to_f / (@length + 1), item)
  )
end

#sdObject



49
50
51
# File 'lib/stream_stat.rb', line 49

def sd
  Math.sqrt variance
end

#varianceObject



45
46
47
# File 'lib/stream_stat.rb', line 45

def variance
  @length.zero? ? 0.0 : @variance_sum / @length
end