lib/easystats.rb in easystats-0.2.0 vs lib/easystats.rb in easystats-0.3.0

- old
+ new

@@ -1,169 +1,143 @@ class Array + # take in an array of numbers and calculate the mean (average) + def mean + return unless self.any? - # take in an array of numbers and calculate the sum - def sum - data = self - sum_of_numbers = 0 + self.sum / self.count.to_f + end unless Array.instance_methods.include? "mean" + alias_method :average, :mean unless Array.instance_methods.include? "average" - # 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 + # take in the array of numbers and calculate the median + def median + return unless self.any? - sum_of_numbers - end unless Array.instance_methods.include? "sum" + data = self - # take in an array of numbers and calculate the mean (average) - def mean - data = self + halfway = data.count / 2 - # 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 unless Array.instance_methods.include? "mean" - alias_method :average, :mean unless Array.instance_methods.include? "average" + # Sort the array + data = data.sort - # take in an array of numbers and calculate the standard deviation - def standard_deviation - data = self - sum_of_deviations_squared = self.sum_of_deviations_squared + # 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 - if data.count > 1 - Math::sqrt(sum_of_deviations_squared / (data.count-1)) - else - 0 - end - end unless Array.instance_methods.include? "standard_deviation" + # Else, there is an odd number of elements in the array + else + median = data[halfway] + end - 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 unless Array.instance_methods.include? "variance" + median + end unless Array.instance_methods.include? "median" - # take in the array of numbers and calculate the median - def median - data = self + # take in an array of numbers and return the mode + def mode + return unless self.any? - halfway = data.count / 2 + # Sort the array + data = self.sort - # Sort the array - data = data.sort + # create a flag to tell the user if all the numbers only appear once + no_mode = true - # 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 + # The variable that will hold the highest number + highest_value = 0 - # Else, there is an odd number of elements in the array + # 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 - median = data[halfway] + tmp["#{num}"] = 1 end + end - median - end unless Array.instance_methods.include? "median" + # Check to make sure that there is a mode + data.each do |num| + if tmp["#{num}"].to_i > 0 + no_mode = false + end + end - # take in an array of numbers and calculate the range - def range - data = self - data = data.sort - data[data.count-1] - data[0] - end unless Array.instance_methods.include? "range" - - # take in an array of numbers and return the mode - 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 + if no_mode == true + nil + else data.each do |num| - if tmp["#{num}"].nil? == false - tmp["#{num}"] = tmp["#{num}"].to_i + 1 - else - tmp["#{num}"] = 1 + if tmp["#{num}"].to_i > most_times + highest_value = num + most_times = tmp["#{num}"] end end - # Check to make sure that there is a mode + # now loop through again just to make sure another number doesn't appear an equal number of times data.each do |num| - if tmp["#{num}"].to_i > 0 - no_mode = false + if num != highest_value + if tmp["#{num}"].to_i == most_times + no_mode = true + end end end - + if no_mode == true - 0 + nil 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 - nil - else - highest_value - end + highest_value end - end unless Array.instance_methods.include? "mode" - - protected - - # this function returns the sum of each squared difference of mean - def sum_of_deviations_squared - data = self + end + end unless Array.instance_methods.include? "mode" - deviations = Array.new - average = self.mean - sum_of_deviations_squared = 0 + # take in an array of numbers and calculate the range + def range + return unless self.any? - data.each do |num| - deviations.push((num-average)**2) - end + data = self.sort + data.last - data.first + end unless Array.instance_methods.include? "range" - deviations.each do |num| - sum_of_deviations_squared += num - end + # take in an array of numbers and calculate the standard deviation + def standard_deviation + return unless self.any? + return 0 if self.one? - sum_of_deviations_squared + Math::sqrt(self.sum_of_deviations_squared / (self.count-1)) + end unless Array.instance_methods.include? "standard_deviation" + + # take in an array of numbers and calculate the sum + def sum + self.reduce { |total, number| total + number } + end unless Array.instance_methods.include? "sum" + + def variance + return unless self.any? + + self.sum_of_deviations_squared / self.count.to_f + end unless Array.instance_methods.include? "variance" + + protected + + # this function returns the sum of each squared difference of mean + 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 end