Class: DDMetrics::Stats
- Inherits:
-
Object
- Object
- DDMetrics::Stats
- Defined in:
- lib/ddmetrics/stats.rb
Defined Under Namespace
Classes: EmptyError
Instance Method Summary collapse
- #avg ⇒ Object
- #count ⇒ Object
-
#initialize(values) ⇒ Stats
constructor
A new instance of Stats.
- #inspect ⇒ Object
- #max ⇒ Object
- #min ⇒ Object
- #quantile(fraction) ⇒ Object
- #sum ⇒ Object
Constructor Details
#initialize(values) ⇒ Stats
Returns a new instance of Stats.
11 12 13 |
# File 'lib/ddmetrics/stats.rb', line 11 def initialize(values) @values = values end |
Instance Method Details
#avg ⇒ Object
29 30 31 |
# File 'lib/ddmetrics/stats.rb', line 29 def avg sum.to_f / count end |
#count ⇒ Object
19 20 21 |
# File 'lib/ddmetrics/stats.rb', line 19 def count @values.size end |
#inspect ⇒ Object
15 16 17 |
# File 'lib/ddmetrics/stats.rb', line 15 def inspect "<#{self.class} count=#{count}>" end |
#max ⇒ Object
37 38 39 |
# File 'lib/ddmetrics/stats.rb', line 37 def max quantile(1.0) end |
#min ⇒ Object
33 34 35 |
# File 'lib/ddmetrics/stats.rb', line 33 def min quantile(0.0) end |
#quantile(fraction) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/ddmetrics/stats.rb', line 41 def quantile(fraction) raise EmptyError if @values.empty? target = (@values.size - 1) * fraction.to_f interp = target % 1.0 (sorted_values[target.floor] * (1.0 - interp)) + (sorted_values[target.ceil] * interp) end |
#sum ⇒ Object
23 24 25 26 27 |
# File 'lib/ddmetrics/stats.rb', line 23 def sum raise EmptyError if @values.empty? @values.reduce(:+) end |