lib/easystats.rb in easystats-0.0.7 vs lib/easystats.rb in easystats-0.1.0

- old
+ new

@@ -1,6 +1,6 @@ - class Array +class Array # take in an array of numbers and calculate the sum def sum data = self sum_of_numbers = 0 @@ -13,11 +13,11 @@ else sum_of_numbers = 0 end sum_of_numbers - end + end unless Array.instance_methods.include? "sum" # take in an array of numbers and calculate the mean (average) def mean data = self @@ -25,44 +25,24 @@ if data.count > 0 self.sum / data.count.to_f else 0 end - end - alias_method :average, :mean + end unless Array.instance_methods.include? "mean" + alias_method :average, :mean unless Array.instance_methods.include? "average" - # 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 - 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 - # 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 - if data.count > 1 + if data.count > 1 Math::sqrt(sum_of_deviations_squared / (data.count-1)) else 0 end - end + end unless Array.instance_methods.include? "standard_deviation" def variance data = self average = self.mean sum_of_deviations = self.sum_of_deviations_squared @@ -70,11 +50,11 @@ if data.count > 0 sum_of_deviations / data.count.to_f else 0 end - end + end unless Array.instance_methods.include? "variance" # take in the array of numbers and calculate the median def median data = self @@ -92,18 +72,18 @@ else median = data[halfway] end median - end + end unless Array.instance_methods.include? "median" # take in an array of numbers and calculate the range def range data = self data = data.sort data[data.count-1] - data[0] - end + end unless Array.instance_methods.include? "range" # take in an array of numbers and return the mode def mode data = self @@ -131,11 +111,11 @@ end end # Check to make sure that there is a mode data.each do |num| - if tmp["#{num}"].to_i > 1 + if tmp["#{num}"].to_i > 0 no_mode = false end end if no_mode == true @@ -143,11 +123,11 @@ else data.each do |num| if tmp["#{num}"].to_i > most_times highest_value = num most_times = tmp["#{num}"] - end + 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 @@ -156,12 +136,38 @@ end end end if no_mode == true - 0 + nil else highest_value end 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 + + 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 + +<<<<<<< HEAD +end +======= end +>>>>>>> origin/master