Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/spout/helpers/array_statistics.rb

Overview

Extensions to the Array class to calculate quartiles, outliers, and statistics

Instance Method Summary collapse

Instance Method Details

#compact_emptyObject



5
6
7
# File 'lib/spout/helpers/array_statistics.rb', line 5

def compact_empty
  compact.reject { |a| a.is_a?(Spout::Models::Empty) }
end

#compact_maxObject



84
85
86
# File 'lib/spout/helpers/array_statistics.rb', line 84

def compact_max
  compact_empty.max
end

#compact_minObject



80
81
82
# File 'lib/spout/helpers/array_statistics.rb', line 80

def compact_min
  compact_empty.min
end

#major_outliersObject



99
100
101
102
103
104
105
106
107
108
# 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

#meanObject



13
14
15
16
17
# 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

#medianObject



32
33
34
35
36
37
# 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_outliersObject



110
111
112
# File 'lib/spout/helpers/array_statistics.rb', line 110

def minor_outliers
  outliers - major_outliers
end

#nObject



9
10
11
# File 'lib/spout/helpers/array_statistics.rb', line 9

def n
  compact_empty.count
end

#outliersObject



88
89
90
91
92
93
94
95
96
97
# 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_fourObject



73
74
75
76
77
78
# 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_oneObject



55
56
57
# File 'lib/spout/helpers/array_statistics.rb', line 55

def quartile_one
  self[0..(quartile_sizes[0] - 1)]
end

#quartile_sizesObject



43
44
45
46
47
48
49
50
51
52
53
# 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_threeObject



66
67
68
69
70
71
# 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_twoObject



59
60
61
62
63
64
# 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_varianceObject



19
20
21
22
23
24
# 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_deviationObject



26
27
28
29
30
# 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

#unknownObject



39
40
41
# File 'lib/spout/helpers/array_statistics.rb', line 39

def unknown
  count { |a| a.is_a?(Spout::Models::Empty) }
end