Class: Benelux::Stats::Calculator

Inherits:
Storable
  • Object
show all
Includes:
Selectable::Object
Defined in:
lib/benelux/stats.rb

Overview

Based on Mongrel::Stats, Copyright © 2005 Zed A. Shaw

Instance Attribute Summary

Attributes included from Selectable::Object

#tags

Instance Method Summary collapse

Methods included from Selectable::Object

#add_tags, #add_tags_quick, #init_tags!, #remove_tags, #tag_values

Constructor Details

#initializeCalculator

Returns a new instance of Calculator.



137
138
139
# File 'lib/benelux/stats.rb', line 137

def initialize
  reset
end

Instance Method Details

#+(other) ⇒ Object



141
142
143
144
145
146
# File 'lib/benelux/stats.rb', line 141

def +(other)
  c = Calculator.new
  c.merge! self
  c.merge! other
  c
end

#==(other) ⇒ Object



231
232
233
234
235
236
# 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

#averageObject



216
# File 'lib/benelux/stats.rb', line 216

def average; avg() end

#avgObject

Calculates and returns the mean for the data passed so far.



218
# File 'lib/benelux/stats.rb', line 218

def avg; return 0.0 unless @n > 0; @sum / @n; end

#dump(msg = "", out = STDERR) ⇒ Object

Dump this Stats object with an optional additional message.



193
194
195
# File 'lib/benelux/stats.rb', line 193

def dump(msg = "", out=STDERR)
  out.puts "#{msg}: #{self.report}"
end

#first_tickObject



185
# File 'lib/benelux/stats.rb', line 185

def first_tick() @last_time = Time.now end

#inspectObject



204
205
206
207
# 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

#meanObject

NOTE: This is an alias for average. We don’t store values so we can’t return the actual mean



215
# File 'lib/benelux/stats.rb', line 215

def mean;    avg() end

#merge!(other) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
# 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

#reportObject

Returns a common display (used by dump)



198
199
200
201
202
# File 'lib/benelux/stats.rb', line 198

def report
  v = [mean, @n, @sum, @sumsq, sd, @min, @max]
  t = %q'%8d(N) %10.4f(SUM) %8.4f(SUMSQ) %8.4f(SD) %8.4f(MIN) %8.4f(MAX)'
  ('%0.4f: ' << t) % v
end

#resetObject

Resets the internal counters so you can start sampling again.



149
150
151
152
# 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) ⇒ Object

Adds a sampling to the calculations.



173
174
175
176
177
178
179
180
181
182
183
# 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) ⇒ Object



154
155
156
# File 'lib/benelux/stats.rb', line 154

def samples(*args)  
  args.flatten.each { |s| sample(s) }
end

#sdObject

Calculates the standard deviation of the data so far.



221
222
223
224
225
226
227
228
229
# 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

#tickObject



186
187
188
189
190
# File 'lib/benelux/stats.rb', line 186

def tick
  tick_time = Time.now
  sample(tick_time - @last_time)
  @last_time = tick_time
end

#to_fObject



210
# File 'lib/benelux/stats.rb', line 210

def to_f; mean.to_f; end

#to_iObject



211
# File 'lib/benelux/stats.rb', line 211

def to_i; mean.to_i; end

#to_sObject



209
# File 'lib/benelux/stats.rb', line 209

def to_s; mean.to_s; end