Class: DRbDump::Statistic

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

Overview

Stores the minimum, maximum, mean, count and standard deviation for a set of values but not the values themselves.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStatistic

:nodoc:



27
28
29
30
31
32
33
# File 'lib/drbdump/statistic.rb', line 27

def initialize # :nodoc:
  @M_2   = 0.0
  @count = 0
  @max   = -Float::INFINITY
  @mean  = 0.0
  @min   = Float::INFINITY
end

Instance Attribute Details

#countObject (readonly)

The number of items in the set



10
11
12
# File 'lib/drbdump/statistic.rb', line 10

def count
  @count
end

#maxObject (readonly) Also known as: maximum

The maximum value added



15
16
17
# File 'lib/drbdump/statistic.rb', line 15

def max
  @max
end

#meanObject (readonly) Also known as: average

The mean of all values



20
21
22
# File 'lib/drbdump/statistic.rb', line 20

def mean
  @mean
end

#minObject (readonly) Also known as: minimum

The minimum value added



25
26
27
# File 'lib/drbdump/statistic.rb', line 25

def min
  @min
end

Instance Method Details

#add(value) ⇒ Object

Adds value to the set of values. Returns the number of values.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/drbdump/statistic.rb', line 38

def add value
  @min = value if value < @min
  @max = value if value > @max
  @count += 1

  delta  = value - @mean
  @mean += delta / @count
  @M_2  += delta * (value - @mean)

  @count
end

#sample_varianceObject

The sample variance for all values



68
69
70
71
72
# File 'lib/drbdump/statistic.rb', line 68

def sample_variance
  sv = @M_2 / (@count - 1)
  return 0.0 if sv.nan?
  sv
end

#standard_deviationObject

The standard deviation of all values



77
78
79
# File 'lib/drbdump/statistic.rb', line 77

def standard_deviation
  Math.sqrt sample_variance
end

#to_aObject

An array containing the number of values in the set, the mean and the standard deviation



85
86
87
# File 'lib/drbdump/statistic.rb', line 85

def to_a
  [@count, @min, @mean, @max, standard_deviation]
end