Module: Enumerable

Defined in:
lib/spatial_stats/enumerable_ext.rb

Overview

Extension to the Enumerable module

Instance Method Summary collapse

Instance Method Details

#meanFloat

Mean works with a numeric array and returns the arithmetic mean.

Examples:

[1,2,3].mean
2.0

Returns:

  • (Float)

    the arithmetic mean



31
32
33
# File 'lib/spatial_stats/enumerable_ext.rb', line 31

def mean
  sum / size.to_f
end

#sample_varianceFloat

Sample Variance works with a numeric array and returns the variance. Formula for variance is (x - mean)**2/(n - 1)

Examples:

[1,2,3].sample_variance
1.0

Returns:

  • (Float)

    the sample variance



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

#standardizeArray

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

Examples:

[1,2,3].standardize
[-1.0, 0.0, 1.0]

Returns:

  • (Array)

    the standardized array



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