class Array
Extensions to the Array
class to calculate quartiles, outliers, and statistics
Public Instance Methods
compact_empty()
click to toggle source
# File lib/spout/helpers/array_statistics.rb, line 5 def compact_empty compact.reject { |a| a.is_a?(Spout::Models::Empty) } end
compact_max()
click to toggle source
# File lib/spout/helpers/array_statistics.rb, line 84 def compact_max compact_empty.max end
compact_min()
click to toggle source
# File lib/spout/helpers/array_statistics.rb, line 80 def compact_min compact_empty.min end
major_outliers()
click to toggle source
# File lib/spout/helpers/array_statistics.rb, line 99 def major_outliers array = compact_empty.sort.select { |v| v.is_a?(Numeric) } q1 = (array.quartile_one + array.quartile_two).median q3 = (array.quartile_three + array.quartile_four).median return [] if q1.nil? || q3.nil? iq_range = q3 - q1 outer_fence_lower = q1 - iq_range * 3 outer_fence_upper = q3 + iq_range * 3 array.select { |v| v > outer_fence_upper || v < outer_fence_lower } end
mean()
click to toggle source
# File lib/spout/helpers/array_statistics.rb, line 13 def mean array = compact_empty return nil if array.size == 0 array.inject(:+).to_f / array.size end
median()
click to toggle source
# File lib/spout/helpers/array_statistics.rb, line 32 def median array = compact_empty.sort return nil if array.size == 0 len = array.size len.odd? ? array[len / 2] : (array[len / 2 - 1] + array[len / 2]).to_f / 2 end
minor_outliers()
click to toggle source
# File lib/spout/helpers/array_statistics.rb, line 110 def minor_outliers outliers - major_outliers end
n()
click to toggle source
# File lib/spout/helpers/array_statistics.rb, line 9 def n compact_empty.count end
outliers()
click to toggle source
# File lib/spout/helpers/array_statistics.rb, line 88 def outliers array = compact_empty.sort.select { |v| v.is_a?(Numeric) } q1 = (array.quartile_one + array.quartile_two).median q3 = (array.quartile_three + array.quartile_four).median return [] if q1.nil? || q3.nil? iq_range = q3 - q1 inner_fence_lower = q1 - iq_range * 1.5 inner_fence_upper = q3 + iq_range * 1.5 array.select { |v| v > inner_fence_upper || v < inner_fence_lower } end
quartile_four()
click to toggle source
# File lib/spout/helpers/array_statistics.rb, line 73 def quartile_four sizes = quartile_sizes start = sizes[0] + sizes[1] + sizes[2] stop = start + sizes[3] - 1 self[start..stop] end
quartile_one()
click to toggle source
# File lib/spout/helpers/array_statistics.rb, line 55 def quartile_one self[0..(quartile_sizes[0] - 1)] end
quartile_sizes()
click to toggle source
# File lib/spout/helpers/array_statistics.rb, line 43 def quartile_sizes quartile_size = count / 4 quartile_fraction = count % 4 quartile_sizes = [quartile_size] * 4 (0..quartile_fraction - 1).to_a.each do |index| quartile_sizes[index] += 1 end quartile_sizes end
quartile_three()
click to toggle source
# File lib/spout/helpers/array_statistics.rb, line 66 def quartile_three sizes = quartile_sizes start = sizes[0] + sizes[1] stop = start + sizes[2] - 1 self[start..stop] end
quartile_two()
click to toggle source
# File lib/spout/helpers/array_statistics.rb, line 59 def quartile_two sizes = quartile_sizes start = sizes[0] stop = start + sizes[1] - 1 self[start..stop] end
sample_variance()
click to toggle source
# File lib/spout/helpers/array_statistics.rb, line 19 def sample_variance array = compact_empty m = array.mean sum = array.inject(0) { |a, e| a + (e - m)**2 } sum / (array.length - 1).to_f end
standard_deviation()
click to toggle source
# File lib/spout/helpers/array_statistics.rb, line 26 def standard_deviation array = compact_empty return nil if array.size < 2 Math.sqrt(array.sample_variance) end
unknown()
click to toggle source
# File lib/spout/helpers/array_statistics.rb, line 39 def unknown count { |a| a.is_a?(Spout::Models::Empty) } end