Class: Core::Stats::Accumulator
Overview
accumulate count, sum, and sum of squares to support calculation of mean and population standard deviation note: use the first value as a zero offset to reduce floating point precision errors
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
Instance Method Summary collapse
- #add_value(value) ⇒ Object
-
#initialize ⇒ Accumulator
constructor
A new instance of Accumulator.
- #mean ⇒ Object
- #stddev ⇒ Object
Constructor Details
#initialize ⇒ Accumulator
Returns a new instance of Accumulator.
14 15 16 17 18 |
# File 'lib/svcbase/stats.rb', line 14 def initialize @count = 0 @sum = 0 @sumsqr = 0 end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
12 13 14 |
# File 'lib/svcbase/stats.rb', line 12 def count @count end |
#max ⇒ Object (readonly)
Returns the value of attribute max.
12 13 14 |
# File 'lib/svcbase/stats.rb', line 12 def max @max end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
12 13 14 |
# File 'lib/svcbase/stats.rb', line 12 def min @min end |
Instance Method Details
#add_value(value) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/svcbase/stats.rb', line 20 def add_value(value) @offset ||= value @max = value if @max.nil? || value > @max @min = value if @min.nil? || value < @max offset_val = value - @offset @sum += offset_val @sumsqr += offset_val**2 @count += 1 end |
#mean ⇒ Object
30 31 32 |
# File 'lib/svcbase/stats.rb', line 30 def mean @offset + @sum / @count end |
#stddev ⇒ Object
34 35 36 |
# File 'lib/svcbase/stats.rb', line 34 def stddev Math.sqrt((@sumsqr - @sum**2 / @count) / @count) end |