Get sigma value : Math extensions « Development « Ruby






Get sigma value

def mean(x)
  sum=0
  x.each {|v| sum += v}
  sum/x.size
end
def variance(x)
  m = mean(x)
  sum =0.0
  x.each {|v| sum += (v-m)**2 }
  sum/x.size
end

def sigma(x)
  Math.sqrt(variance(x))
end

data = [2,3,2,2,3,4,5,5,4,3,4,1,2 ]
puts sigma(data)     # 1.20894105

 








Related examples in the same category

1.Add function to Math module
2.Your own math function
3.Get mean value
4.Get hmean value
5.Return the gmean value
6.Get the median value
7.Get the mode value
8.Get variance value