Class: ExoBasic::ShortTermAvg

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

Constant Summary collapse

DEFAULT_D =
12

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AvgTraits

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

Constructor Details

#initialize(d = DEFAULT_D) ⇒ ShortTermAvg

Returns a new instance of ShortTermAvg.



9
10
11
12
13
# File 'lib/exobasic/stats/short_term_avg.rb', line 9

def initialize(d=DEFAULT_D)
  @d       = d <= 0 ? DEFAULT_D : d
  @history = []
  @meta    = AvgMeta.new
end

Instance Attribute Details

#dObject

Returns the value of attribute d.



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

def d
  @d
end

#historyObject (readonly)

Returns the value of attribute history.



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

def history
  @history
end

#metaObject (readonly)

Returns the value of attribute meta.



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

def meta
  @meta
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  @d == other.d &&
  (@history <=> other.history) == 0 &&
  @meta == other.meta
end

#avgObject



20
21
22
# File 'lib/exobasic/stats/short_term_avg.rb', line 20

def avg
  history.empty? ? 0.0 : history.sum / history.length.to_f
end

#offer(x) ⇒ Object



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

def offer(x)
  siz = history.length
  if siz < @d
    history.push(x)
  else
    history = history[1..-2].push(x)
  end

  @meta = @meta.offer(x, self.avg, siz)
end