How can I do standard deviation in Ruby?

后端 未结 9 1456
一个人的身影
一个人的身影 2021-01-30 15:45

I have several records with a given attribute, and I want to find the standard deviation.

How do I do that?

9条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-30 16:52

    The presented computation are not very efficient because they require several (at least two, but often three because you usually want to present average in addition to std-dev) passes through the array.

    I know Ruby is not the place to look for efficiency, but here is my implementation that computes average and standard deviation with a single pass over the list values:

    module Enumerable
    
      def avg_stddev
        return nil unless count > 0
        return [ first, 0 ] if count == 1
        sx = sx2 = 0
        each do |x|
          sx2 += x**2
          sx += x
        end
        [ 
          sx.to_f  / count,
          Math.sqrt( # http://wijmo.com/docs/spreadjs/STDEV.html
            (sx2 - sx**2.0/count)
            / 
            (count - 1)
          )
        ]
      end
    
    end
    

提交回复
热议问题