Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/dgaff/array.rb
Class Method Summary collapse
Instance Method Summary collapse
- #all_stats ⇒ Object
- #average ⇒ Object
- #ci_with_mean(conf_level = 1.96) ⇒ Object
- #counts ⇒ Object
- #exclude?(elem) ⇒ Boolean
- #join_with_oxford_comma(delimiter = ", ") ⇒ Object
- #median ⇒ Object
- #mode ⇒ Object
- #moving_average(increment = 1) ⇒ Object
- #normalize(min = 0, max = 1) ⇒ Object
- #percentile(percentile = 0.0) ⇒ Object
- #reverse_percentile(value = 0.0) ⇒ Object
- #sample_variance ⇒ Object
- #standard_deviation ⇒ Object
- #standardize ⇒ Object
- #sum ⇒ Object
Class Method Details
.model_sample(n, count) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/dgaff/array.rb', line 20 def self.model_sample(n, count) offsets = [] while offsets.length < n offsets << rand(count) end return offsets end |
Instance Method Details
#all_stats ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/dgaff/array.rb', line 99 def all_stats summary_statistics = {} summary_statistics[:min] = self.min summary_statistics[:first_quartile] = self.percentile(0.25) summary_statistics[:second_quartile] = self.percentile(0.5) summary_statistics[:third_quartile] = self.percentile(0.75) summary_statistics[:max] = self.max summary_statistics[:median] = self.median summary_statistics[:mode] = self.mode summary_statistics[:mean] = self.average summary_statistics[:standard_deviation] = self.standard_deviation summary_statistics[:sum] = self.sum summary_statistics[:sample_variance] = self.sample_variance summary_statistics[:elbow] = self.elbow summary_statistics[:n] = self.length summary_statistics end |
#average ⇒ Object
42 43 44 |
# File 'lib/dgaff/array.rb', line 42 def average return self.sum/self.length.to_f end |
#ci_with_mean(conf_level = 1.96) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/dgaff/array.rb', line 13 def ci_with_mean(conf_level=1.96) return [0,0,0] if self.empty? mean = self.average stdev = self.standard_deviation [mean-(conf_level*stdev)/Math.sqrt(self.length), mean, mean+(conf_level*stdev)/Math.sqrt(self.length)] end |
#counts ⇒ Object
66 67 68 69 70 71 |
# File 'lib/dgaff/array.rb', line 66 def counts self.inject(Hash.new(0)) do |hash,element| hash[element] += 1 hash end end |
#exclude?(elem) ⇒ Boolean
117 118 119 |
# File 'lib/dgaff/array.rb', line 117 def exclude?(elem) !self.include?(elem) end |
#join_with_oxford_comma(delimiter = ", ") ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/dgaff/array.rb', line 127 def join_with_oxford_comma(delimiter=", ") string = "" if self.length == 1 string = self.first elsif self.length == 2 string = self.join(" and ") else self.each_with_index do |elem,i| if i == 0 string+= elem.to_s elsif i == self.length-1 string+="#{delimiter}and #{elem}" else string+="#{delimiter}#{elem}" end end end string end |
#median ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/dgaff/array.rb', line 28 def median return nil if self.empty? self.sort! if self.length % 2 == 0 (self[self.length / 2] + self[self.length/2 - 1]) / 2.0 else self[self.length / 2] end end |
#mode ⇒ Object
94 95 96 97 |
# File 'lib/dgaff/array.rb', line 94 def mode freq = self.inject(Hash.new(0)) { |h,v| h[v] += 1; h } self.sort_by { |v| freq[v] }.last end |
#moving_average(increment = 1) ⇒ Object
2 3 4 5 6 7 8 9 10 11 |
# File 'lib/dgaff/array.rb', line 2 def moving_average(increment = 1) return self.average if increment == 1 a = self.dup result = [] while(!a.empty?) data = a.slice!(0,increment) result << data.average end result end |
#normalize(min = 0, max = 1) ⇒ Object
121 122 123 124 125 |
# File 'lib/dgaff/array.rb', line 121 def normalize(min=0, max=1) current_min = self.min.to_f current_max = self.max.to_f self.map {|n| min + (n - current_min) * (max - min) / (current_max - current_min)} end |
#percentile(percentile = 0.0) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/dgaff/array.rb', line 73 def percentile(percentile=0.0) if percentile == 0.0 return self.sort.first else classes = self.collect(&:class).uniq if ([Hash, Array]-classes==[Hash, Array]) && classes.length == 1 return self ? self.sort[((self.length * percentile).ceil)-1] : nil rescue nil else return self[((self.length * percentile).ceil)-1] end end end |
#reverse_percentile(value = 0.0) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/dgaff/array.rb', line 86 def reverse_percentile(value=0.0) index_value = nil self.collect(&:to_f).sort.each do |val| index_value = val;break if value <= val end return (self.index(index_value)/self.length.to_f) end |
#sample_variance ⇒ Object
46 47 48 49 50 |
# File 'lib/dgaff/array.rb', line 46 def sample_variance avg=self.average sum=self.inject(0){|acc,i|acc +(i-avg)**2} return(1/self.length.to_f*sum) end |
#standard_deviation ⇒ Object
52 53 54 55 |
# File 'lib/dgaff/array.rb', line 52 def standard_deviation return 0 if self.empty? return Math.sqrt(self.sample_variance) end |
#standardize ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/dgaff/array.rb', line 57 def standardize return self if self.uniq.length == 1 stdev = self.standard_deviation mean = self.average self.collect do |val| (val-mean)/stdev end end |
#sum ⇒ Object
38 39 40 |
# File 'lib/dgaff/array.rb', line 38 def sum return self.collect(&:to_f).inject(0){|acc,i|acc +i} end |