Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do standard deviation in Ruby?

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

How do I do that?

like image 334
Satchel Avatar asked Oct 13 '11 04:10

Satchel


People also ask

How do you create a standard deviation?

Step 1: Find the mean. Step 2: For each data point, find the square of its distance to the mean. Step 3: Sum the values from Step 2. Step 4: Divide by the number of data points.

What is standard deviation in array?

Returns the standard deviation, a measure of the spread of a distribution, of the array elements. The standard deviation is computed for the flattened array by default, otherwise over the specified axis.


2 Answers

module Enumerable      def sum       self.inject(0){|accum, i| accum + i }     end      def mean       self.sum/self.length.to_f     end      def sample_variance       m = self.mean       sum = self.inject(0){|accum, i| accum +(i-m)**2 }       sum/(self.length - 1).to_f     end      def standard_deviation       Math.sqrt(self.sample_variance)     end  end  

Testing it:

a = [ 20, 23, 23, 24, 25, 22, 12, 21, 29 ] a.standard_deviation   # => 4.594682917363407 

01/17/2012:

fixing "sample_variance" thanks to Dave Sag

like image 140
tolitius Avatar answered Oct 10 '22 10:10

tolitius


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.

like image 24
eprothro Avatar answered Oct 10 '22 11:10

eprothro