Module: Math

Defined in:
lib/basset/core_extensions.rb

Class Method Summary collapse

Class Method Details

.avg(pop) ⇒ Object



86
87
88
89
# File 'lib/basset/core_extensions.rb', line 86

def avg(pop)
  total = pop.inject(0) { |sum, n| sum + n }
  total.to_f / pop.count.to_f
end

.stddev(population) ⇒ Object

calculate the standard deviation of a population accepts: an array, the population returns: the standard deviation



81
82
83
# File 'lib/basset/core_extensions.rb', line 81

def stddev(population)
  sqrt(variance(population))
end

.variance(population) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/basset/core_extensions.rb', line 63

def variance(population)
  n = 0
  mean = 0.0
  s = 0.0
  population.each { |x|
    n = n + 1
    delta = x - mean
    mean = mean + (delta / n)
    s = s + delta * (x - mean)
  }
  # if you want to calculate std deviation
  # of a sample change this to "s / (n-1)"
  return s / n
end