Class: RubyUtils::Statistic

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_utils/statistic.rb

Overview

This class holds methods to apply statistical functions of the provided data

Class Method Summary collapse

Class Method Details

.determine_mode_values(frequency) ⇒ Array (private)

private method to extract the mode values from the frequency distribution of the data array

Parameters:

  • frequency (Hash)

    the value distribution of the data values

Returns:

  • (Array)

    the mode(s) of the input data



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ruby_utils/statistic.rb', line 87

private_class_method def self.determine_mode_values(frequency)
  mode = Array.new()
  count = 0
  frequency.each_pair { |key, value|
    if (value > count)
      mode = Array.new()
      mode << key
      count = value
    elsif (value == count)
      mode << key
    end 
  }

  mode.sort
end

.extreme_values(data) ⇒ ExtremeValues

method to determine the extreme value min and max of the given data

Parameters:

  • data (Array)

    a list of values

Returns:



67
68
69
# File 'lib/ruby_utils/statistic.rb', line 67

def self.extreme_values(data)
  ExtremeValues.new(data.min, data.max)
end

.max_value(data) ⇒ Float

method to determine the maximum value of the given data

Parameters:

  • data (Array)

    a list of values

Returns:

  • (Float)

    the maximum value of the input data



53
54
55
# File 'lib/ruby_utils/statistic.rb', line 53

def self.max_value(data)
  data.max
end

.mean_value(data) ⇒ Float

method to calculate the arithmetic mean values of the given data

Parameters:

  • data (Array)

    a list of values that have a natural order and can determine a mean value

Returns:

  • (Float)

    the mean value of the input data



10
11
12
13
14
15
16
17
# File 'lib/ruby_utils/statistic.rb', line 10

def self.mean_value(data)
  mean = 0.0
  data.each { |value|
    mean += value
  }

  mean / data.length
end

.median(data) ⇒ Float

method to calculate the median of the given data. For a list with an odd number of elements the median is the value in the middle of the list. For an even number of elements the mean value of the two middle numbers is used

Parameters:

  • data (Array)

    a list of values that have a natural order and can determine a mean value

Returns:

  • (Float)

    the median of the input data



25
26
27
28
29
# File 'lib/ruby_utils/statistic.rb', line 25

def self.median(data)
  sorted = data.sort
  return sorted[data.length / 2] if (data.length % 2 == 1)
  (sorted[data.length / 2] + sorted[data.length / 2 - 1]) / 2.0
end

.min_value(data) ⇒ Float

method to determine the minimum value of the given data

Parameters:

  • data (Array)

    a list of values

Returns:

  • (Float)

    the minimum value of the input data



60
61
62
# File 'lib/ruby_utils/statistic.rb', line 60

def self.min_value(data)
  data.min
end

.mode(data) ⇒ Array

method to calculate the mode for the given data. Since it is frequent that more than one value is the mode of the data set an array is returned, which holds a least one value representing the mode.

Parameters:

  • data (Array)

    a list of values that have a natural order and can determine a mean value

Returns:

  • (Array)

    the mode(s) of the input data



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby_utils/statistic.rb', line 37

def self.mode(data)
  frequency = Hash.new()
  data.each { |value|
    if (frequency[value] == nil)
      frequency[value] = 1
    else
      frequency[value] = frequency[value] + 1
    end 
  }

  determine_mode_values(frequency)      
end

.variance(data) ⇒ Float

method to determine the empiric variance for the given data

Parameters:

  • data (Array)

    a list of values

Returns:

  • (Float)

    the empiric variance of the input data



74
75
76
77
78
79
80
81
# File 'lib/ruby_utils/statistic.rb', line 74

def self.variance(data)
  mean_value = mean_value(data)
  variance = 0.0
  data.each { |value|
    variance += (value - mean_value)**2        
  }
  variance / (data.length - 1)
end