Skip to content

Instantly share code, notes, and snippets.

@scottdavis
Created October 11, 2010 06:07
Show Gist options
  • Select an option

  • Save scottdavis/620074 to your computer and use it in GitHub Desktop.

Select an option

Save scottdavis/620074 to your computer and use it in GitHub Desktop.
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)
}
return s / n
end
def standard_deviation(population)
Math.sqrt(variance(population))
end
def median(collection)
collection.sort!
middle = collection.size.to_f / 2
return collection.first if collection.size == 1;
a = middle.ceil
return (collection[a-1.to_i] + collection[a.to_i]) / 2 if middle % 2
collection[middle.to_i]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment