Created
June 11, 2015 05:02
-
-
Save monkut/2e8bec49b0659d941abc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import sqrt | |
class WelfordRunningVariance(object): | |
""" | |
Python implentation of Welford's running variance algorithm | |
http://www.johndcook.com/standard_deviation.html | |
""" | |
def __init__(self): | |
self._count = 0 | |
self._mean = None | |
self._last_mean = None | |
self._max = None | |
self._min = None | |
self._sum = None | |
self._s = None | |
self._last_s = None | |
def send(self, value): | |
self._count += 1 | |
if self._count == 1: | |
self._mean = value | |
self._max = value | |
self._min = value | |
self._sum = value | |
self._last_mean = value | |
self._last_s = 0.0 | |
else: | |
self._mean = self._last_mean + (value - self._last_mean)/self._count | |
self._s = self._last_s + (value - self._last_mean) * (value - self._mean) | |
self._sum += value | |
# check & update max/min values if necessary | |
if value > self._max: | |
self._max = value | |
if value < self._min: | |
self._min = value | |
# prepare for next iteration | |
self._last_mean = self._mean | |
self._last_s = self._s | |
def next(self): | |
""" | |
Mimmicing generator function | |
""" | |
return self._mean | |
def count(self): | |
return self._count | |
def mean(self): | |
return self._mean | |
def max(self): | |
return self._max | |
def min(self): | |
return self._min | |
def sum(self): | |
return self._sum | |
def var(self): | |
result = 0 | |
if self._count >= 2: | |
result = self._s/(self._count - 1) | |
return result | |
def stddev(self): | |
return sqrt(self.var()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment