Sha256: 5509d6de931fba9f9e2ed44e611319d9b916e13daacc9bb3dc5b3a32c9af2c8c

Contents?: true

Size: 734 Bytes

Versions: 7

Compression:

Stored size: 734 Bytes

Contents

module GeneValidator
  # extention of the Array Class (i.e new methods for vectors)
  module ExtraArrayMethods
    def sum
      inject(0) { |accum, i| accum + i }
    end

    def mean
      sum / length.to_f
    end

    def median
      sorted = sort
      len    = sorted.length
      (sorted[(len - 1) / 2] + sorted[len / 2]) / 2.0
    end

    def mode
      freq = inject(Hash.new(0)) { |h, v| h[v] += 1; h }
      sort_by { |v| freq[v] }.last
    end

    def sample_variance
      m   = mean
      sum = inject(0) { |accum, i| accum + (i - m)**2 }
      sum / (length - 1).to_f
    end

    def standard_deviation
      Math.sqrt(sample_variance)
    end
  end
end

class Array
  include GeneValidator::ExtraArrayMethods
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
genevalidator-1.6.5 lib/genevalidator/ext/array.rb
genevalidator-1.6.4 lib/genevalidator/ext/array.rb
genevalidator-1.6.3 lib/genevalidator/ext/array.rb
genevalidator-1.6.2 lib/genevalidator/ext/array.rb
genevalidator-1.6.1 lib/genevalidator/ext/array.rb
genevalidator-1.6.0 lib/genevalidator/ext/array.rb
genevalidator-1.5.6 lib/genevalidator/ext/array.rb