Class: FastStats::Mean

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_stats/mean.rb

Constant Summary collapse

DEFAULT_ROUND =
15

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMean

Returns a new instance of Mean.



11
12
13
14
15
16
# File 'lib/fast_stats/mean.rb', line 11

def initialize()
  @sum = 0.0
  @n = 0
  @log_sum = 0.0
  @log_n = 0
end

Instance Attribute Details

#log_sumObject (readonly)

Returns the value of attribute log_sum.



9
10
11
# File 'lib/fast_stats/mean.rb', line 9

def log_sum
  @log_sum
end

#nObject (readonly)

Returns the value of attribute n.



9
10
11
# File 'lib/fast_stats/mean.rb', line 9

def n
  @n
end

#sumObject (readonly)

Returns the value of attribute sum.



9
10
11
# File 'lib/fast_stats/mean.rb', line 9

def sum
  @sum
end

Instance Method Details

#add(val) ⇒ Object Also known as: <<



18
19
20
21
22
23
# 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

#arithmetic(round: DEFAULT_ROUND) ⇒ Object



27
28
29
30
# 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) ⇒ Object



32
33
34
35
# 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) ⇒ Object



37
38
39
40
41
42
# File 'lib/fast_stats/mean.rb', line 37

def summary(round: DEFAULT_ROUND)
  {
    "arithmetic" => arithmetic(round: round),
    "geometric" => geometric(round: round),
  }
end