class FastStats::Mean

Constants

DEFAULT_ROUND

Attributes

log_sum[R]
n[R]
sum[R]

Public Class Methods

new() click to toggle source
# File lib/fast_stats/mean.rb, line 11
def initialize()
  @sum = 0.0
  @n = 0
  @log_sum = 0.0
  @log_n = 0
end

Public Instance Methods

<<(val)
Alias for: add
add(val) click to toggle source
# File lib/fast_stats/mean.rb, line 18
def add(val)
  throw ArgumentError.new "#add, val must be >= 0" if val < 0
  @sum += val
  @log_sum += safe_log(val)
  @n += 1
end
Also aliased as: <<
arithmetic(round: DEFAULT_ROUND) click to toggle source
# File lib/fast_stats/mean.rb, line 27
def arithmetic(round: DEFAULT_ROUND)
  return nil if n == 0
  (sum / n).round round
end
geometric(round: DEFAULT_ROUND) click to toggle source
# File lib/fast_stats/mean.rb, line 32
def geometric(round: DEFAULT_ROUND)
  return nil if n == 0
  (2 ** (log_sum / n)).round round
end
summary(round: DEFAULT_ROUND) click to toggle source
# File lib/fast_stats/mean.rb, line 37
def summary(round: DEFAULT_ROUND)
  {
    "arithmetic" => arithmetic(round: round),
    "geometric" => geometric(round: round),
  }
end

Private Instance Methods

safe_geometric_mean_val(val) click to toggle source
# File lib/fast_stats/mean.rb, line 50
def safe_geometric_mean_val(val)
  val > 0 ? val : 1
end
safe_log(val) click to toggle source
# File lib/fast_stats/mean.rb, line 46
def safe_log(val)
  Math.log2 safe_geometric_mean_val(val)
end