Class: StreamStat::V
- Inherits:
-
Object
- Object
- StreamStat::V
- Defined in:
- lib/stream_stat.rb
Overview
Accumrator.
This holds :avg, :variance & :sd.
Instance Method Summary collapse
- #avg ⇒ Object
-
#initialize(length = 0, sum = 0, variance_sum = 0.0) ⇒ V
constructor
A new instance of V.
- #next(item) ⇒ Object
- #sd ⇒ Object
- #variance ⇒ Object
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
#avg ⇒ Object
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 |
#sd ⇒ Object
49 50 51 |
# File 'lib/stream_stat.rb', line 49 def sd Math.sqrt variance end |
#variance ⇒ Object
45 46 47 |
# File 'lib/stream_stat.rb', line 45 def variance @length.zero? ? 0.0 : @variance_sum / @length end |