Class: Bmg::Summarizer::Percentile
- Inherits:
-
Bmg::Summarizer
- Object
- Bmg::Summarizer
- Bmg::Summarizer::Percentile
- 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
Instance Method Summary collapse
-
#_happens(memo, val) ⇒ Object
Collects the value.
-
#finalize(memo) ⇒ Object
Finalizes the computation.
-
#initialize(*args, &bl) ⇒ Percentile
constructor
A new instance of Percentile.
-
#least ⇒ Object
Returns [] as least value.
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 = args.select{|a| a.is_a?(Hash) }.inject(DEFAULT_OPTIONS){|memo,opts| memo.merge(opts) }.dup super(functor, ) 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 [:variant] == :continuous (sorted[above] + sorted[below]) / 2.0 else sorted[below] end end |
#least ⇒ Object
Returns [] as least value.
27 28 29 |
# File 'lib/bmg/summarizer/percentile.rb', line 27 def least() [] end |