Class: ExoBasic::MeanAvg

Inherits:
Object
  • Object
show all
Includes:
AvgTraits
Defined in:
lib/exobasic/stats/mean_avg.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AvgTraits

#approx=, #count, #deep_copy, #maximum, #minimum, #offer_many

Constructor Details

#initializeMeanAvg

Returns a new instance of MeanAvg.



7
8
9
10
# File 'lib/exobasic/stats/mean_avg.rb', line 7

def initialize
  @avg  = 0.0
  @meta = AvgMeta.new
end

Instance Attribute Details

#avgObject (readonly)

Returns the value of attribute avg.



5
6
7
# File 'lib/exobasic/stats/mean_avg.rb', line 5

def avg
  @avg
end

#metaObject (readonly)

Returns the value of attribute meta.



5
6
7
# File 'lib/exobasic/stats/mean_avg.rb', line 5

def meta
  @meta
end

Class Method Details

.mk(h) ⇒ Object



17
18
19
20
21
22
# File 'lib/exobasic/stats/mean_avg.rb', line 17

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

  x
end

Instance Method Details

#+(other) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/exobasic/stats/mean_avg.rb', line 37

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

  MeanAvg.mk({
    :avg => n_prime == 0 ? 0.0 : (@avg * @meta.n + other.avg * other.meta.n) / n_prime.to_f,
    :meta => @meta + other.meta
  })
end

#==(other) ⇒ Object



32
33
34
35
# File 'lib/exobasic/stats/mean_avg.rb', line 32

def ==(other)
  StatsHelpers.double_equals(@avg, other.avg) &&
  @meta == other.meta
end

#from_hash(h) ⇒ Object



12
13
14
15
# File 'lib/exobasic/stats/mean_avg.rb', line 12

def from_hash(h)
  @avg  = h[:avg]
  @meta = h[:meta]
end

#offer(x) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/exobasic/stats/mean_avg.rb', line 24

def offer(x)
  n     = @meta.n + 1
  @meta = @meta.offer(x, @avg, n)

  n_prime = n.to_f
  @avg    = x / n_prime + (n_prime - 1.0) * @avg / n_prime
end