Class: Axiom::Aggregate::Mean

Inherits:
Axiom::Aggregate show all
Defined in:
lib/axiom/aggregate/mean.rb

Overview

The mean of a sequence of numbers

Defined Under Namespace

Modules: Methods

Constant Summary collapse

DEFAULT =
[ 0, nil ].freeze

Instance Attribute Summary

Attributes included from Operation::Unary

#operand

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Axiom::Aggregate

#call, #default, default, #finalize

Methods included from Visitable

#accept

Methods included from Operation::Unary

#initialize

Class Method Details

.call(accumulator, value) ⇒ Array(Integer, Numeric)

Return the count and mean for a sequence of numbers

Examples:

count, mean = Mean.call([ count, mean ], value)

Parameters:

  • accumulator (Array(Integer, Numeric))
  • value (Numeric)

Returns:

  • (Array(Integer, Numeric))


23
24
25
26
27
28
# File 'lib/axiom/aggregate/mean.rb', line 23

def self.call(accumulator, value)
  return accumulator if value.nil?
  count, mean = accumulator
  count       = Count.call(count, value)
  [ count, mean.nil? ? value.to_f : mean + ((value - mean) / count) ]
end

.finalize(accumulator) ⇒ Float?

Extract the mean from the accumulator

Examples:

mean = Mean.finalize(accumulator)

Parameters:

  • accumulator (Array(Integer, Numeric))

Returns:

  • (Float)

    returned for a non-empty set

  • (nil)

    returned for an empty set



43
44
45
# File 'lib/axiom/aggregate/mean.rb', line 43

def self.finalize(accumulator)
  accumulator.last
end

Instance Method Details

#typeClass<Attribute::Float>

Return the type returned from #call

Examples:

type = Axiom::Aggregate::Mean.type

Returns:



55
56
57
# File 'lib/axiom/aggregate/mean.rb', line 55

def type
  Attribute::Float
end