basic statistics library?

P

peajoe

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
 
J

Jeff Fry

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
 
E

Eric Hodel

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
 
H

Hal Fulton

Jeff said:
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
 
D

David A. Black

Hi --

Hi,

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

Welcome :)
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 :)


David
 
M

mark sparshatt

Jeff said:
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
 
B

Belorion

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

Matt
 
R

Randy Kramer

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
 
E

Eric Hodel

--Apple-Mail-2-771087552
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=US-ASCII; format=flowed

[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 - (e-mail address removed) - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

--Apple-Mail-2-771087552
content-type: application/pgp-signature; x-mac-type=70674453;
name=PGP.sig
content-description: This is a digitally signed message part
content-disposition: inline; filename=PGP.sig
content-transfer-encoding: 7bit

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)

iEYEARECAAYFAkJLKkIACgkQMypVHHlsnwSFIwCfWFnDaQpzfXLQIPkYT5+cVZ0m
DzcAmgNhfLWuyIvM9HQn3tTdd5b3qXEz
=dvIJ
-----END PGP SIGNATURE-----

--Apple-Mail-2-771087552--
 
P

Paul Sanchez

peajoe said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top