module MathStatisticsArrayMethods::ArrayStatistics

Module to extend the array class

Public Instance Methods

combination_quantity(size_of_combination = self.length) click to toggle source

Give you the amount of possible combination for a given draw size

# 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() click to toggle source

Hash of the frequency per values of the array.

# 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() click to toggle source

Measure of center, not affected by outliers

# 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() click to toggle source

Most frequent value

# 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) click to toggle source

Give you the amount of permutation available for a given draw size if you can pick twice the same item

# 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) click to toggle source

Give you the amount of permutation available for a given draw size

# 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) click to toggle source

return the probability of a number to be picked in this set.

# 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() click to toggle source

max value minus min value

# File lib/math_statistics_array_methods/array_statistics.rb, line 101
def range
        return self.max - self.min
end
sample_average() click to toggle source

Measure of center, affected by outliers

# 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() click to toggle source

Measure of variation, “average” distance from the mean.

# File lib/math_statistics_array_methods/array_statistics.rb, line 44
def sample_standard_deviation
        return Math.sqrt(variance)
end
variance() click to toggle source

variance of this array

# 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