Basic statistics library?

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?

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.

Thank you,

peajoe

Hi,

I am brand new to ruby and liking it so far - particularly the watir test libraries. I am comfortable with the Java for loop syntax (i=0, i<4, i++) and haven't found something in ruby that feels as elegant, particularly when I want to use an increasing value i within the loop. Here's an example (with my limited ruby)

        i=0
        0.upto(3) do puts 'try number ' + i.to_s i = i+1 # is there a more elegant way to do i++ ?
        end

I am guessing that ruby provides a more elegant way to do the above. I would love recommendations both on how to iterate when I want to use the changing value of the iterator in the loop, and how in ruby to do i++. Is there a way with upto() to use the current value during iteration? or another iterator where that is possible without needing to define i seperately?

Thanks in advance,
Jeff Fry

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

module Enumerable

···

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?

     ##
     # 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

peajoe wrote:

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?

You might want to have a look at the NArray library that is available from RAA: http://www.ir.isas.ac.jp/~masa/ruby/index-e.html

It offers a few statistical methods and is quite fast as it is written in C.

If you are running on a *nix friendly station, take a look at ruby-gsl
(http://raa.ruby-lang.org/project/ruby-gsl/\), which if I understand
correctly are the ruby bindings to the C gsl library.

Of course, this might be overkill for doing A/B/C/D/F grading ... but
I thought I'd point it out :wink:

Matt

···

On Thu, 31 Mar 2005 04:14:44 +0900, peajoe <no_spam@invalid.net> 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?

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.

Thank you,

peajoe

In article <no_spam-7132D1.13101030032005@news1.east.earthlink.net>,

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?

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.

Thank you,

peajoe

Don't know if this will meet your needs, it's not fancy but it works for
me...

class SimpleStats
  attr_reader :n, :sampleMean, :min, :max

  def initialize
    reset
  end

  def reset
    @n = 0
    @min = 1.0 / 0.0
    @max = -@min
    @sumsq = @sampleMean = @min / @min
  end

  def newObs(datum)
    x = datum.to_f
    @max = x if !@max || x > @max
    @min = x if !@min || x < @min
    @n += 1
    if (@n > 1)
      delta = x - sampleMean
      @sampleMean += delta / @n
      @sumsq += delta * (x - @sampleMean)
    else
      @sampleMean = x
      @sumsq = 0.0
    end
  end

  def sampleVariance
    @sumsq / (@n - 1)
  end

  def stdDeviation
    Math::sqrt sampleVariance
  end
end

···

peajoe <no_spam@invalid.net> wrote:

Jeff Fry wrote:

Hi,

I am brand new to ruby and liking it so far - particularly the watir test libraries. I am comfortable with the Java for loop syntax (i=0, i<4, i++) and haven't found something in ruby that feels as elegant, particularly when I want to use an increasing value i within the loop. Here's an example (with my limited ruby)

       i=0
       0.upto(3) do

                                         puts 'try

number ' + i.to_s i = i+1 # is there a more elegant way to do i++ ?
       end

Try:

    0.upto(3) do |i|
      puts "try number #{i}"
    end

I am guessing that ruby provides a more elegant way to do the above. I would love recommendations both on how to iterate when I want to use the changing value of the iterator in the loop, and how in ruby to do i++.

i+=1 is the easiest way to increment a number.

By the way: In many/most cases, you can iterate over a collection (e.g.
an array) without worrying about the index, using #each. If you want to
iterate over the same collection and you need the index, you can use
each_with_index.

HTH,
Hal

Hi --

Hi,

I am brand new to ruby and liking it so far - particularly the watir test libraries.

Welcome :slight_smile:

I am comfortable with the Java for loop syntax (i=0, i<4, i++) and haven't found something in ruby that feels as elegant, particularly when I want to use an increasing value i within the loop. Here's an example (with my limited ruby)

      i=0
      0.upto(3) do puts 'try number ' + i.to_s i = i+1 # is there a more elegant way to do i++ ?
      end

I am guessing that ruby provides a more elegant way to do the above. I would love recommendations both on how to iterate when I want to use the changing value of the iterator in the loop, and how in ruby to do i++. Is there a way with upto() to use the current value during iteration? or another iterator where that is possible without needing to define i seperately?

You need to pass an argument to your code block:

   0.upto(3) do |i| puts "try number #{i}" end

or you could do:

   4.times {|i| puts "try number #{i}" } # here I've used the {} block
            # syntax, which is pretty
            # common for same-line blocks

or even:

   puts (0..3).map {|i| "try number #{i}" }

(iterating over a range, which you can do if the range uses integral
values)

For general-purpose incrementing, there's:

   i += 1

(There's no ++ in Ruby because numbers are immediate values. So this:

   i = 0
   i++

would actually be the same as:

   0++

which would cause all sorts of trouble :slight_smile:

David

···

On Thu, 31 Mar 2005, Jeff Fry wrote:

--
David A. Black
dblack@wobblini.net

Jeff Fry wrote:

Hi,

I am brand new to ruby and liking it so far - particularly the watir test libraries. I am comfortable with the Java for loop syntax (i=0, i<4, i++) and haven't found something in ruby that feels as elegant, particularly when I want to use an increasing value i within the loop. Here's an example (with my limited ruby)

       i=0
       0.upto(3) do puts 'try number ' + i.to_s i = i+1 # is there a more elegant way to do i++ ?
       end

how about this

0.upto(3) do |i|
   puts 'try number ' + i.to_s
end

the upto method passes the current value into the block when calling it.

also another way of writing i++ is i += 1

I am guessing that ruby provides a more elegant way to do the above. I would love recommendations both on how to iterate when I want to use the changing value of the iterator in the loop, and how in ruby to do i++. Is there a way with upto() to use the current value during iteration? or another iterator where that is possible without needing to define i seperately?

hth

···

--
Mark Sparshatt

Yes! (Probably several.)

This is off the top of my head (and I'm a newbie) so I may get some syntax
wrong:

   [0..3].each {|i| puts 'try number ' + i.to_s}

Randy Kramer

···

On Wednesday 30 March 2005 02:29 pm, Jeff Fry wrote:

        i=0
        0.upto(3) do
            puts 'try number ' + i.to_s
            i = i+1 # is there a
more elegant way to do i++ ?
        end

Please don't hijack threads by using your reply button and replacing the subject. Sensible mailers such as yours add In-Reply-To and References headers that cause responses to your hijacking attempt to be intermixed with the original thread.

Modern mailers have 'new' buttons and address books for a reason.

PGP.sig (194 Bytes)

···

On 30 Mar 2005, at 11:29, Jeff Fry wrote:

[some stuff]

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

Eric Hodel <drbrain@segment7.net> writes:

[some stuff]

Please don't hijack threads by using your reply button and replacing
the subject. Sensible mailers such as yours add In-Reply-To and
References headers that cause responses to your hijacking attempt to
be intermixed with the original thread.

Or, if you have to, remove that header line.

Modern mailers have 'new' buttons and address books for a reason.

I find myself often doing that, though.

···

On 30 Mar 2005, at 11:29, Jeff Fry wrote:

Eric Hodel - drbrain@segment7.net - http://segment7.net

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org