Class: Alf::Aggregator::Variance
- Inherits:
-
Alf::Aggregator
- Object
- Alf::Aggregator
- Alf::Aggregator::Variance
- Defined in:
- lib/alf/aggregator/variance.rb
Overview
Defines a ‘variance()` aggregation operator.
Example:
# direct ruby usage
Alf::Aggregator.variance{ qty }.aggregate(...)
# lispy
(summarize :supplies, [:sid], :total => variance{ qty })
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Alf::Aggregator
Instance Method Summary collapse
-
#_happens(memo, val) ⇒ Object
Aggregates on a tuple occurence.
-
#finalize(memo) ⇒ Object
Finalizes the computation.
-
#least ⇒ Object
Returns the least value.
Methods inherited from Alf::Aggregator
#==, #aggregate, coerce, #default_options, #happens, #has_source_code!, #infer_type, inherited, #initialize, #to_lispy
Methods included from Support::Registry
#each, #listen, #listeners, #register, #registered
Constructor Details
This class inherits a constructor from Alf::Aggregator
Instance Method Details
#_happens(memo, val) ⇒ Object
Aggregates on a tuple occurence.
26 27 28 29 30 31 32 33 |
# File 'lib/alf/aggregator/variance.rb', line 26 def _happens(memo, val) count, mean, m2 = memo count += 1 delta = val - mean mean += (delta / count) m2 += delta*(val - mean) [count, mean, m2] end |
#finalize(memo) ⇒ Object
Finalizes the computation.
38 39 40 41 |
# File 'lib/alf/aggregator/variance.rb', line 38 def finalize(memo) count, mean, m2 = memo m2 / count end |
#least ⇒ Object
Returns the least value.
19 20 21 |
# File 'lib/alf/aggregator/variance.rb', line 19 def least() [0, 0.0, 0.0] end |