Sha256: 19e35c9d0f56cb6d9754647ef7b113d7a3d14c498977d26e578f11ac1e488539

Contents?: true

Size: 1023 Bytes

Versions: 13

Compression:

Stored size: 1023 Bytes

Contents

module Alf
  class Aggregator
    #
    # Defines a `variance()` aggregation operator.
    #
    # Example:
    #
    #   # direct ruby usage
    #   Alf::Aggregator.variance{ qty }.aggregate(...)
    #
    #   # lispy 
    #   (summarize :supplies, [:sid], :total => variance{ qty })
    #
    class Variance < Aggregator

      # Returns the least value.
      #
      # @see Aggregator::InstanceMethods#least
      def least()
        [0, 0.0, 0.0]
      end

      # Aggregates on a tuple occurence.
      #
      # @see Aggregator::InstanceMethods#_happens
      def _happens(memo, val) 
        count, mean, m2 = memo
        count += 1
        delta = val - mean
        mean  += (delta / count)
        m2    += delta*(val - mean)
        [count, mean, m2]
      end

      # Finalizes the computation.
      #
      # @see Aggregator::InstanceMethods#finalize
      def finalize(memo) 
        count, mean, m2 = memo
        m2 / count
      end

    end # class Variance
  end # class Aggregator
end # module Alf

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
alf-core-0.16.3 lib/alf/aggregator/variance.rb
alf-core-0.16.2 lib/alf/aggregator/variance.rb
alf-core-0.16.1 lib/alf/aggregator/variance.rb
alf-core-0.16.0 lib/alf/aggregator/variance.rb
alf-core-0.15.0 lib/alf/aggregator/variance.rb
alf-core-0.14.0 lib/alf-aggregator/alf/aggregator/variance.rb
alf-core-0.13.1 lib/alf-aggregator/alf/aggregator/variance.rb
alf-core-0.13.0 lib/alf-aggregator/alf/aggregator/variance.rb
alf-0.12.2 lib/alf/aggregator/variance.rb
alf-0.12.1 lib/alf/aggregator/variance.rb
alf-0.12.0 lib/alf/aggregator/variance.rb
alf-0.11.1 lib/alf/aggregator/variance.rb
alf-0.11.0 lib/alf/aggregator/variance.rb