Skip to content

Instantly share code, notes, and snippets.

@lagsalot
Forked from jasonwatkinspdx/stats.rb
Created September 16, 2009 15:07
Show Gist options
  • Select an option

  • Save lagsalot/188090 to your computer and use it in GitHub Desktop.

Select an option

Save lagsalot/188090 to your computer and use it in GitHub Desktop.
ruby measurement class example
class Measurement
attr_accessor :count, :sum, :sum_squares, :min, :max
def initialize
@count, @sum, @sum_squares, @min, @max = 0, 0, 0, nil, nil
end
def record n
@count += 1
@sum += n
@sum_squares += n**2
@min ||= n
@max ||= n
@min = n if n < @min
@max = n if n > @max
end
def avg
sum.to_f / count
end
def sdev
((sum_squares - (sum*avg))/(count - 1))**0.5
end
def to_s
"avg: %12g sdev: %12g sum: %12g min: %12g max: %12g" % [avg, sdev, @sum, @min, @max]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment