[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

basic statistics library?

peajoe

3/30/2005 7:10:00 PM

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
11 Answers

Jeff Fry

3/30/2005 7:29:00 PM

0

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



Eric Hodel

3/30/2005 7:33:00 PM

0

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://se...
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04



Hal E. Fulton

3/30/2005 7:37:00 PM

0

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



dblack

3/30/2005 7:38:00 PM

0

mark sparshatt

3/30/2005 7:41:00 PM

0

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



Florian Gross

3/30/2005 8:04:00 PM

0

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/in...

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

Belorion

3/30/2005 8:09:00 PM

0

If you are running on a *nix friendly station, take a look at ruby-gsl
(http://raa.ruby-lang.org/project...), 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 ;)

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


Randy Kramer

3/30/2005 9:06:00 PM

0

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

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



Eric Hodel

3/30/2005 10:38:00 PM

0

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

> [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.

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

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

Paul Sanchez

3/31/2005 1:59:00 AM

0

In article <no_spam-7132D1.13101030032005@news1.east.earthlink.net>,
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

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