Module: Enumerable
- Defined in:
- lib/viral_seq/enumerable.rb
Overview
additional statistic/math functions to Module::Enumerable
Instance Method Summary collapse
-
#count_freq ⇒ Object
tabulate elements and frequencies of an Enumerable return [Hash] return a hash of :element => :freq_count.
-
#count_freq2(decimal = 2) ⇒ Object
tabulate elements and frequencies (as percentage) of an Enumerable { return [Hash] return a hash of :element => :percentage.
-
#lower_quartile ⇒ Numeric
generate lower quartile value.
-
#mean ⇒ Float
generate mean number.
-
#median ⇒ Numeric
generate median number.
-
#sample_variance ⇒ Float
generate sample variance.
-
#stdev ⇒ Float
generate standard deviation.
-
#sum ⇒ Numeric
generate summed value.
-
#upper_quartile ⇒ Numeric
generate upper quartile value.
Instance Method Details
#count_freq ⇒ Object
tabulate elements and frequencies of an Enumerable return [Hash] return a hash of :element => :freq_count
107 108 109 110 111 112 113 |
# File 'lib/viral_seq/enumerable.rb', line 107 def count_freq hash = Hash.new(0) self.each do |element| hash[element] +=1 end return hash end |
#count_freq2(decimal = 2) ⇒ Object
tabulate elements and frequencies (as percentage) of an Enumerable { return [Hash] return a hash of :element => :percentage
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/viral_seq/enumerable.rb', line 119 def count_freq2(decimal = 2) hash1 = Hash.new(0) self.each do |element| hash1[element] += 1 end total_elements = self.size hash2 = Hash.new(0) hash1.each do |key,value| hash2[key] = (value/total_elements.to_f).round(decimal) end return hash2 end |
#lower_quartile ⇒ Numeric
generate lower quartile value
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/viral_seq/enumerable.rb', line 91 def lower_quartile return nil if self.empty? sorted_array = self.sort u = 0.25*sorted_array.length + 1 if (u-u.truncate).is_a?(Integer) return sorted_array[(u-u.truncate)-1] else sample = sorted_array[u.truncate.abs-1] sample1 = sorted_array[(u.truncate.abs)] return sample+((sample1-sample)*(u-u.truncate)) end end |
#mean ⇒ Float
generate mean number
56 57 58 |
# File 'lib/viral_seq/enumerable.rb', line 56 def mean self.sum/self.length.to_f end |
#median ⇒ Numeric
generate median number
42 43 44 45 46 |
# File 'lib/viral_seq/enumerable.rb', line 42 def median len = self.length sorted = self.sort len % 2 == 1 ? sorted[len/2] : (sorted[len/2 - 1] + sorted[len/2]).to_f / 2 end |
#sample_variance ⇒ Float
generate sample variance
62 63 64 65 66 |
# File 'lib/viral_seq/enumerable.rb', line 62 def sample_variance m = self.mean sum = self.inject(0){|accum, i| accum + (i-m)**2 } sum/(self.length - 1).to_f end |
#stdev ⇒ Float
generate standard deviation
70 71 72 |
# File 'lib/viral_seq/enumerable.rb', line 70 def stdev return Math.sqrt(self.sample_variance) end |
#sum ⇒ Numeric
generate summed value
50 51 52 |
# File 'lib/viral_seq/enumerable.rb', line 50 def sum self.inject(0){|accum, i| accum + i } end |
#upper_quartile ⇒ Numeric
generate upper quartile value
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/viral_seq/enumerable.rb', line 76 def upper_quartile return nil if self.empty? sorted_array = self.sort u = (0.25*(3*sorted_array.length)) if (u-u.truncate).is_a?(Integer) return sorted_array[(u-u.truncate)-1] else sample = sorted_array[u.truncate.abs-1] sample1 = sorted_array[(u.truncate.abs)] return sample+((sample1-sample)*(u-u.truncate)) end end |