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(...)
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(attribute = nil, nth = 50, &bl) ⇒ Percentile
constructor
A new instance of Percentile.
-
#least ⇒ Object
Returns [] as least value.
Methods inherited from Bmg::Summarizer
avg, by_proc, collect, concat, count, distinct, #happens, max, min, multiple, percentile, stddev, sum, summarization, #summarize, #to_summarizer_name, variance
Constructor Details
#initialize(attribute = nil, nth = 50, &bl) ⇒ Percentile
Returns a new instance of Percentile.
13 14 15 16 17 |
# File 'lib/bmg/summarizer/percentile.rb', line 13 def initialize(attribute = nil, nth = 50, &bl) attribute, nth = nil, attribute if attribute.is_a?(Integer) super(*[attribute].compact, &bl) @nth = nth end |
Instance Method Details
#_happens(memo, val) ⇒ Object
Collects the value
25 26 27 |
# File 'lib/bmg/summarizer/percentile.rb', line 25 def _happens(memo, val) memo << val end |
#finalize(memo) ⇒ Object
Finalizes the computation.
30 31 32 33 34 35 36 37 |
# File 'lib/bmg/summarizer/percentile.rb', line 30 def finalize(memo) return nil if memo.empty? index = memo.size * (@nth / 100.0) above = [[index.ceil - 1, memo.size - 1].min, 0].max below = [index.floor - 1, 0].max sorted = memo.sort (sorted[above] + sorted[below]) / 2 end |
#least ⇒ Object
Returns [] as least value.
20 21 22 |
# File 'lib/bmg/summarizer/percentile.rb', line 20 def least() [] end |