Class: Procview::Accumulator
- Inherits:
-
Object
- Object
- Procview::Accumulator
- Defined in:
- lib/procview/stats.rb
Overview
From en.wikipedia.org/wiki/Algorithms_for_calculating_variance The “On-line (Welford) algorithm…
Instance Attribute Summary collapse
-
#ave ⇒ Object
readonly
Returns the value of attribute ave.
-
#n ⇒ Object
readonly
Returns the value of attribute n.
-
#q ⇒ Object
readonly
Returns the value of attribute q.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Together with Chan’s Parallel interpretation.
- #acc!(duration) ⇒ Object
-
#initialize ⇒ Accumulator
constructor
A new instance of Accumulator.
- #stddev ⇒ Object
- #sum ⇒ Object
- #var ⇒ Object
Constructor Details
#initialize ⇒ Accumulator
Returns a new instance of Accumulator.
68 69 70 71 72 |
# File 'lib/procview/stats.rb', line 68 def initialize @n = 0 @ave = 0.0 @q = 0.0 end |
Instance Attribute Details
#ave ⇒ Object (readonly)
Returns the value of attribute ave.
67 68 69 |
# File 'lib/procview/stats.rb', line 67 def ave @ave end |
#n ⇒ Object (readonly)
Returns the value of attribute n.
67 68 69 |
# File 'lib/procview/stats.rb', line 67 def n @n end |
#q ⇒ Object (readonly)
Returns the value of attribute q.
67 68 69 |
# File 'lib/procview/stats.rb', line 67 def q @q end |
Instance Method Details
#+(other) ⇒ Object
Together with Chan’s Parallel interpretation
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/procview/stats.rb', line 89 def +(other) return self unless (self.n + other.n) > 0 delta = other.ave - self.ave m_a = self.var * (self.n - 1) m_b = other.var * (other.n - 1) m2 = m_a + m_b + (delta ** 2) * self.n * other.n / (self.n + other.n) newvar = m2 / (self.n + other.n - 1) @ave = (self.sum + other.sum) / (@n + other.n) @n += other.n @q = newvar * @n return self end |
#acc!(duration) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/procview/stats.rb', line 73 def acc! duration @n += 1 ak = @ave + (duration - @ave)/@n @q += (duration - @ave)*(duration - ak) @ave = ak end |
#stddev ⇒ Object
85 86 87 |
# File 'lib/procview/stats.rb', line 85 def stddev return Math.sqrt(var) end |
#sum ⇒ Object
79 80 81 |
# File 'lib/procview/stats.rb', line 79 def sum return @n * @ave end |
#var ⇒ Object
82 83 84 |
# File 'lib/procview/stats.rb', line 82 def var return (@n == 0) ? 0.0 : @q/@n end |