Created
March 30, 2018 19:24
-
-
Save vvanirudh/211117108d3a0e657872df903120cd39 to your computer and use it in GitHub Desktop.
Running mean and standard deviation
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 https://www.johndcook.com/blog/standard_deviation/ | |
# and https://github.com/modestyachts/ARS | |
class RunningStat(object): | |
def __init__(self, shape=None): | |
self._n = 0 | |
self._M = np.zeros(shape, dtype = np.float64) | |
self._S = np.zeros(shape, dtype = np.float64) | |
self._M2 = np.zeros(shape, dtype = np.float64) | |
def copy(self): | |
other = RunningStat() | |
other._n = self._n | |
other._M = np.copy(self._M) | |
other._S = np.copy(self._S) | |
return other | |
def push(self, x): | |
x = np.asarray(x) | |
# Unvectorized update of the running statistics. | |
assert x.shape == self._M.shape, ("x.shape = {}, self.shape = {}" | |
.format(x.shape, self._M.shape)) | |
n1 = self._n | |
self._n += 1 | |
if self._n == 1: | |
self._M[...] = x | |
else: | |
delta = x - self._M | |
deltaM2 = np.square(x) - self._M2 | |
self._M[...] += delta / self._n | |
self._S[...] += delta * delta * n1 / self._n | |
def update(self, other): | |
n1 = self._n | |
n2 = other._n | |
n = n1 + n2 | |
delta = self._M - other._M | |
delta2 = delta * delta | |
M = (n1 * self._M + n2 * other._M) / n | |
S = self._S + other._S + delta2 * n1 * n2 / n | |
self._n = n | |
self._M = M | |
self._S = S | |
def __repr__(self): | |
return '(n={}, mean_mean={}, mean_std={})'.format( | |
self.n, np.mean(self.mean), np.mean(self.std)) | |
@property | |
def n(self): | |
return self._n | |
@property | |
def mean(self): | |
return self._M | |
@property | |
def var(self): | |
return self._S / (self._n - 1) if self._n > 1 else np.square(self._M) | |
@property | |
def std(self): | |
return np.sqrt(self.var) | |
@property | |
def shape(self): | |
return self._M.shape |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment