statistics.rb 1.29 KB
Newer Older
# Statistics Module: Computes basic statistics
class Statistics
  def mean(a)
    if a.nil? || a.empty?
      return "--"
    else
Daniel Bucci's avatar
Daniel Bucci committed
      result = (a.inject(0) { |sum, el| sum + el }.to_f) / a.to_a.length
Daniel Bucci's avatar
Daniel Bucci committed
    result.round(1)
Daniel Bucci's avatar
Daniel Bucci committed
  # I stole this from the internets.
  def variance(a)
    n = 0
    mean = 0.0
    s = 0.0
Daniel Bucci's avatar
Daniel Bucci committed
    a.each do |x|
      n += 1
      delta = x - mean
Daniel Bucci's avatar
Daniel Bucci committed
      mean += (delta / n)
      s += delta * (x - mean)
    end
    s / n
  end

  # calculate the standard deviation of a population
  # accepts: an array, the population
  # returns: the standard deviation
  def stddev(a)
    if a.nil? || a.empty?
      "--"
    else
      Math.sqrt(variance(a)).round(1)
    end
  end

  def max(a)
    if a.nil? || a.empty?
      "--"
    else
      a.to_a.max.round(1)
    end
  end

  def min(a)
    if a.nil? || a.empty?
      "--"
    else
      a.to_a.min.round(1)
    end
  end

  def median(a)
Daniel Bucci's avatar
Daniel Bucci committed
    return "--" if a.nil? || a.empty?

    a = a.sort
    len = a.size
Daniel Bucci's avatar
Daniel Bucci committed
    if len.even?
      result = (a[len / 2 - 1] + a[len / 2]) / 2.0
Daniel Bucci's avatar
Daniel Bucci committed
      result = a[len / 2].to_f
Daniel Bucci's avatar
Daniel Bucci committed
    result.round(1)
  end

  def stats(pop)
    pop = pop.compact
    result = {}
    [:median, :min, :max, :mean, :stddev].each do |stat|
Daniel Bucci's avatar
Daniel Bucci committed
      result[stat] = send(stat, pop)
Daniel Bucci's avatar
Daniel Bucci committed
    result