Class: Tryouts::Stats
- Inherits:
-
Object
- Object
- Tryouts::Stats
- Defined in:
- lib/tryouts/stats.rb
Instance Attribute Summary collapse
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
-
#n ⇒ Object
readonly
Returns the value of attribute n.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#sum ⇒ Object
readonly
Returns the value of attribute sum.
-
#sumsq ⇒ Object
readonly
Returns the value of attribute sumsq.
Instance Method Summary collapse
-
#dump(msg = "", out = STDERR) ⇒ Object
Dump this Stats object with an optional additional message.
-
#initialize(name = :unknown) ⇒ Stats
constructor
A new instance of Stats.
-
#mean ⇒ Object
Calculates and returns the mean for the data passed so far.
-
#reset ⇒ Object
Resets the internal counters so you can start sampling again.
-
#sample(s) ⇒ Object
Adds a sampling to the calculations.
- #samples ⇒ Object
-
#sdev ⇒ Object
Calculates the standard deviation of the data so far.
-
#tick ⇒ Object
Adds a time delta between now and the last time you called this.
-
#to_s ⇒ Object
Returns a common display (used by dump).
Constructor Details
#initialize(name = :unknown) ⇒ Stats
Returns a new instance of Stats.
22 23 24 25 |
# File 'lib/tryouts/stats.rb', line 22 def initialize(name=:unknown) @name = name reset end |
Instance Attribute Details
#max ⇒ Object (readonly)
Returns the value of attribute max.
20 21 22 |
# File 'lib/tryouts/stats.rb', line 20 def max @max end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
20 21 22 |
# File 'lib/tryouts/stats.rb', line 20 def min @min end |
#n ⇒ Object (readonly)
Returns the value of attribute n.
20 21 22 |
# File 'lib/tryouts/stats.rb', line 20 def n @n end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
20 21 22 |
# File 'lib/tryouts/stats.rb', line 20 def name @name end |
#sum ⇒ Object (readonly)
Returns the value of attribute sum.
20 21 22 |
# File 'lib/tryouts/stats.rb', line 20 def sum @sum end |
#sumsq ⇒ Object (readonly)
Returns the value of attribute sumsq.
20 21 22 |
# File 'lib/tryouts/stats.rb', line 20 def sumsq @sumsq end |
Instance Method Details
#dump(msg = "", out = STDERR) ⇒ Object
Dump this Stats object with an optional additional message.
53 54 55 |
# File 'lib/tryouts/stats.rb', line 53 def dump(msg = "", out=STDERR) out.puts "#{msg}: #{self.to_s}" end |
#mean ⇒ Object
Calculates and returns the mean for the data passed so far.
64 65 66 |
# File 'lib/tryouts/stats.rb', line 64 def mean @sum / @n end |
#reset ⇒ Object
Resets the internal counters so you can start sampling again.
28 29 30 31 32 33 34 35 |
# File 'lib/tryouts/stats.rb', line 28 def reset @sum = 0.0 @sumsq = 0.0 @last_time = Time.new @n = 0.0 @min = 0.0 @max = 0.0 end |
#sample(s) ⇒ Object
Adds a sampling to the calculations.
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/tryouts/stats.rb', line 40 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).to_f end |
#samples ⇒ Object
37 |
# File 'lib/tryouts/stats.rb', line 37 def samples; @n; end |
#sdev ⇒ Object
Calculates the standard deviation of the data so far.
69 70 71 72 73 74 75 76 |
# File 'lib/tryouts/stats.rb', line 69 def sdev # (sqrt( ((s).sumsq - ( (s).sum * (s).sum / (s).n)) / ((s).n-1) )) begin Math.sqrt( (@sumsq - ( @sum * @sum / @n)) / (@n-1) ).to_f rescue Errno::EDOM 0.0 end end |
#tick ⇒ Object
88 89 90 91 92 |
# File 'lib/tryouts/stats.rb', line 88 def tick now = Time.now sample(now - @last_time) @last_time = now end |
#to_s ⇒ Object
Returns a common display (used by dump)
58 59 60 |
# File 'lib/tryouts/stats.rb', line 58 def to_s "[#{@name}]: SUM=%0.4f, SUMSQ=%0.4f, N=%0.4f, MEAN=%0.4f, SD=%0.4f, MIN=%0.4f, MAX=%0.4f" % [@sum, @sumsq, @n, mean, sd, @min, @max] end |