Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/dgaff/array.rb
Class Method Summary collapse
Instance Method Summary collapse
- #accumulate ⇒ Object
- #all_combinations(length_range = 1..self.length) ⇒ Object
- #all_stats ⇒ Object
- #area ⇒ Object
- #average ⇒ Object
- #centroid ⇒ Object
- #chunk(pieces = 2) ⇒ Object
- #ci_with_mean(conf_level = 1.96) ⇒ Object
- #counts ⇒ Object
- #exclude?(elem) ⇒ Boolean
- #frequencies ⇒ Object
- #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
- #repack ⇒ Object
- #reverse_percentile(value = 0.0) ⇒ Object
- #rolling_average ⇒ Object
- #sample_variance ⇒ Object
- #standard_deviation ⇒ Object
- #standardize ⇒ Object
- #sth ⇒ Object
- #structs_to_hashes ⇒ Object
- #sum ⇒ Object
- #to_f ⇒ Object
- #to_i ⇒ Object
Class Method Details
.model_sample(n, count) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/dgaff/array.rb', line 98 def self.model_sample(n, count) offsets = [] while offsets.length < n offsets << rand(count) end return offsets end |
Instance Method Details
#accumulate ⇒ Object
225 226 227 228 |
# File 'lib/dgaff/array.rb', line 225 def accumulate sum = 0 self.map{|x| sum += x} end |
#all_combinations(length_range = 1..self.length) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/dgaff/array.rb', line 55 def all_combinations(length_range=1..self.length) permutations = [] length_range.max.downto(length_range.min) do |length| self.permutation(length).each do |perm| permutations << perm.sort if !permutations.include?(perm.sort) end end return permutations end |
#all_stats ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/dgaff/array.rb', line 177 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 |
#area ⇒ Object
49 50 51 52 53 |
# File 'lib/dgaff/array.rb', line 49 def area side_one = (self[0].to_f-self[2].to_f).abs side_two = (self[1].to_f-self[3].to_f).abs return side_one*side_two end |
#average ⇒ Object
120 121 122 |
# File 'lib/dgaff/array.rb', line 120 def average return self.sum/self.length.to_f end |
#centroid ⇒ Object
42 43 44 45 46 47 |
# File 'lib/dgaff/array.rb', line 42 def centroid dimensions = self.flatten x_cent = (x_vals = 1.upto(dimensions.length).collect{|x| dimensions[x] if x.even?}.compact).sum/x_vals.length y_cent = (y_vals = 1.upto(dimensions.length).collect{|y| dimensions[y] if !y.even?}.compact).sum/y_vals.length return x_cent, y_cent end |
#chunk(pieces = 2) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/dgaff/array.rb', line 19 def chunk(pieces=2) len = self.length return [] if len == 0 mid = (len/pieces) chunks = [] start = 0 1.upto(pieces) do |i| last = start+mid last = last-1 unless len%pieces >= i chunks << self[start..last] || [] start = last+1 end chunks end |
#ci_with_mean(conf_level = 1.96) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/dgaff/array.rb', line 91 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
144 145 146 147 148 149 |
# File 'lib/dgaff/array.rb', line 144 def counts self.inject(Hash.new(0)) do |hash,element| hash[element] += 1 hash end end |
#exclude?(elem) ⇒ Boolean
195 196 197 |
# File 'lib/dgaff/array.rb', line 195 def exclude?(elem) !self.include?(elem) end |
#frequencies ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/dgaff/array.rb', line 10 def frequencies new_val = {} self.each do |s| elem = s.to_s new_val[elem].nil? ? new_val[elem]=1 : new_val[elem]+=1 end return new_val end |
#join_with_oxford_comma(delimiter = ", ") ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/dgaff/array.rb', line 205 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
106 107 108 109 110 111 112 113 114 |
# File 'lib/dgaff/array.rb', line 106 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
172 173 174 175 |
# File 'lib/dgaff/array.rb', line 172 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
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/dgaff/array.rb', line 80 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
199 200 201 202 203 |
# File 'lib/dgaff/array.rb', line 199 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
151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/dgaff/array.rb', line 151 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 |
#repack ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/dgaff/array.rb', line 34 def repack set = [] self.each do |slice| set<<slice yield set end end |
#reverse_percentile(value = 0.0) ⇒ Object
164 165 166 167 168 169 170 |
# File 'lib/dgaff/array.rb', line 164 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 |
#rolling_average ⇒ Object
230 231 232 233 234 235 236 |
# File 'lib/dgaff/array.rb', line 230 def rolling_average averaged = [] self.accumulate.each_with_index do |el, i| averaged << el/(i+1).to_f end averaged end |
#sample_variance ⇒ Object
124 125 126 127 128 |
# File 'lib/dgaff/array.rb', line 124 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
130 131 132 133 |
# File 'lib/dgaff/array.rb', line 130 def standard_deviation return 0 if self.empty? return Math.sqrt(self.sample_variance) end |
#standardize ⇒ Object
135 136 137 138 139 140 141 142 |
# File 'lib/dgaff/array.rb', line 135 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 |
#sth ⇒ Object
76 77 78 |
# File 'lib/dgaff/array.rb', line 76 def sth structs_to_hashes end |
#structs_to_hashes ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/dgaff/array.rb', line 65 def structs_to_hashes keys = (self.first.methods-Class.methods).collect{|x| x.to_s.gsub("=", "") if x.to_s.include?("=") && x.to_s!= "[]="}.compact hashed_set = [] self.each do |struct| object = {} keys.collect{|k| object[k] = k.class == DateTime ? struct.send(k).to_time : struct.send(k)} hashed_set << object end return hashed_set end |
#sum ⇒ Object
116 117 118 |
# File 'lib/dgaff/array.rb', line 116 def sum return self.collect(&:to_f).inject(0){|acc,i|acc +i} end |
#to_f ⇒ Object
6 7 8 |
# File 'lib/dgaff/array.rb', line 6 def to_f self.collect{|x| x.to_f} end |
#to_i ⇒ Object
2 3 4 |
# File 'lib/dgaff/array.rb', line 2 def to_i self.collect{|x| x.to_i} end |