Module: Enumerable

Defined in:
lib/sixarm_ruby_math_statistics/enumerable.rb

Overview

Math statistics methods that are Enumerable extensions.

Instance Method Summary collapse

Instance Method Details

#deviationObject

Calculate the deviation of a list of numbers.

Example:

[3, 1, 4].deviation => 1.247


83
84
85
# File 'lib/sixarm_ruby_math_statistics/enumerable.rb', line 83

def deviation
  size==0 ? nil : Math::sqrt(variance)
end

#meanObject

Calculate the mean of a list of numbers.

Example:

[3, 1, 4].mean => 2.666

If the list is blank, then return nil.



41
42
43
# File 'lib/sixarm_ruby_math_statistics/enumerable.rb', line 41

def mean
  size==0 ? nil : sum.to_f / size
end

#medianObject

Calculate the median of a list of numbers.

Example:

[3, 1, 4].median => 3.0

If the list is blank, then return nil.



53
54
55
# File 'lib/sixarm_ruby_math_statistics/enumerable.rb', line 53

def median
  size==0 ? nil : ((0==self.size%2) ? sort[size/2-1,2].mean : sort[self.size/2].to_f)
end

#sum(identity = 0, &block) ⇒ Object

Calculate the sum of a list of numbers.

Example:

[3, 1, 4].sum => 8

Example with a block:

[3, 1, 4].sum{|x| x * 2} => 16

Example with a default value in case the list is empty:

[].sum(99) => 99

This method is copied from rails to ensure compatibility.



24
25
26
27
28
29
30
31
# File 'lib/sixarm_ruby_math_statistics/enumerable.rb', line 24

def sum(identity = 0, &block)
  return identity unless size > 0
  if block_given?
    map(&block).sum
  else
    inject { |sum, element| sum + element }
  end
end

#sum_of_squaresObject

Calculate the sum of squares of a list of numbers.

Example:

[3, 1, 4].sum_of_squares => 26


63
64
65
# File 'lib/sixarm_ruby_math_statistics/enumerable.rb', line 63

def sum_of_squares
  size==0 ? 0 : inject(0){|sum,x|sum+(x*x)}
end

#varianceObject

Calculate the variance of a list of numbers.

Example:

[3, 1, 4].variance => 1.555


73
74
75
# File 'lib/sixarm_ruby_math_statistics/enumerable.rb', line 73

def variance
  size==0 ? nil : ( m=mean and sum_of_squares.to_f/size - m*m )
end