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

Methods inherited from Axiom::Aggregate

#call, #default, default, #finalize

Methods included from Operation::Unary

#initialize

Methods inherited from Function

extract_value, rename_attributes, #type

Methods included from Visitable

#accept

Class Method Details

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

Return the count and mean for a sequence of numbers

Examples:

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

Parameters:

Returns:

  • (Array(Integer, Rational))


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 ? Rational(value - mean, count) + mean : value.to_r]
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
46
# File 'lib/axiom/aggregate/mean.rb', line 43

def self.finalize(accumulator)
  mean = accumulator.last
  mean.to_f if mean
end

.typeClass<Types::Float>

Return the type returned from #call

Examples:

type = Axiom::Aggregate::Mean.type  # => Axiom::Types::Float

Returns:

  • (Class<Types::Float>)


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

def self.type
  Types::Float
end