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
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/swak.rb', line 114 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
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/swak.rb', line 128 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
91 92 93 |
# File 'lib/swak.rb', line 91 def avg return sum.to_f / self.length end |
#hist ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/swak.rb', line 65 def hist h = {} for item in self h[item] ||= 0 h[item] += 1 end return h end |
#mean ⇒ Object
110 111 112 |
# File 'lib/swak.rb', line 110 def mean return self.avg end |
#shuffle ⇒ Object
79 80 81 |
# File 'lib/swak.rb', line 79 def shuffle return dup().sort_by {rand} end |
#shuffle! ⇒ Object
74 75 76 77 |
# File 'lib/swak.rb', line 74 def shuffle! sort_by {rand} return self end |
#stddev ⇒ Object
106 107 108 |
# File 'lib/swak.rb', line 106 def stddev return Math.sqrt(self.variance()) end |
#sum ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/swak.rb', line 83 def sum total = 0 for item in self total += item end return total end |
#variance ⇒ Object
Sample variance
96 97 98 99 100 101 102 103 104 |
# File 'lib/swak.rb', line 96 def variance average = avg() variance = 0 for item in self diff = item - average variance += diff * diff end return variance / (self.length - 1) end |