Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/spout/helpers/array_statistics.rb
Overview
Extensions to the Array class to calculate quartiles, outliers, and statistics
Instance Method Summary collapse
- #compact_empty ⇒ Object
- #compact_max ⇒ Object
- #compact_min ⇒ Object
- #major_outliers ⇒ Object
- #mean ⇒ Object
- #median ⇒ Object
- #minor_outliers ⇒ Object
- #n ⇒ Object
- #outliers ⇒ Object
- #quartile_four ⇒ Object
- #quartile_one ⇒ Object
- #quartile_sizes ⇒ Object
- #quartile_three ⇒ Object
- #quartile_two ⇒ Object
- #sample_variance ⇒ Object
- #standard_deviation ⇒ Object
- #unknown ⇒ Object
Instance Method Details
#compact_empty ⇒ Object
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_max ⇒ Object
84 85 86 |
# File 'lib/spout/helpers/array_statistics.rb', line 84 def compact_max compact_empty.max end |
#compact_min ⇒ Object
80 81 82 |
# File 'lib/spout/helpers/array_statistics.rb', line 80 def compact_min compact_empty.min end |
#major_outliers ⇒ Object
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 |
#mean ⇒ Object
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 |
#median ⇒ Object
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_outliers ⇒ Object
110 111 112 |
# File 'lib/spout/helpers/array_statistics.rb', line 110 def minor_outliers outliers - major_outliers end |
#n ⇒ Object
9 10 11 |
# File 'lib/spout/helpers/array_statistics.rb', line 9 def n compact_empty.count end |
#outliers ⇒ Object
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_four ⇒ Object
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_one ⇒ Object
55 56 57 |
# File 'lib/spout/helpers/array_statistics.rb', line 55 def quartile_one self[0..(quartile_sizes[0] - 1)] end |
#quartile_sizes ⇒ Object
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_three ⇒ Object
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_two ⇒ Object
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_variance ⇒ Object
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_deviation ⇒ Object
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 |