Class: Axiom::Aggregate::Variance
- Inherits:
-
Axiom::Aggregate
- Object
- Function
- Axiom::Aggregate
- Axiom::Aggregate::Variance
- Defined in:
- lib/axiom/aggregate/variance.rb
Overview
The variance of a sequence of numbers
Direct Known Subclasses
Defined Under Namespace
Modules: Methods
Constant Summary collapse
- DEFAULT =
[0, nil, Rational(0)].freeze
Instance Attribute Summary
Attributes included from Operation::Unary
Class Method Summary collapse
-
.call(accumulator, value) ⇒ Array(Integer, Numeric, Rational)
Return the count, mean, and sum of squares for a sequence of numbers.
-
.finalize(accumulator) ⇒ Rational, ...
Calculate the variance from the accumulator.
-
.type ⇒ Class<Types::Float>
Return the type returned from #call.
Methods inherited from Axiom::Aggregate
#call, #default, default, #finalize
Methods included from Operation::Unary
Methods inherited from Function
extract_value, rename_attributes, #type
Methods included from Visitable
Class Method Details
.call(accumulator, value) ⇒ Array(Integer, Numeric, Rational)
Return the count, mean, and sum of squares for a sequence of numbers
23 24 25 26 27 28 29 30 |
# File 'lib/axiom/aggregate/variance.rb', line 23 def self.call(accumulator, value) return accumulator if value.nil? mean, sum_of_squares = accumulator.last(2) delta = mean.nil? ? value : value - mean count, new_mean = Mean.call(accumulator, value) sum_of_squares += delta * (value - new_mean) [count, new_mean, sum_of_squares] end |
.finalize(accumulator) ⇒ Rational, ...
Calculate the variance from the accumulator
47 48 49 50 51 |
# File 'lib/axiom/aggregate/variance.rb', line 47 def self.finalize(accumulator) sum_of_squares, count = accumulator.values_at(2, 0) return Float::INFINITY if sum_of_squares.equal?(Float::INFINITY) Rational(sum_of_squares, count) unless count.zero? end |
.type ⇒ Class<Types::Float>
Return the type returned from #call
61 62 63 |
# File 'lib/axiom/aggregate/variance.rb', line 61 def self.type Types::Float end |