Class: Core::Stats::Accumulator

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

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

Instance Method Summary collapse

Constructor Details

#initializeAccumulator

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

#countObject (readonly)

Returns the value of attribute count.



12
13
14
# File 'lib/svcbase/stats.rb', line 12

def count
  @count
end

#maxObject (readonly)

Returns the value of attribute max.



12
13
14
# File 'lib/svcbase/stats.rb', line 12

def max
  @max
end

#minObject (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

#meanObject



30
31
32
# File 'lib/svcbase/stats.rb', line 30

def mean
  @offset + @sum / @count
end

#stddevObject



34
35
36
# File 'lib/svcbase/stats.rb', line 34

def stddev
  Math.sqrt((@sumsqr - @sum**2 / @count) / @count)
end