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

Instance Method Details

#averageObject

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_varianceObject

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_deviationObject

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

#sumObject

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