Created
October 11, 2010 06:07
-
-
Save scottdavis/620074 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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