class ExoBasic::MeanAvg

Attributes

avg[R]
meta[R]

Public Class Methods

mk(h) click to toggle source
# File lib/exobasic/stats/mean_avg.rb, line 17
def self.mk(h)
  x = MeanAvg.new
  x.from_hash(h)

  x
end
new() click to toggle source
# File lib/exobasic/stats/mean_avg.rb, line 7
def initialize
  @avg  = 0.0
  @meta = AvgMeta.new
end

Public Instance Methods

+(other) click to toggle source
# 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) click to toggle source
# File lib/exobasic/stats/mean_avg.rb, line 32
def ==(other)
  StatsHelpers.double_equals(@avg, other.avg) &&
  @meta == other.meta
end
from_hash(h) click to toggle source
# File lib/exobasic/stats/mean_avg.rb, line 12
def from_hash(h)
  @avg  = h[:avg]
  @meta = h[:meta]
end
offer(x) click to toggle source
# 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