Paul Sanchez
3/31/2005 1:59:00 AM
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