Class: Procview::Accumulator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeAccumulator

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

#aveObject (readonly)

Returns the value of attribute ave.



67
68
69
# File 'lib/procview/stats.rb', line 67

def ave
  @ave
end

#nObject (readonly)

Returns the value of attribute n.



67
68
69
# File 'lib/procview/stats.rb', line 67

def n
  @n
end

#qObject (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

#stddevObject



85
86
87
# File 'lib/procview/stats.rb', line 85

def stddev
   return Math.sqrt(var)
end

#sumObject



79
80
81
# File 'lib/procview/stats.rb', line 79

def sum
   return @n * @ave
end

#varObject



82
83
84
# File 'lib/procview/stats.rb', line 82

def var
   return (@n == 0) ? 0.0 : @q/@n
end