Module: Enumerable
- Defined in:
- lib/spatial_stats/enumerable_ext.rb
Overview
Extension to the Enumerable module
Instance Method Summary collapse
-
#mean ⇒ Float
Mean works with a numeric array and returns the arithmetic mean.
-
#sample_variance ⇒ Float
Sample Variance works with a numeric array and returns the variance.
-
#standardize ⇒ Array
Standardize works with a numeric array and transforms each value so that the mean is 0 and the variance is 1.
Instance Method Details
#mean ⇒ Float
Mean works with a numeric array and returns the arithmetic mean.
31 32 33 |
# File 'lib/spatial_stats/enumerable_ext.rb', line 31 def mean sum / size.to_f end |
#sample_variance ⇒ Float
Sample Variance works with a numeric array and returns the variance. Formula for variance is (x - mean)**2/(n - 1)
44 45 46 47 48 |
# File 'lib/spatial_stats/enumerable_ext.rb', line 44 def sample_variance m = mean numerator = sum { |v| (v - m)**2 } numerator / (size - 1).to_f end |
#standardize ⇒ Array
Standardize works with a numeric array and transforms each value so that the mean is 0 and the variance is 1. Formula is (x - mean)/stdev
16 17 18 19 20 21 |
# File 'lib/spatial_stats/enumerable_ext.rb', line 16 def standardize # standardize is (variable - mean)/stdev m = mean std = Math.sqrt(sample_variance) map { |v| (v - m) / std } end |