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
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/swak/toplevel.rb', line 126 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
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/swak/toplevel.rb', line 140 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
103 104 105 |
# File 'lib/swak/toplevel.rb', line 103 def avg return sum.to_f / self.length end |
#hist ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/swak/toplevel.rb', line 77 def hist h = {} for item in self h[item] ||= 0 h[item] += 1 end return h end |
#mean ⇒ Object
122 123 124 |
# File 'lib/swak/toplevel.rb', line 122 def mean return self.avg end |
#shuffle ⇒ Object
91 92 93 |
# File 'lib/swak/toplevel.rb', line 91 def shuffle return dup().sort_by {rand} end |
#shuffle! ⇒ Object
86 87 88 89 |
# File 'lib/swak/toplevel.rb', line 86 def shuffle! sort_by {rand} return self end |
#stddev ⇒ Object
118 119 120 |
# File 'lib/swak/toplevel.rb', line 118 def stddev return Math.sqrt(self.variance()) end |
#sum ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/swak/toplevel.rb', line 95 def sum total = 0 for item in self total += item end return total end |
#variance ⇒ Object
Sample variance
108 109 110 111 112 113 114 115 116 |
# File 'lib/swak/toplevel.rb', line 108 def variance average = avg() variance = 0 for item in self diff = item - average variance += diff * diff end return variance / (self.length - 1) end |