Module: SimpleStats
- Included in:
- Array
- Defined in:
- lib/simple_stats.rb
Overview
Statistical methods to be used with numeric Arrays
Instance Method Summary collapse
- #coefficient_of_variation ⇒ Object
- #histogram(num_of_bins = 20) ⇒ Object
- #kurtosis ⇒ Object
- #mean ⇒ Object
- #median ⇒ Object
- #percentile(percentage) ⇒ Object
- #skewness ⇒ Object
- #standard_deviation ⇒ Object
- #variance ⇒ Object
- #within?(tolerance, options = {}) ⇒ Boolean
Instance Method Details
#coefficient_of_variation ⇒ Object
25 26 27 28 |
# File 'lib/simple_stats.rb', line 25 def coefficient_of_variation mean = self.mean mean == 0 ? 0.0 : self.standard_deviation/mean end |
#histogram(num_of_bins = 20) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/simple_stats.rb', line 48 def histogram(num_of_bins=20) min, max = self.min, self.max bin_width = Float(max-min)/num_of_bins bins = [] num_of_bins.times do bins << (min..min+bin_width) min += bin_width end histohash = {} bins.each{ |b| histohash[b]=0 } self.each do |e| bin = bins.find{ |b| b.include? e } bin = bins.last unless bin # round off histohash[ bin ] += 1 end histohash.sort do |x,y| 0.5*(x[0].begin+x[0].end) <=> 0.5*(y[0].begin+y[0].end) end end |
#kurtosis ⇒ Object
37 38 39 40 41 42 |
# File 'lib/simple_stats.rb', line 37 def kurtosis mean, variance = self.mean, self.variance variance == 0 ? 0.0 : self.inject(0) { |sum, x| sum + (x-mean)**4 } / self.size / variance**2 end |
#mean ⇒ Object
6 7 8 |
# File 'lib/simple_stats.rb', line 6 def mean self.inject(0) { |sum, x| sum + x.to_f / self.size } end |
#median ⇒ Object
10 11 12 13 14 |
# File 'lib/simple_stats.rb', line 10 def median sorted = self.sort mid = sorted.size / 2 sorted.size % 2 == 0 ? [ sorted[mid], sorted[mid-1] ].mean : sorted[mid] end |
#percentile(percentage) ⇒ Object
44 45 46 |
# File 'lib/simple_stats.rb', line 44 def percentile(percentage) self.sort[ (self.size-1)*percentage/100 + 0.5 ] end |
#skewness ⇒ Object
30 31 32 33 34 35 |
# File 'lib/simple_stats.rb', line 30 def skewness mean, variance = self.mean, self.variance variance == 0 ? 0.0 : self.inject(0) { |sum, x| sum + (x-mean)**3 } / self.size / variance**(3/2.0) end |
#standard_deviation ⇒ Object
21 22 23 |
# File 'lib/simple_stats.rb', line 21 def standard_deviation Math::sqrt(self.variance) end |
#variance ⇒ Object
16 17 18 19 |
# File 'lib/simple_stats.rb', line 16 def variance mean = self.mean self.inject(0) { |sum, x| sum + (x-mean)**2 } / self.size end |
#within?(tolerance, options = {}) ⇒ Boolean
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/simple_stats.rb', line 68 def within?(tolerance,={}) last_value = self.last max_difference = self.inject(0) do |max,value| difference = ( last_value - value ).abs max >= difference ? max : difference end scale = [:percent] ? 100.0/last_value : 1 scale = 0 if scale == 1.0/0 max_difference * scale <= tolerance end |