class Benelux::Stats::Calculator
Based on Mongrel::Stats, Copyright © 2005 Zed A. Shaw
Public Class Methods
new()
click to toggle source
# File lib/benelux/stats.rb, line 137 def initialize reset end
Public Instance Methods
+(other)
click to toggle source
# File lib/benelux/stats.rb, line 141 def +(other) c = Calculator.new c.merge! self c.merge! other c end
==(other)
click to toggle source
# File lib/benelux/stats.rb, line 231 def ==(other) return false unless self.class == other.class a=([@sum, @min, @max, @n, @sumsq] - [other.sum, other.min, other.max, other.n, other.sumsq]) a.empty? end
average()
click to toggle source
# File lib/benelux/stats.rb, line 216 def average; avg() end
avg()
click to toggle source
Calculates and returns the mean for the data passed so far.
# File lib/benelux/stats.rb, line 218 def avg; return 0.0 unless @n > 0; @sum / @n; end
dump(msg = "", out=STDERR)
click to toggle source
Dump this Stats object with an optional additional message.
# File lib/benelux/stats.rb, line 193 def dump(msg = "", out=STDERR) out.puts "#{msg}: #{self.report}" end
first_tick()
click to toggle source
# File lib/benelux/stats.rb, line 185 def first_tick() @last_time = Time.now end
inspect()
click to toggle source
# File lib/benelux/stats.rb, line 204 def inspect v = [ mean, @n, @sum, @sumsq, sd, @min, @max, tags] "%.4f: n=%.4f sum=%.4f sumsq=%.4f sd=%.4f min=%.4f max=%.4f %s" % v end
mean()
click to toggle source
NOTE: This is an alias for average. We don't store values so we can't return the actual mean
# File lib/benelux/stats.rb, line 215 def mean; avg() end
merge!(other)
click to toggle source
# File lib/benelux/stats.rb, line 158 def merge!(other) return self if other.n == 0 if @n == 0 @min, @max = other.min, other.max else @min = other.min if other.min < @min @max = other.max if other.max > @max end @sum += other.sum @sumsq += other.sumsq @n += other.n self end
report()
click to toggle source
Returns a common display (used by dump)
# File lib/benelux/stats.rb, line 198 def report v = [mean, @n, @sum, @sumsq, sd, @min, @max] t = '%8d(N) %10.4f(SUM) %8.4f(SUMSQ) %8.4f(SD) %8.4f(MIN) %8.4f(MAX)' ('%0.4f: ' << t) % v end
reset()
click to toggle source
Resets the internal counters so you can start sampling again.
# File lib/benelux/stats.rb, line 149 def reset @n, @sum, @sumsq = 0.0, 0.0, 0.0 @min, @max = 0.0, 0.0 end
sample(s)
click to toggle source
Adds a sampling to the calculations.
# File lib/benelux/stats.rb, line 173 def sample(s) @sum += s @sumsq += s * s if @n == 0 @min = @max = s else @min = s if @min > s @max = s if @max < s end @n+=1 end
samples(*args)
click to toggle source
# File lib/benelux/stats.rb, line 154 def samples(*args) args.flatten.each { |s| sample(s) } end
sd()
click to toggle source
Calculates the standard deviation of the data so far.
# File lib/benelux/stats.rb, line 221 def sd return 0.0 if @n <= 1 # (sqrt( ((s).sumsq - ( (s).sum * (s).sum / (s).n)) / ((s).n-1) )) begin return Math.sqrt( (@sumsq - (@sum * @sum / @n)) / (@n-1) ) rescue Errno::EDOM return 0.0 end end
tick()
click to toggle source
# File lib/benelux/stats.rb, line 186 def tick tick_time = Time.now sample(tick_time - @last_time) @last_time = tick_time end
to_f()
click to toggle source
# File lib/benelux/stats.rb, line 210 def to_f; mean.to_f; end
to_i()
click to toggle source
# File lib/benelux/stats.rb, line 211 def to_i; mean.to_i; end
to_s()
click to toggle source
# File lib/benelux/stats.rb, line 209 def to_s; mean.to_s; end