class ExoBasic::ShortTermAvg

Constants

DEFAULT_D

Attributes

d[R]
history[R]
meta[R]

Public Class Methods

new(d=DEFAULT_D) click to toggle source
# 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

Public Instance Methods

==(other) click to toggle source
# File lib/exobasic/stats/short_term_avg.rb, line 35
def ==(other)
  @d == other.d &&
  (@history <=> other.history) == 0 &&
  @meta == other.meta
end
avg() click to toggle source
# File lib/exobasic/stats/short_term_avg.rb, line 20
def avg
  history.empty? ? 0.0 : history.sum / history.length.to_f
end
d=(d) click to toggle source
# File lib/exobasic/stats/short_term_avg.rb, line 15
def d=(d)
  @d       = d <= 0 ? ShortTermAvg::DEFAULT_D : d
  @history = @history[0..@d - 1]
end
offer(x) click to toggle source
# 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