module MagicNumbers::Summary

Public Instance Methods

mean() click to toggle source

Arithmetic mean

@example

[1, 2, 3, 4].mean #=> 2.5
[].mean           #=> nil
["a", 5].mean     #=> TypeError

@see en.wikipedia.org/wiki/Arithmetic_mean Wikipedia: Arithmetic Mean @raise [TypeError] When collection includes a non Numeric type @return [Numeric] The mean

# File lib/magic_numbers/summary.rb, line 39
def mean
  return nil unless any?
  sum / size.to_f
end
numeric?() click to toggle source

Check if all elements are numbers.

@example

[1, 2, 3].numeric? #=> True
["a", 5].numeric?  #=> False
# File lib/magic_numbers/summary.rb, line 8
def numeric?
  all? { |x| x.is_a?(Numeric) }
end
range() click to toggle source

The difference between the minimum and the maximum values.

@example

[19, 4, 5, 8].range #=> 15
[].range            #=> nil
["a", 5].range      #=> TypeError

@see en.wikipedia.org/wiki/Range_(statistics) Wikipedia: Range @raise [TypeError] When collection includes a non Numeric type @return [Numeric] The range

# File lib/magic_numbers/summary.rb, line 104
def range
  return nil unless any?
  if numeric?
    max - min
  else
    raise TypeError, "collection must include only Numeric types"
  end
end
sd(degrees_of_freedom = 0)
Alias for: standard_deviation
standard_deviation(degrees_of_freedom = 0) click to toggle source

A statistic that measures how spread out a set of data is.

@example

[1, 4, 5, 8].sd      #=> 2.5
[3, 4, 7, 12].sd     #=> 3.5
[2, 3, 6, 12].sd(1)  #=> 4.5
[].sd                #=> nil
["a", 5].sd          #=> TypeError
[2, 3, 6, 12].sd(-1) #=> ArgumentError
[2, 3, 6, 12].sd(4)  #=> ArgumentError

@see en.wikipedia.org/wiki/Standard_deviation Wikipedia: Standard Deviation @see en.wikipedia.org/wiki/Degrees_of_freedom_%28statistics%29 Wikipedia: Degrees of Freedom @raise [TypeError] When collection includes a non Numeric type @raise [ArgumentError] When degress of freedom is not between (0..collection_size - 1) @return [Numeric] The population standard deviation

# File lib/magic_numbers/summary.rb, line 88
def standard_deviation(degrees_of_freedom = 0)
  return nil unless l_variance = variance(degrees_of_freedom)
  Math.sqrt(l_variance)
end
Also aliased as: sd
sum() click to toggle source

Sum of all elements.

@example

[1, 2, 3].sum #=> 6
[].sum        #=> nil
["a", 5].sum  #=> TypeError

@raise [TypeError] When collection includes a non Numeric type @return [Numeric] The sum of all objects

# File lib/magic_numbers/summary.rb, line 21
def sum
  if numeric?
    reduce(:+)
  else
    raise TypeError, "collection must include only Numeric types"
  end
end
var(degrees_of_freedom = 0)
Alias for: variance
variance(degrees_of_freedom = 0) click to toggle source

A measure of the expected deviation from the mean.

@example

[2, 3, 4, 5].var      #=> 1.25
[1, 4, 5, 8].var      #=> 6.25
[2, 3, 6, 12].var(1)  #=> 20.25
[].var                #=> nil
["a", 5].var          #=> TypeError
[2, 3, 6, 12].var(-1) #=> ArgumentError
[2, 3, 6, 12].var(4)  #=> ArgumentError

@see en.wikipedia.org/wiki/Variance Wikipedia: Variance @see en.wikipedia.org/wiki/Degrees_of_freedom_%28statistics%29 Wikipedia: Degrees of Freedom @raise [TypeError] When collection includes a non Numeric type @raise [ArgumentError] When degress of freedom is not between (0..collection_size - 1) @return [Numeric] The population variance

# File lib/magic_numbers/summary.rb, line 60
def variance(degrees_of_freedom = 0)
  l_mean, l_size = mean, size
  msg = "degrees_of_freedom must be greater or equal to zero and lesser than collection size"

  return nil unless l_mean
  raise ArgumentError, msg unless (0...l_size) === degrees_of_freedom

  squares = map { |x| (x - mean) ** 2 }
  squares.sum / (l_size - degrees_of_freedom)
end
Also aliased as: var