mean and standard deviation : Utility Method « Method « Ruby






mean and standard deviation

def mean(array)
  array.inject(0) { |sum, x| sum += x } / array.size.to_f
end
def mean_and_standard_deviation(array)
  m = mean(array)
  variance = array.inject(0) { |variance, x| variance += (x - m) ** 2 }
  return m, Math.sqrt(variance/(array.size-1))
end

puts mean_and_standard_deviation([1,2,3,1,1,2,1])

puts mean_and_standard_deviation([1,2,3,1,1,2,1000])

 








Related examples in the same category

1.Taking Mean
2.Track Median
3.Track Mode
4.mean without float conversion
5.Converting Between Degrees and Radians