Skip to content

Instantly share code, notes, and snippets.

@da-h
Forked from alexalemi/welford.py
Last active March 18, 2019 14:00
Show Gist options
  • Save da-h/1b2dde3dcedf166036d77024427c5998 to your computer and use it in GitHub Desktop.
Save da-h/1b2dde3dcedf166036d77024427c5998 to your computer and use it in GitHub Desktop.
Python Welford Algorithm
# adaption of https://gist.github.com/alexalemi/2151722
# by da-h
# minor changes
import numpy as np
class Welford(object):
""" Implements Welford's algorithm for computing a running mean
and standard deviation as described at:
http://www.johndcook.com/standard_deviation.html
can take single values or iterables
Properties:
mean - returns the mean
std - returns the std
meanfull- returns the mean and std of the mean
Usage:
>>> foo = Welford()
>>> foo(range(100))
>>> foo
<Welford: 49.5 +- 29.0114919759>
>>> foo([1]*1000)
>>> foo
<Welford: 5.40909090909 +- 16.4437417146>
>>> foo.mean
5.409090909090906
>>> foo.std
16.44374171455467
>>> foo.meanfull
(5.409090909090906, 0.4957974674244838)
"""
def __init__(self,lst=None):
self.count = 0
self.M = 0
self.M2 = 0
self.__call__(lst)
def update(self,x):
if x is None:
return
self.count += 1
delta = x - self.M
self.M += delta / self.count
delta2 = x - self.M
self.M2 += delta*delta2
def __call__(self,x):
self.update(x)
@property
def mean(self):
if self.count<=2:
return float('nan')
return self.M
@property
def var(self,samplevar=True):
if self.count<=2:
return float('nan')
return self.M2/(self.count if samplevar else self.count -1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment