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 Attribute Summary collapse
-
.empty_collection_default_value ⇒ Object
Returns the value of attribute empty_collection_default_value.
Instance Method Summary collapse
- #descriptive_statistics ⇒ Object
- #mean(collection = self) ⇒ Object
- #median(collection = self) ⇒ Object
- #mode(collection = self) ⇒ Object
- #number(collection = self) ⇒ Object
- #percentile(p, collection = self) ⇒ Object
-
#percentile_rank(p, collection = self) ⇒ Object
percent of cases that are at or below a score.
- #range(collection = self) ⇒ Object
- #standard_deviation(collection = self) ⇒ Object
- #sum(collection = self) ⇒ Object
- #variance(collection = self) ⇒ Object
Class Attribute Details
.empty_collection_default_value ⇒ Object
Returns the value of attribute empty_collection_default_value.
5 6 7 |
# File 'lib/descriptive_statistics/class_methods.rb', line 5 def empty_collection_default_value @empty_collection_default_value end |
Instance Method Details
#descriptive_statistics ⇒ 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 return { :number => self.number, :sum => self.sum, :variance => self.variance, :standard_deviation => self.standard_deviation, :min => self.min, :max => self.max, :mean => self.mean, :mode => self.mode, :median => self.median, :range => self.range, :q1 => self.percentile(25), :q2 => self.percentile(50), :q3 => self.percentile(75) } end |
#mean(collection = self) ⇒ Object
2 3 4 5 6 7 |
# File 'lib/descriptive_statistics/mean.rb', line 2 def mean(collection = self) values = Support::convert(collection) return DescriptiveStatistics.empty_collection_default_value unless values.size > 0 values.sum / values.number end |
#median(collection = self) ⇒ Object
2 3 4 5 6 7 |
# File 'lib/descriptive_statistics/median.rb', line 2 def median(collection = self) values = Support::convert(collection) return DescriptiveStatistics.empty_collection_default_value unless values.size > 0 values.percentile(50) end |
#mode(collection = self) ⇒ Object
2 3 4 5 6 7 8 9 10 11 |
# File 'lib/descriptive_statistics/mode.rb', line 2 def mode(collection = self) values = Support::extract(collection) return unless values.size > 0 values .group_by { |e| e } .values .max_by(&:size) .first end |
#number(collection = self) ⇒ Object
2 3 4 5 6 |
# File 'lib/descriptive_statistics/number.rb', line 2 def number(collection = self) values = Support::extract(collection) values.size.to_f end |
#percentile(p, collection = self) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/descriptive_statistics/percentile.rb', line 2 def percentile(p, collection = self) values = Support::convert(collection) return DescriptiveStatistics.empty_collection_default_value unless values.size > 0 return values.first unless values.size > 1 sorted = values.sort return sorted[-1] if p == 100 rank = p / 100.0 * (values.number - 1) lrank = rank.floor d = rank - lrank lower = sorted[lrank] upper = sorted[lrank+1] lower + (upper - lower) * d end |
#percentile_rank(p, collection = self) ⇒ 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) values = Support::convert(collection) return DescriptiveStatistics.empty_collection_default_value unless values.size > 0 return (((values.sort.rindex{ |x| x <= p } || -1.0) + 1.0)) / values.number * 100.0 end |
#range(collection = self) ⇒ Object
2 3 4 5 6 7 |
# File 'lib/descriptive_statistics/range.rb', line 2 def range(collection = self) values = Support::convert(collection) return DescriptiveStatistics.empty_collection_default_value unless values.size > 0 values.max - values.min end |
#standard_deviation(collection = self) ⇒ Object
2 3 4 5 6 7 |
# File 'lib/descriptive_statistics/standard_deviation.rb', line 2 def standard_deviation(collection = self) values = Support::convert(collection) return DescriptiveStatistics.empty_collection_default_value unless values.size > 0 Math.sqrt(values.variance) end |
#sum(collection = self) ⇒ Object
2 3 4 5 6 7 |
# File 'lib/descriptive_statistics/sum.rb', line 2 def sum(collection = self) values = Support::convert(collection) return DescriptiveStatistics.empty_collection_default_value unless values.size > 0 return values.inject(:+) end |
#variance(collection = self) ⇒ Object
2 3 4 5 6 7 8 |
# File 'lib/descriptive_statistics/variance.rb', line 2 def variance(collection = self) values = Support::convert(collection) return DescriptiveStatistics.empty_collection_default_value unless values.size > 0 mean = values.mean values.map{ |sample| (mean - sample) ** 2 }.inject(:+) / values.number end |