How can I do standard deviation in Ruby?

后端 未结 9 1463
一个人的身影
一个人的身影 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:30

    It appears that Angela may have been wanting an existing library. After playing with statsample, array-statisics, and a few others, I'd recommend the descriptive_statistics gem if you're trying to avoid reinventing the wheel.

    gem install descriptive_statistics
    
    $ irb
    1.9.2 :001 > require 'descriptive_statistics'
     => true 
    1.9.2 :002 > samples = [1, 2, 2.2, 2.3, 4, 5]
     => [1, 2, 2.2, 2.3, 4, 5] 
    1.9.2p290 :003 > samples.sum
     => 16.5 
    1.9.2 :004 > samples.mean
     => 2.75 
    1.9.2 :005 > samples.variance
     => 1.7924999999999998 
    1.9.2 :006 > samples.standard_deviation
     => 1.3388427838995882 
    

    I can't speak to its statistical correctness, or your comfort with monkey-patching Enumerable; but it's easy to use and easy to contribute to.

提交回复
热议问题