How can I do standard deviation in Ruby?

后端 未结 9 1447
一个人的身影
一个人的身影 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条回答
  •  梦毁少年i
    2021-01-30 16:37

    Or how about:

    class Stats
        def initialize( a )
            @avg = a.count > 0 ? a.sum / a.count.to_f : 0.0
            @stdev = a.count > 0 ? ( a.reduce(0){ |sum, v| sum + (@avg - v) ** 2 } / a.count ) ** 0.5 : 0.0
        end
    end
    

提交回复
热议问题