Module: BenchmarkHarness::Stats

Included in:
Array
Defined in:
lib/benchmark_harness/stats.rb

Overview

copied from github.com/mgrigajtis/easystats/blob/master/lib/easystats.rb until orig author fixes sum conflict problem

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

take in an array of numbers and calculate the sum



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/benchmark_harness/stats.rb', line 6

def self.included(base)
  unless  base.method_defined? :sum
    base.class_eval do
      define_method :sum do
        data = self
        sum_of_numbers = 0

        # Get the total sum only if there are actually numbers to total
        if data.count > 0
          data.each do |num|
            sum_of_numbers += num
          end
        else
          sum_of_numbers = 0
        end

        sum_of_numbers
      end
    end
  end      
end

Instance Method Details

#meanObject Also known as: average

take in an array of numbers and calculate the mean (average)



29
30
31
32
33
34
35
36
37
38
# File 'lib/benchmark_harness/stats.rb', line 29

def mean
  data = self

  # Check to make sure there are numbers to avoid division by 0
  if data.count > 0
    self.sum / data.count.to_f
  else
    0
  end
end

#medianObject

take in the array of numbers and calculate the median



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/benchmark_harness/stats.rb', line 86

def median
  data = self

  halfway = data.count / 2

  # Sort the array
  data = data.sort

  # The median will be different based on the number of numbers in the array
  # If there is an even number in the array
  if(data.count % 2) == 0
    median = (data[halfway] + data[halfway-1]) / 2.0

  # Else, there is an odd number of elements in the array
  else
    median = data[halfway]
  end

  median
end

#modeObject

take in an array of numbers and return the mode



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/benchmark_harness/stats.rb', line 115

def mode
  data = self

  # Sort the array
  data = data.sort

  # create a flag to tell the user if all the numbers only appear once
  no_mode = true

  # The variable that will hold the highest number
  highest_value = 0

  # The variable that holds the most time the value appears
  most_times = 0

  # Create a new hash to hold the numbers
  tmp = Hash.new

  # Populate the hash
  data.each do |num|
    if tmp["#{num}"].nil? == false
      tmp["#{num}"] = tmp["#{num}"].to_i + 1
    else
      tmp["#{num}"] = 1
    end
  end

  # Check to make sure that there is a mode
  data.each do |num|
    if tmp["#{num}"].to_i > 1
      no_mode = false
    end
  end

  if no_mode == true
    0
  else
    data.each do |num|
      if tmp["#{num}"].to_i > most_times
        highest_value = num
        most_times = tmp["#{num}"]
      end 
    end

    # now loop through again just to make sure another number doesn't appear an equal number of times
    data.each do |num|
      if num != highest_value
        if tmp["#{num}"].to_i == most_times
          no_mode = true
        end
      end
    end

    if no_mode == true
      0
    else
      highest_value
    end
  end
end

#rangeObject

take in an array of numbers and calculate the range



108
109
110
111
112
# File 'lib/benchmark_harness/stats.rb', line 108

def range
  data = self
  data = data.sort
  data[data.count-1] - data[0]
end

#standard_deviationObject

take in an array of numbers and calculate the standard deviation



62
63
64
65
66
67
68
69
70
71
# File 'lib/benchmark_harness/stats.rb', line 62

def standard_deviation
  data = self
  sum_of_deviations_squared = self.sum_of_deviations_squared

  if data.count > 1 
    Math::sqrt(sum_of_deviations_squared / (data.count-1))
  else
    0
  end
end

#sum_of_deviations_squaredObject

this is an internat function (technically the developer can use it but should have no need) this function returns the sum of each squared difference of mean



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/benchmark_harness/stats.rb', line 43

def sum_of_deviations_squared
  data = self

  deviations = Array.new
  average = self.mean
  sum_of_deviations_squared = 0

  data.each do |num|
    deviations.push((num-average)**2)
  end

  deviations.each do |num|
    sum_of_deviations_squared += num
  end

  sum_of_deviations_squared
end

#varianceObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/benchmark_harness/stats.rb', line 73

def variance
  data = self
  average = self.mean
  sum_of_deviations = self.sum_of_deviations_squared

  if data.count > 0
    sum_of_deviations / data.count.to_f
  else
    0
  end
end