Skip to content

Instantly share code, notes, and snippets.

@joaohornburg
Created October 17, 2014 16:36
Show Gist options
  • Save joaohornburg/4152d78a9afcea49ce18 to your computer and use it in GitHub Desktop.
Save joaohornburg/4152d78a9afcea49ce18 to your computer and use it in GitHub Desktop.
Standard deviation
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