Skip to content

Instantly share code, notes, and snippets.

@DavidEGrayson
Created October 3, 2014 21:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidEGrayson/e64962e8281ac8b1d637 to your computer and use it in GitHub Desktop.
Save DavidEGrayson/e64962e8281ac8b1d637 to your computer and use it in GitHub Desktop.
This is my cleaned-up version http://stackoverflow.com/a/7749613/28128
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
@benjiwheeler
Copy link

note that this is sample stdev, not population -- should prolly name it explicitly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment