Skip to content

Instantly share code, notes, and snippets.

@philipperemy
Created January 13, 2021 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philipperemy/0011d612031299787b17b2a58066579c to your computer and use it in GitHub Desktop.
Save philipperemy/0011d612031299787b17b2a58066579c to your computer and use it in GitHub Desktop.
import numpy as np
# online computation of the mean and the variance.
class RollingMean:
def __init__(self):
self.st = 0.0
self.n = 0
def update(self, x):
self.st += x
self.n += 1
return self.st / self.n
class RollingStd:
def __init__(self):
self.sst = 0.0
self.st = 0.0
self.n = 0
def update(self, x):
self.sst += x ** 2
self.st += x
self.n += 1
return np.sqrt((self.sst - (self.st ** 2) / self.n) / self.n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment