Class: Array
Instance Method Summary collapse
- #argmax ⇒ Object
- #argmin ⇒ Object
- #avg ⇒ Object
- #hist ⇒ Object
- #mean ⇒ Object
- #shuffle ⇒ Object
- #shuffle! ⇒ Object
- #stddev ⇒ Object
- #sum ⇒ Object
-
#variance ⇒ Object
Sample variance.
Instance Method Details
#argmax ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/swak.rb', line 130 def argmax max_i = 0 max_val = self[max_i] self.each_with_index do |x, i| if x > max_val max_val = x max_i = i end end return max_i end |
#argmin ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/swak.rb', line 144 def argmin min_i = 0 min_val = self[min_i] self.each_with_index do |x, i| if x < min_val min_val = x min_i = i end end return min_i end |
#avg ⇒ Object
107 108 109 |
# File 'lib/swak.rb', line 107 def avg return sum.to_f / self.length end |
#hist ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/swak.rb', line 81 def hist h = {} for item in self h[item] ||= 0 h[item] += 1 end return h end |
#mean ⇒ Object
126 127 128 |
# File 'lib/swak.rb', line 126 def mean return self.avg end |
#shuffle ⇒ Object
95 96 97 |
# File 'lib/swak.rb', line 95 def shuffle return dup().sort_by {rand} end |
#shuffle! ⇒ Object
90 91 92 93 |
# File 'lib/swak.rb', line 90 def shuffle! sort_by {rand} return self end |
#stddev ⇒ Object
122 123 124 |
# File 'lib/swak.rb', line 122 def stddev return Math.sqrt(self.variance()) end |
#sum ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/swak.rb', line 99 def sum total = 0 for item in self total += item end return total end |
#variance ⇒ Object
Sample variance
112 113 114 115 116 117 118 119 120 |
# File 'lib/swak.rb', line 112 def variance average = avg() variance = 0 for item in self diff = item - average variance += diff * diff end return variance / (self.length - 1) end |