Sha256: 0fa44567cc973f99ee9e15f924819dac34a93b2a4f466723d53d76d3d45dfff7

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

class Array
  def mean
    return if empty?

    sum / count.to_f
  end unless method_defined? :mean

  alias_method :average, :mean unless method_defined? :average

  def median
    return if empty?

    data = sort

    halfway = data.count / 2

    if data.count.even?
      (data[halfway] + data[halfway - 1]) / 2.0
    else
      data[halfway]
    end
  end unless method_defined? :median

  def mode
    return if empty?
    return first if one?
    return if self == uniq

    frequencies = inject(Hash.new(0)) { |k,v| k[v] += 1; k }
    frequencies = frequencies.sort_by { |k,v| v }

    return if frequencies[-1][1] == frequencies[-2][1]

    frequencies.last[0]
  end unless method_defined? :mode

  def probability_distribution
    return if empty?

    total = count.to_f

    uniq.inject({}) { |result, item|
      result.update({ item => count(item) / total })
    }
  end unless method_defined? :probability_distribution

  def range
    return if empty?

    max - min
  end unless method_defined? :range

  def standard_deviation
    return if empty?
    return 0 if one?

    Math::sqrt sum_of_deviations_squared / (count - 1)
  end unless method_defined? :standard_deviation

  def sum
    reduce :+
  end unless method_defined? :sum

  def variance
    return if empty?

    sum_of_deviations_squared / count.to_f
  end unless method_defined? :variance

  protected

  def sum_of_deviations_squared
    mean = self.mean

    inject(0) { |total, number| total + ((number - mean) ** 2) }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
easystats-0.4.0.rc2 lib/easystats.rb