Module: DescriptiveStatistics
- Included in:
- Stats, Enumerable
- Defined in:
- lib/descriptive_statistics/sum.rb,
lib/descriptive_statistics/mean.rb,
lib/descriptive_statistics/mode.rb,
lib/descriptive_statistics/range.rb,
lib/descriptive_statistics/stats.rb,
lib/descriptive_statistics/median.rb,
lib/descriptive_statistics/number.rb,
lib/descriptive_statistics/variance.rb,
lib/descriptive_statistics/percentile.rb,
lib/descriptive_statistics/class_methods.rb,
lib/descriptive_statistics/percentile_rank.rb,
lib/descriptive_statistics/support/convert.rb,
lib/descriptive_statistics/standard_deviation.rb,
lib/descriptive_statistics/descriptive_statistics.rb
Defined Under Namespace
Modules: Support Classes: Stats
Class Method Summary collapse
Instance Method Summary collapse
- #descriptive_statistics(&block) ⇒ Object
- #mean(collection = self, &block) ⇒ Object
- #median(collection = self, &block) ⇒ Object
- #mode(collection = self, &block) ⇒ Object
- #number(collection = self, &block) ⇒ Object
- #percentile(p, collection = self, &block) ⇒ Object
-
#percentile_rank(p, collection = self, &block) ⇒ Object
percent of cases that are at or below a score.
- #range(collection = self, &block) ⇒ Object
- #standard_deviation(collection = self, &block) ⇒ Object
- #sum(collection = self, &block) ⇒ Object
- #variance(collection = self, &block) ⇒ Object
Class Method Details
.empty_collection_default_value ⇒ Object
5 6 7 |
# File 'lib/descriptive_statistics/class_methods.rb', line 5 def empty_collection_default_value @empty_collection_default_value end |
.empty_collection_default_value=(value) ⇒ Object
9 10 11 12 |
# File 'lib/descriptive_statistics/class_methods.rb', line 9 def empty_collection_default_value=(value) @empty_collection_default_value = value DescriptiveStatistics.instance_methods.each { |m| default_values[m] = value } end |
Instance Method Details
#descriptive_statistics(&block) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/descriptive_statistics/descriptive_statistics.rb', line 2 def descriptive_statistics(&block) return { :number => self.number(&block), :sum => self.sum(&block), :variance => self.variance(&block), :standard_deviation => self.standard_deviation(&block), :min => self.min(&block), :max => self.max(&block), :mean => self.mean(&block), :mode => self.mode(&block), :median => self.median(&block), :range => self.range(&block), :q1 => self.percentile(25, &block), :q2 => self.percentile(50, &block), :q3 => self.percentile(75, &block) } end |
#mean(collection = self, &block) ⇒ Object
2 3 4 5 6 7 |
# File 'lib/descriptive_statistics/mean.rb', line 2 def mean(collection = self, &block) values = Support::convert(collection, &block) return DescriptiveStatistics.mean_empty_collection_default_value if values.empty? values.sum / values.number end |
#median(collection = self, &block) ⇒ Object
2 3 4 5 6 7 |
# File 'lib/descriptive_statistics/median.rb', line 2 def median(collection = self, &block) values = Support::convert(collection, &block) return DescriptiveStatistics.median_empty_collection_default_value if values.empty? values.percentile(50) end |
#mode(collection = self, &block) ⇒ Object
2 3 4 5 6 7 8 9 10 11 |
# File 'lib/descriptive_statistics/mode.rb', line 2 def mode(collection = self, &block) values = Support::extract(collection, &block) return if values.to_a.empty? values .group_by { |e| e } .values .max_by(&:size) .first end |
#number(collection = self, &block) ⇒ Object
2 3 4 5 6 |
# File 'lib/descriptive_statistics/number.rb', line 2 def number(collection = self, &block) values = Support::extract(collection, &block) values.to_a.size.to_f end |
#percentile(p, collection = self, &block) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/descriptive_statistics/percentile.rb', line 2 def percentile(p, collection = self, &block) values = Support::convert(collection, &block) return DescriptiveStatistics.percentile_empty_collection_default_value if values.empty? return values.first if values.size == 1 values.sort! return values.last if p == 100 rank = p / 100.0 * (values.size - 1) lower, upper = values[rank.floor,2] lower + (upper - lower) * (rank - rank.floor) end |
#percentile_rank(p, collection = self, &block) ⇒ Object
percent of cases that are at or below a score
3 4 5 6 7 8 |
# File 'lib/descriptive_statistics/percentile_rank.rb', line 3 def percentile_rank(p, collection = self, &block) values = Support::convert(collection, &block) return DescriptiveStatistics.percentile_rank_empty_collection_default_value if values.empty? return (((values.sort.rindex { |x| x <= p } || -1.0) + 1.0)) / values.number * 100.0 end |
#range(collection = self, &block) ⇒ Object
2 3 4 5 6 7 |
# File 'lib/descriptive_statistics/range.rb', line 2 def range(collection = self, &block) values = Support::convert(collection, &block) return DescriptiveStatistics.range_empty_collection_default_value if values.empty? values.max - values.min end |
#standard_deviation(collection = self, &block) ⇒ Object
2 3 4 5 6 7 |
# File 'lib/descriptive_statistics/standard_deviation.rb', line 2 def standard_deviation(collection = self, &block) values = Support::convert(collection, &block) return DescriptiveStatistics.standard_deviation_empty_collection_default_value if values.empty? Math.sqrt(values.variance) end |
#sum(collection = self, &block) ⇒ Object
2 3 4 5 6 7 |
# File 'lib/descriptive_statistics/sum.rb', line 2 def sum(collection = self, &block) values = Support::convert(collection, &block) return DescriptiveStatistics.sum_empty_collection_default_value if values.empty? return values.reduce(:+) end |
#variance(collection = self, &block) ⇒ Object
2 3 4 5 6 7 8 |
# File 'lib/descriptive_statistics/variance.rb', line 2 def variance(collection = self, &block) values = Support::convert(collection, &block) return DescriptiveStatistics.variance_empty_collection_default_value if values.empty? mean = values.mean values.map { |sample| (mean - sample) ** 2 }.reduce(:+) / values.number end |