Newer
Older
Rameez Remsudeen
committed
# Statistics Module: Computes basic statistics
class Statistics
def mean(a)
if a.nil? || a.empty?
return "--"
else
result = (a.inject(0) { |sum, el| sum + el }.to_f) / a.to_a.length
def variance(a)
n = 0
mean = 0.0
s = 0.0
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)
if len.even?
result = (a[len / 2 - 1] + a[len / 2]) / 2.0
end
def stats(pop)
pop = pop.compact
result = {}
[:median, :min, :max, :mean, :stddev].each do |stat|