On 30 Mar 2005, at 11:14, peajoe wrote:

> Hi Everybody,
>
> I am looking to do some basic statistics in ruby.  Mean, Variance,
> Standard deviation, etc.   Does anyone know if there is a gem or a
> library available for this?

I have some code like this in the Production Log Analyzer, its pretty 
small

module Enumerable

     ##
     # Sum of all the elements of the Enumerable

     def sum
         return self.inject(0) { |acc, i| acc + i }
     end

     ##
     # Average of all the elements of the Enumerable
     #
     # The Enumerable must respond to #length

     def average
         return self.sum / self.length.to_f
     end

     ##
     # Sample variance of all the elements of the Enumerable
     #
     # The Enumerable must respond to #length

     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 all the elements of the Enumerable
     #
     # The Enumerable must respond to #length

     def standard_deviation
         return Math.sqrt(self.sample_variance)
     end

end

And some tests:

class TestEnumerable < Test::Unit::TestCase

     def test_sum
         assert_equal 45, (1..9).sum
     end

     def test_average
         # Ranges don't have a length
         assert_in_delta 5.0, (1..9).to_a.average, 0.01
     end

     def test_sample_variance
         assert_in_delta 6.6666, (1..9).to_a.sample_variance, 0.0001
     end

     def test_standard_deviation
         assert_in_delta 2.5819, (1..9).to_a.standard_deviation, 0.0001
     end

end

> More exactly, I want to take test scores for students and conver them 
> to
> A, B, C, D, F.  In order to calculate Grade Point Averages.

-- 
Eric Hodel - drbrain / segment7.net - http://segment7.net
FEC2 57F1 D465 EB15 5D6E  7C11 332A 551C 796C 9F04