Created
October 17, 2014 16:36
-
-
Save joaohornburg/4152d78a9afcea49ce18 to your computer and use it in GitHub Desktop.
Standard deviation
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 | |
self.inject(:+) | |
end | |
def mean | |
self.sum/self.length.to_f | |
end | |
def sample_variance | |
m = self.mean | |
sum = self.inject(0){|accum, i| accum +(i-m)**2 } | |
sum/(self.length - 1).to_f | |
end | |
def std_dev | |
return Math.sqrt(self.sample_variance) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment