Skip to content

Instantly share code, notes, and snippets.

@danielnc
Created December 6, 2012 18:37
Show Gist options
  • Save danielnc/4226904 to your computer and use it in GitHub Desktop.
Save danielnc/4226904 to your computer and use it in GitHub Desktop.
Stats for streaming values(mean,variance, standard_deviation)
# Check: http://www.johndcook.com/standard_deviation.html
module OmniRPC
module Util
class StreamingStats
attr_reader :min, :max
def initialize
@m_n = 0.0
@max = -Float::INFINITY
@min = Float::INFINITY
end
def clear
@total = 0
@m_n = 0.0
@max = -Float::INFINITY
@min = Float::INFINITY
end
def push(value)
value = value.to_f
@max = [value, @max].max
@min = [value, @min].min
@m_n += 1
# See Knuth TAOCP vol 2, 3rd edition, page 232
if @m_n == 1
@m_oldM = @m_newM = value
@m_oldS = 0.0
else
@m_newM = @m_oldM + (value - @m_oldM)/@m_n
@m_newS = @m_oldS + (value - @m_oldM)*(value - @m_newM)
# set up for next iteration
@m_oldM = @m_newM
@m_oldS = @m_newS
end
end
def total
@m_n.to_i
end
def mean
@m_n > 0 ? @m_newM : 0.0
end
def variance
((@m_n > 1) ? @m_newS/(@m_n - 1) : 0.0)
end
def standard_deviation
Math.sqrt(variance())
end
def standard_error
1.96 * Math.sqrt(variance / total)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment