Class: FlatKit::StatType::NumericalStats

Inherits:
NominalStats show all
Defined in:
lib/flat_kit/stat_type/numerical_stats.rb

Overview

Stats object will keep track of the min, max, count, sum and sumsq and when you want you may also retrieve the mean, stddev and rate.

this contrived example shows getting a list of all the files in a directory and running stats on file sizes.

s = FlatKit::Stats.new
dir = ARGV.shift || Dir.pwd
Dir.entries( dir ).each do |entry|
  fs = File.stat( entry )
  if fs.file? then
    s.update( fs.size )
   end
end

%w[ count min max mean sum stddev rate ].each do |m|
  puts "#{m.rjust(6)} : #{s.send( m ) }"
end

Instance Attribute Summary collapse

Attributes inherited from NominalStats

#count

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NominalStats

#collected_stats, #frequencies, #mode, #unique_count, #unique_values

Methods inherited from FlatKit::StatType

#collected_stats, for, nominal_types, numerical_types, ordinal_types, #to_hash, #to_json

Constructor Details

#initialize(collecting_frequencies: false) ⇒ NumericalStats

Returns a new instance of NumericalStats.



49
50
51
52
53
54
55
# File 'lib/flat_kit/stat_type/numerical_stats.rb', line 49

def initialize(collecting_frequencies: false)
  super
  @min = Float::INFINITY
  @max = -Float::INFINITY
  @sum = 0.0
  @sumsq = 0.0
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



37
38
39
# File 'lib/flat_kit/stat_type/numerical_stats.rb', line 37

def max
  @max
end

#minObject (readonly)

A list of the available stats



36
37
38
# File 'lib/flat_kit/stat_type/numerical_stats.rb', line 36

def min
  @min
end

#sumObject (readonly)

Returns the value of attribute sum.



38
39
40
# File 'lib/flat_kit/stat_type/numerical_stats.rb', line 38

def sum
  @sum
end

#sumsqObject (readonly)

Returns the value of attribute sumsq.



39
40
41
# File 'lib/flat_kit/stat_type/numerical_stats.rb', line 39

def sumsq
  @sumsq
end

Class Method Details

.all_statsObject



45
46
47
# File 'lib/flat_kit/stat_type/numerical_stats.rb', line 45

def self.all_stats
  @all_stats ||= %w[ count max mean min mode rate stddev sum sumsq unique_count unique_values ]
end

.default_statsObject



41
42
43
# File 'lib/flat_kit/stat_type/numerical_stats.rb', line 41

def self.default_stats
  @default_stats ||= %w[ count max mean min rate stddev sum sumsq ]
end

Instance Method Details

#meanObject

call-seq:

stat.mean -> Float

Return the arithmetic mean of the values put into the Stats object. If no values have passed through the stats object then 0.0 is returned;



83
84
85
86
# File 'lib/flat_kit/stat_type/numerical_stats.rb', line 83

def mean
  return 0.0 if @count.zero?
  return @sum / @count
end

#rateObject

call-seq:

stat.rate -> Float

Return the count divided by sum.

In many cases when Stats#update( value ) is called, the value is a unit of time, typically seconds or microseconds. #rate is a convenience for those times. In this case, where value is a unit if time, then count divided by sum is a useful value, i.e. something per unit of time.

In the case where value is a non-time related value, then the value returned by rate is not really useful.



101
102
103
104
# File 'lib/flat_kit/stat_type/numerical_stats.rb', line 101

def rate
  return 0.0 if @sum.zero?
  return @count / @sum
end

#stddevObject

call-seq:

stat.stddev -> Float

Return the standard deviation of all the values that have passed through the Stats object. The standard deviation has no meaning unless the count is > 1, therefore if the current stat.count is < 1 then 0.0 will be returned;



114
115
116
117
# File 'lib/flat_kit/stat_type/numerical_stats.rb', line 114

def stddev
  return 0.0 unless @count > 1
  Math.sqrt((@sumsq - ((@sum * @sum)/@count)) / (@count - 1))
end

#update(value) ⇒ Object

call-seq:

stat.update( val ) -> val

Update the running stats with the new value. Return the input value.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/flat_kit/stat_type/numerical_stats.rb', line 62

def update(value)
  @mutex.synchronize do
    @min = (value < @min) ? value : @min
    @max = (value > @max) ? value : @max

    @count += 1
    @sum   += value
    @sumsq += (value * value)

    # from Nomnial update
    @frequencies[value] += 1 if @collecting_frequencies
  end

  return value
end