Created
October 3, 2014 21:34
-
-
Save DavidEGrayson/e64962e8281ac8b1d637 to your computer and use it in GitHub Desktop.
This is my cleaned-up version http://stackoverflow.com/a/7749613/28128
This file contains 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
module Enumerable | |
def sum | |
inject(0, :+) | |
end | |
def mean | |
sum / length.to_f | |
end | |
def sample_variance | |
m = mean | |
sum = inject(0) { |accum, i| accum + (i-m)**2 } | |
sum / (length - 1).to_f | |
end | |
def standard_deviation | |
Math.sqrt sample_variance | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
note that this is sample stdev, not population -- should prolly name it explicitly.