Thursday, January 20, 2011

Ruby Simple Statistics adding standard deviation

Ruby Simple Statistics - Standard Deviation





>> data
=> [1, 2, 3, 4, 5, 6]
>> data.class
=> Array


An array of numbers in ruby, (an Array object), can return the size/length, minimum (min) value, maximum (max) value. Missing methods are average, sample_variance, standard_deviation. These can added using an Enumerable module mixins.


Enumerable module mixins - add standard deviation to all arrays etc.


# Add methods to Enumerable, which makes them available to Array
module Enumerable

# sum of an array of numbers
def sum
return self.inject(0){|acc,i|acc +i}
end

# average of an array of numbers
def average
return self.sum/self.length.to_f
end

# variance of an array of numbers
def sample_variance
avg=self.average
sum=self.inject(0){|acc,i|acc +(i-avg)**2}
return(1/self.length.to_f*sum)
end

# standard deviation of an array of numbers
def standard_deviation
return Math.sqrt(self.sample_variance)
end

end # module Enumerable



Example Useage


# enter an interpreted ruby session to play with this:

$ irb
# create a new array called data with six numbers in it

>> data = [1, 2, 3, 4, 5, 6]
=> [1, 2, 3, 4, 5, 6]

>> data.class
=> Array

# try running the following commands. Some will work, some require the module from above.

>> data.sum
=> 21

>> data.size
=> 6

>> data.min
=> 1

>> data.max
=> 6

>> data.average
=> 3.5

>> data.sample_variance
=> 2.91666666666667

>> data.standard_deviation
=> 1.70782512765993



References

  1. ruby inject blogbackground on inject.
  2. see notes for mixins for Enumerable adding standard_deviation to Enumerable
  3. ruby core documentationSee Module Enumerable and Class Enumerable::Enumerator

No comments:

Post a Comment