Module: MathStatisticsArrayMethods::ArrayStatistics
- Included in:
- Array
- Defined in:
- lib/math_statistics_array_methods/array_statistics.rb
Overview
Module to extend the array class
Instance Method Summary collapse
-
#combination_quantity(size_of_combination = self.length) ⇒ Object
Give you the amount of possible combination for a given draw size.
-
#frequencies ⇒ Object
Hash of the frequency per values of the array.
-
#median ⇒ Object
Measure of center, not affected by outliers.
-
#mode ⇒ Object
Most frequent value.
-
#permuation_repetition_quantity(size_of_permutation = self.length) ⇒ Object
Give you the amount of permutation available for a given draw size if you can pick twice the same item.
-
#permutation_quantity(size_of_permutation = self.length) ⇒ Object
Give you the amount of permutation available for a given draw size.
-
#probability_of(a) ⇒ Object
return the probability of a number to be picked in this set.
-
#range ⇒ Object
max value minus min value.
-
#sample_average ⇒ Object
Measure of center, affected by outliers.
-
#sample_standard_deviation ⇒ Object
Measure of variation, “average” distance from the mean.
-
#variance ⇒ Object
variance of this array.
Instance Method Details
#combination_quantity(size_of_combination = self.length) ⇒ Object
Give you the amount of possible combination for a given draw size
87 88 89 |
# File 'lib/math_statistics_array_methods/array_statistics.rb', line 87 def combination_quantity(size_of_combination = self.length) return length.factorial / ((length - size_of_combination).factorial * size_of_combination.factorial) end |
#frequencies ⇒ Object
Hash of the frequency per values of the array.
51 52 53 54 55 56 57 58 59 |
# File 'lib/math_statistics_array_methods/array_statistics.rb', line 51 def frequencies frequency = Hash.new self.each do |n| frequency[n] = 0 unless frequency.include? n frequency[n] += 1 end return frequency end |
#median ⇒ Object
Measure of center, not affected by outliers
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/math_statistics_array_methods/array_statistics.rb', line 20 def median sorted = self.sort middle_index = (length - 1) / 2 if length % 2 == 0 second_middle = middle_index + 1 return [ sorted[middle_index], sorted[second_middle] ].sample_average else return sorted[middle_index] end end |
#mode ⇒ Object
Most frequent value
64 65 66 |
# File 'lib/math_statistics_array_methods/array_statistics.rb', line 64 def mode return frequencies.group_by { |_,value| value }.max_by { |key,_| key }.last.map{|k, v| k} end |
#permuation_repetition_quantity(size_of_permutation = self.length) ⇒ Object
Give you the amount of permutation available for a given draw size if you can pick twice the same item
94 95 96 |
# File 'lib/math_statistics_array_methods/array_statistics.rb', line 94 def permuation_repetition_quantity(size_of_permutation = self.length) return length ** size_of_permutation end |
#permutation_quantity(size_of_permutation = self.length) ⇒ Object
Give you the amount of permutation available for a given draw size
80 81 82 |
# File 'lib/math_statistics_array_methods/array_statistics.rb', line 80 def permutation_quantity(size_of_permutation = self.length) return length.factorial / (length - size_of_permutation).factorial end |
#probability_of(a) ⇒ Object
return the probability of a number to be picked in this set.
71 72 73 74 75 |
# File 'lib/math_statistics_array_methods/array_statistics.rb', line 71 def probability_of(a) f = frequencies positive_outcome = f.include?(a) ? f[a] : 0 return positive_outcome / length.to_f end |
#range ⇒ Object
max value minus min value
101 102 103 |
# File 'lib/math_statistics_array_methods/array_statistics.rb', line 101 def range return self.max - self.min end |
#sample_average ⇒ Object
Measure of center, affected by outliers
11 12 13 14 15 |
# File 'lib/math_statistics_array_methods/array_statistics.rb', line 11 def sample_average return 0 if self.empty? total = self.inject(0){|acc, n| acc += n} return total.to_f / self.length.to_f end |
#sample_standard_deviation ⇒ Object
Measure of variation, “average” distance from the mean.
44 45 46 |
# File 'lib/math_statistics_array_methods/array_statistics.rb', line 44 def sample_standard_deviation return Math.sqrt(variance) end |
#variance ⇒ Object
variance of this array
35 36 37 38 39 |
# File 'lib/math_statistics_array_methods/array_statistics.rb', line 35 def variance avg = self.sample_average total = self.inject(0) {|acc, n| acc += (n - avg)**2 } return total / length.to_f end |