Module: MagicNumbers::Summary
Instance Method Summary collapse
-
#mean ⇒ Numeric
Arithmetic mean.
-
#numeric? ⇒ Boolean
Check if all elements are numbers.
-
#range ⇒ Numeric
The difference between the minimum and the maximum values.
-
#standard_deviation(degrees_of_freedom = 0) ⇒ Numeric
(also: #sd)
A statistic that measures how spread out a set of data is.
-
#sum ⇒ Numeric
Sum of all elements.
-
#variance(degrees_of_freedom = 0) ⇒ Numeric
(also: #var)
A measure of the expected deviation from the mean.
Instance Method Details
#mean ⇒ Numeric
Arithmetic mean
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.
8 9 10 |
# File 'lib/magic_numbers/summary.rb', line 8 def numeric? all? { |x| x.is_a?(Numeric) } end |
#range ⇒ Numeric
The difference between the minimum and the maximum values.
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.
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 |
#sum ⇒ Numeric
Sum of all elements.
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.
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 |