Class: FlatKit::StatType::NumericalStats
- Inherits:
-
NominalStats
- Object
- FlatKit::StatType
- NominalStats
- FlatKit::StatType::NumericalStats
- Defined in:
- lib/flat_kit/stat_type/numerical_stats.rb
Overview
Internal: Stats object to 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
-
#max ⇒ Object
readonly
A list of the available stats.
-
#min ⇒ Object
readonly
A list of the available stats.
-
#sum ⇒ Object
readonly
A list of the available stats.
-
#sumsq ⇒ Object
readonly
A list of the available stats.
Attributes inherited from NominalStats
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(collecting_frequencies: false) ⇒ NumericalStats
constructor
A new instance of NumericalStats.
-
#mean ⇒ Object
call-seq: stat.mean -> Float.
-
#rate ⇒ Object
call-seq: stat.rate -> Float.
-
#stddev ⇒ Object
call-seq: stat.stddev -> Float.
-
#update(value) ⇒ Object
call-seq: stat.update( val ) -> val.
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.
46 47 48 49 50 51 52 |
# File 'lib/flat_kit/stat_type/numerical_stats.rb', line 46 def initialize(collecting_frequencies: false) super @min = Float::INFINITY @max = -Float::INFINITY @sum = 0.0 @sumsq = 0.0 end |
Instance Attribute Details
#max ⇒ Object (readonly)
A list of the available stats
36 37 38 |
# File 'lib/flat_kit/stat_type/numerical_stats.rb', line 36 def max @max end |
#min ⇒ Object (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 |
#sum ⇒ Object (readonly)
A list of the available stats
36 37 38 |
# File 'lib/flat_kit/stat_type/numerical_stats.rb', line 36 def sum @sum end |
#sumsq ⇒ Object (readonly)
A list of the available stats
36 37 38 |
# File 'lib/flat_kit/stat_type/numerical_stats.rb', line 36 def sumsq @sumsq end |
Class Method Details
.all_stats ⇒ Object
42 43 44 |
# File 'lib/flat_kit/stat_type/numerical_stats.rb', line 42 def self.all_stats @all_stats ||= %w[count max mean min mode rate stddev sum sumsq unique_count unique_values] end |
.default_stats ⇒ Object
38 39 40 |
# File 'lib/flat_kit/stat_type/numerical_stats.rb', line 38 def self.default_stats @default_stats ||= %w[count max mean min rate stddev sum sumsq] end |
Instance Method Details
#mean ⇒ Object
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;
80 81 82 83 84 |
# File 'lib/flat_kit/stat_type/numerical_stats.rb', line 80 def mean return 0.0 if @count.zero? @sum / @count end |
#rate ⇒ Object
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.
99 100 101 102 103 |
# File 'lib/flat_kit/stat_type/numerical_stats.rb', line 99 def rate return 0.0 if @sum.zero? @count / @sum end |
#stddev ⇒ Object
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;
113 114 115 116 117 |
# File 'lib/flat_kit/stat_type/numerical_stats.rb', line 113 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.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/flat_kit/stat_type/numerical_stats.rb', line 59 def update(value) @mutex.synchronize do @min = [value, @min].min @max = [value, @max].max @count += 1 @sum += value @sumsq += (value * value) # from Nomnial update @frequencies[value] += 1 if @collecting_frequencies end value end |