Class Benelux::Stats::Calculator

  1. lib/benelux/stats.rb
Parent: Storable

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

Included modules

  1. Selectable::Object

Public class methods

new ()
[show source]
     # File lib/benelux/stats.rb, line 137
137:       def initialize
138:         reset
139:       end

Public instance methods

+ (other)
[show source]
     # File lib/benelux/stats.rb, line 141
141:       def +(other)
142:         c = Calculator.new
143:         c.merge! self
144:         c.merge! other
145:         c
146:       end
== (other)
[show source]
     # File lib/benelux/stats.rb, line 231
231:       def ==(other)
232:         return false unless self.class == other.class
233:         a=([@sum, @min, @max, @n, @sumsq] - 
234:            [other.sum, other.min, other.max, other.n, other.sumsq])
235:         a.empty?
236:       end
average ()
[show source]
     # File lib/benelux/stats.rb, line 216
216:       def average; avg() end
avg ()

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

[show source]
     # File lib/benelux/stats.rb, line 218
218:       def avg; return 0.0 unless @n > 0; @sum / @n; end
dump (msg = "", out=STDERR)

Dump this Stats object with an optional additional message.

[show source]
     # File lib/benelux/stats.rb, line 193
193:       def dump(msg = "", out=STDERR)
194:         out.puts "#{msg}: #{self.report}"
195:       end
first_tick ()
[show source]
     # File lib/benelux/stats.rb, line 185
185:       def first_tick() @last_time = Time.now end
inspect ()
[show source]
     # File lib/benelux/stats.rb, line 204
204:       def inspect
205:         v = [ mean, @n, @sum, @sumsq, sd, @min, @max, tags]
206:         "%.4f: n=%.4f sum=%.4f sumsq=%.4f sd=%.4f min=%.4f max=%.4f %s" % v
207:       end
mean ()

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

[show source]
     # File lib/benelux/stats.rb, line 215
215:       def mean;    avg() end
merge! (other)
[show source]
     # File lib/benelux/stats.rb, line 158
158:       def merge!(other)
159:         return self if other.n == 0
160:         if @n == 0
161:           @min, @max = other.min, other.max
162:         else
163:           @min = other.min if other.min < @min
164:           @max = other.max if other.max > @max
165:         end
166:         @sum += other.sum
167:         @sumsq += other.sumsq
168:         @n += other.n
169:         self
170:       end
report ()

Returns a common display (used by dump)

[show source]
     # File lib/benelux/stats.rb, line 198
198:       def report
199:         v = [mean, @n, @sum, @sumsq, sd, @min, @max]
200:         t = %q'%8d(N) %10.4f(SUM) %8.4f(SUMSQ) %8.4f(SD) %8.4f(MIN) %8.4f(MAX)'
201:         ('%0.4f: ' << t) % v
202:       end
reset ()

Resets the internal counters so you can start sampling again.

[show source]
     # File lib/benelux/stats.rb, line 149
149:       def reset
150:         @n, @sum, @sumsq = 0.0, 0.0, 0.0
151:         @min, @max = 0.0, 0.0
152:       end
sample (s)

Adds a sampling to the calculations.

[show source]
     # File lib/benelux/stats.rb, line 173
173:       def sample(s)
174:         @sum += s
175:         @sumsq += s * s
176:         if @n == 0
177:           @min = @max = s
178:         else
179:           @min = s if @min > s
180:           @max = s if @max < s
181:         end
182:         @n+=1
183:       end
samples (*args)
[show source]
     # File lib/benelux/stats.rb, line 154
154:       def samples(*args)  
155:         args.flatten.each { |s| sample(s) }
156:       end
sd ()

Calculates the standard deviation of the data so far.

[show source]
     # File lib/benelux/stats.rb, line 221
221:       def sd
222:         return 0.0 if @n <= 1
223:         # (sqrt( ((s).sumsq - ( (s).sum * (s).sum / (s).n)) / ((s).n-1) ))
224:         begin
225:           return Math.sqrt( (@sumsq - (@sum * @sum / @n)) / (@n-1) )
226:         rescue Errno::EDOM
227:           return 0.0
228:         end
229:       end
tick ()
[show source]
     # File lib/benelux/stats.rb, line 186
186:       def tick
187:         tick_time = Time.now
188:         sample(tick_time - @last_time)
189:         @last_time = tick_time
190:       end
to_f ()
[show source]
     # File lib/benelux/stats.rb, line 210
210:       def to_f; mean.to_f; end
to_i ()
[show source]
     # File lib/benelux/stats.rb, line 211
211:       def to_i; mean.to_i; end
to_s ()
[show source]
     # File lib/benelux/stats.rb, line 209
209:       def to_s; mean.to_s; end