class FlatKit::StatType::NumericalStats

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

Attributes

max[R]
min[R]

A list of the available stats

sum[R]
sumsq[R]

Public Class Methods

all_stats() click to toggle source
# 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_stats() click to toggle source
# 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
new(collecting_frequencies: false) click to toggle source
Calls superclass method FlatKit::StatType::NominalStats::new
# 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

Public Instance Methods

mean → Float click to toggle source

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;

# File lib/flat_kit/stat_type/numerical_stats.rb, line 83
def mean
  return 0.0 if @count.zero?
  return @sum / @count
end
rate → Float click to toggle source

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.

# File lib/flat_kit/stat_type/numerical_stats.rb, line 101
def rate
  return 0.0 if @sum.zero?
  return @count / @sum
end
stddev → Float click to toggle source

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;

# 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( val ) → val click to toggle source

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

# 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