Class: ExoBasic::AvgMeta

Inherits:
Object
  • Object
show all
Defined in:
lib/exobasic/stats/avg_meta.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAvgMeta

Returns a new instance of AvgMeta.


12
13
14
15
16
17
18
19
20
21
# File 'lib/exobasic/stats/avg_meta.rb', line 12

def initialize
  @n                  = 0
  @change             = 0.0
  @averageChange      = 0.0
  @squareOfChange     = 0.0
  @variance           = 0.0
  @averageDirectional = 0.0
  @minPossibility     = Float::MAX
  @maxPossibility     = Float::MIN
end

Instance Attribute Details

#averageChangeObject (readonly)

Returns the value of attribute averageChange.


3
4
5
# File 'lib/exobasic/stats/avg_meta.rb', line 3

def averageChange
  @averageChange
end

#averageDirectionalObject (readonly)

Returns the value of attribute averageDirectional.


3
4
5
# File 'lib/exobasic/stats/avg_meta.rb', line 3

def averageDirectional
  @averageDirectional
end

#changeObject (readonly)

Returns the value of attribute change.


3
4
5
# File 'lib/exobasic/stats/avg_meta.rb', line 3

def change
  @change
end

#maxPossibilityObject (readonly)

Returns the value of attribute maxPossibility.


3
4
5
# File 'lib/exobasic/stats/avg_meta.rb', line 3

def maxPossibility
  @maxPossibility
end

#minPossibilityObject (readonly)

Returns the value of attribute minPossibility.


3
4
5
# File 'lib/exobasic/stats/avg_meta.rb', line 3

def minPossibility
  @minPossibility
end

#nObject (readonly)

Returns the value of attribute n.


3
4
5
# File 'lib/exobasic/stats/avg_meta.rb', line 3

def n
  @n
end

#squareOfChangeObject (readonly)

Returns the value of attribute squareOfChange.


3
4
5
# File 'lib/exobasic/stats/avg_meta.rb', line 3

def squareOfChange
  @squareOfChange
end

#varianceObject (readonly)

Returns the value of attribute variance.


3
4
5
# File 'lib/exobasic/stats/avg_meta.rb', line 3

def variance
  @variance
end

Class Method Details

.append_helper(n1, d1, n2, d2, nAll) ⇒ Object


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

def self.append_helper(n1, d1, n2, d2, nAll)
  d1 * n1 / nAll + d2 * n2 / nAll
end

.mk(h) ⇒ Object


34
35
36
37
38
39
# File 'lib/exobasic/stats/avg_meta.rb', line 34

def self.mk(h)
  x = AvgMeta.new
  x.from_hash(h)

  x
end

.zeroObject

Monoid


78
79
80
# File 'lib/exobasic/stats/avg_meta.rb', line 78

def self.zero
  AvgMeta.new
end

Instance Method Details

#+(other) ⇒ Object


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/exobasic/stats/avg_meta.rb', line 86

def +(other)
  n_prime = @n + other.n

  nd = n_prime.to_f
  n1 = @n.to_f
  n2 = other.n.to_f

  x = {
    :n => n_prime
  }
  if n1 <= 0
    x[:change]             = other.change
    x[:averageChange]      = other.averageChange
    x[:squareOfChange]     = other.squareOfChange
    x[:variance]           = other.variance
    x[:averageDirectional] = other.averageDirectional
  elsif n2 <= 0
    x[:change]             = @change
    x[:averageChange]      = @averageChange
    x[:squareOfChange]     = @squareOfChange
    x[:variance]           = @variance
    x[:averageDirectional] = @averageDirectional
  else
    x[:change]             = AvgMeta.append_helper(n1, @change,
                                                   n2, other.change,
                                                   nd),
    x[:averageChange]      = AvgMeta.append_helper(n1, @averageChange,
                                                   n2, other.averageChange,
                                                   nd),
    x[:squareOfChange]     = AvgMeta.append_helper(n1, @squareOfChange,
                                                   n2, other.squareOfChange,
                                                   nd),
    x[:variance]           = AvgMeta.append_helper(n1, @variance,
                                                   n2, other.variance,
                                                   nd),
    x[:averageDirectional] = AvgMeta.append_helper(n1, @averageDirectional,
                                                   n2, other.averageDirectional,
                                                   nd)
  end

  x[:minPossibility] = [@minPossibility, other.minPossibility].min
  x[:maxPossibility] = [@maxPossibility, other.maxPossibility].max

  AvgMeta.mk(x)
end

#==(other) ⇒ Object

Equal


69
70
71
72
73
74
75
# File 'lib/exobasic/stats/avg_meta.rb', line 69

def ==(other)
  StatsHelpers.double_equals(@change,             other.change) &&
  StatsHelpers.double_equals(@variance,           other.variance) &&
  StatsHelpers.double_equals(@averageDirectional, other.averageDirectional) &&
  StatsHelpers.double_equals(@minPossibility,     other.minPossibility) &&
  StatsHelpers.double_equals(@maxPossibility,     other.maxPossibility)
end

#deep_copyObject

Copy


64
65
66
# File 'lib/exobasic/stats/avg_meta.rb', line 64

def deep_copy
  Marshal.load(Marshal.dump(self))
end

#empty?Boolean

Returns:

  • (Boolean)

41
42
43
# File 'lib/exobasic/stats/avg_meta.rb', line 41

def empty?
  @n == 0
end

#from_hash(h) ⇒ Object


23
24
25
26
27
28
29
30
31
32
# File 'lib/exobasic/stats/avg_meta.rb', line 23

def from_hash(h)
  @n                  = h[:n]
  @change             = h[:change]
  @averageChange      = h[:averageChange]
  @squareOfChange     = h[:squareOfChange]
  @variance           = h[:variance]
  @averageDirectional = h[:averageDirectional]
  @minPossibility     = h[:minPossibility]
  @maxPossibility     = h[:maxPossibility]
end

#offer(x, prev_mean, d) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/exobasic/stats/avg_meta.rb', line 45

def offer(x, prev_mean, d)
  delta                  = x - prev_mean
  d2                     = d <= 0 ? 1.0 : d.to_f
  change_prime           = delta.abs
  square_of_change_prime = delta * delta

  AvgMeta.mk({
    :n => @n + 1,
    :change => change_prime,
    :averageChange => change_prime / d2 + (d2 - 1.0) * @averageChange / d2,
    :squareOfChange => square_of_change_prime,
    :variance => square_of_change_prime / d2 + (d2 - 1.0) * @variance / d2,
    :averageDirectional => delta,
    :minPossibility => [x, @minPossibility].min,
    :maxPossibility => [x, @maxPossibility].max
  })
end

#possibilityRangeObject


136
137
138
# File 'lib/exobasic/stats/avg_meta.rb', line 136

def possibilityRange
  [@minPossibility, @maxPossibility]
end

#standardDeviationObject


132
133
134
# File 'lib/exobasic/stats/avg_meta.rb', line 132

def standardDeviation
  Math.sqrt(@variance)
end