Module: Enumerable
- Included in:
- Sbn::Combination
- Defined in:
- lib/sbn/helpers.rb
Overview
Thanks to Eric Hodel for the following additions to the enumerable model, from ruby-talk post #135920.
Instance Method Summary collapse
-
#average ⇒ Object
Average of all the elements of the Enumerable.
-
#sample_variance ⇒ Object
Sample variance of all the elements of the Enumerable.
-
#standard_deviation ⇒ Object
Standard deviation of all the elements of the Enumerable.
-
#sum ⇒ Object
Sum of all the elements of the Enumerable.
Instance Method Details
#average ⇒ Object
Average of all the elements of the Enumerable
The Enumerable must respond to #length
119 120 121 |
# File 'lib/sbn/helpers.rb', line 119 def average return self.sum / self.length.to_f end |
#sample_variance ⇒ Object
Sample variance of all the elements of the Enumerable
The Enumerable must respond to #length
127 128 129 130 131 |
# File 'lib/sbn/helpers.rb', line 127 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
Standard deviation of all the elements of the Enumerable
The Enumerable must respond to #length
137 138 139 |
# File 'lib/sbn/helpers.rb', line 137 def standard_deviation return Math.sqrt(self.sample_variance) end |
#sum ⇒ Object
Sum of all the elements of the Enumerable
111 112 113 |
# File 'lib/sbn/helpers.rb', line 111 def sum return self.inject(0) { |acc, i| acc + i } end |