Module: MagicNumbers::Summary

Included in:
Array, Set, Vector
Defined in:
lib/magic_numbers/summary.rb

Instance Method Summary collapse

Instance Method Details

#meanNumeric

Arithmetic mean

Examples:

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

Returns:

  • (Numeric)

    The mean

Raises:

  • (TypeError)

    When collection includes a non Numeric type

See Also:



39
40
41
42
# File 'lib/magic_numbers/summary.rb', line 39

def mean
  return nil unless any?
  sum / size.to_f
end

#numeric?Boolean

Check if all elements are numbers.

Examples:

[1, 2, 3].numeric? #=> True
["a", 5].numeric?  #=> False

Returns:

  • (Boolean)


8
9
10
# File 'lib/magic_numbers/summary.rb', line 8

def numeric?
  all? { |x| x.is_a?(Numeric) }
end

#rangeNumeric

The difference between the minimum and the maximum values.

Examples:

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

Returns:

  • (Numeric)

    The range

Raises:

  • (TypeError)

    When collection includes a non Numeric type

See Also:



104
105
106
107
108
109
110
111
# 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

#standard_deviation(degrees_of_freedom = 0) ⇒ Numeric Also known as: sd

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

Examples:

[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

Returns:

  • (Numeric)

    The population standard deviation

Raises:

  • (TypeError)

    When collection includes a non Numeric type

  • (ArgumentError)

    When degress of freedom is not between (0..collection_size - 1)

See Also:



88
89
90
91
# 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

#sumNumeric

Sum of all elements.

Examples:

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

Returns:

  • (Numeric)

    The sum of all objects

Raises:

  • (TypeError)

    When collection includes a non Numeric type



21
22
23
24
25
26
27
# File 'lib/magic_numbers/summary.rb', line 21

def sum
  if numeric?
    reduce(:+)
  else
    raise TypeError, "collection must include only Numeric types"
  end
end

#variance(degrees_of_freedom = 0) ⇒ Numeric Also known as: var

A measure of the expected deviation from the mean.

Examples:

[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

Returns:

  • (Numeric)

    The population variance

Raises:

  • (TypeError)

    When collection includes a non Numeric type

  • (ArgumentError)

    When degress of freedom is not between (0..collection_size - 1)

See Also:



60
61
62
63
64
65
66
67
68
69
# 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