Class: Bmg::Summarizer::Percentile

Inherits:
Bmg::Summarizer show all
Defined in:
lib/bmg/summarizer/percentile.rb

Overview

Percentile summarizer.

Example:

# direct ruby usage
Bmg::Summarizer.percentile(:qty, 50).summarize(...)

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :variant => :continuous
}

Instance Attribute Summary

Attributes inherited from Bmg::Summarizer

#functor, #options

Instance Method Summary collapse

Methods inherited from Bmg::Summarizer

avg, bucketize, by_proc, collect, concat, count, distinct, distinct_count, first, #happens, last, max, median, median_cont, median_disc, min, multiple, percentile, percentile_cont, percentile_disc, stddev, sum, summarization, #summarize, #to_summarizer_name, value_by, variance

Constructor Details

#initialize(*args, &bl) ⇒ Percentile

Returns a new instance of Percentile.



17
18
19
20
21
22
23
24
# File 'lib/bmg/summarizer/percentile.rb', line 17

def initialize(*args, &bl)
  @nth = args.find{|a| a.is_a?(Integer) } || 50
  functor = args.find{|a| a.is_a?(Symbol) } || bl
  options = args.select{|a| a.is_a?(Hash) }.inject(DEFAULT_OPTIONS){|memo,opts|
    memo.merge(opts)
  }.dup
  super(functor, options)
end

Instance Method Details

#_happens(memo, val) ⇒ Object

Collects the value



32
33
34
# File 'lib/bmg/summarizer/percentile.rb', line 32

def _happens(memo, val)
  memo << val
end

#finalize(memo) ⇒ Object

Finalizes the computation.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bmg/summarizer/percentile.rb', line 37

def finalize(memo)
  return nil if memo.empty?
  index = memo.size.to_f * (@nth.to_f / 100.0)
  floor, ceil = index.floor, index.ceil
  ceil +=1 if floor == ceil
  below = [floor - 1, 0].max
  above = [[ceil - 1, memo.size - 1].min, 0].max
  sorted = memo.sort
  if options[:variant] == :continuous
    (sorted[above] + sorted[below]) / 2.0
  else
    sorted[below]
  end
end

#leastObject

Returns [] as least value.



27
28
29
# File 'lib/bmg/summarizer/percentile.rb', line 27

def least()
  []
end