[Numpy-discussion] online (1-shot) calculation of variance (complex)

Neal Becker ndbecker2@gmail....
Thu May 8 18:05:35 CDT 2008

Robert Kern wrote:

> On Thu, May 8, 2008 at 5:45 PM, Neal Becker <ndbecker2@gmail.com> wrote:
>> I saw some links on 1-pass recursive calculation of mean/variance.  When
>> I tried the algorithms, it did not seem to give correct results for
>> complex
>> values.  Anyone know how to correctly implement this?
> Well, exactly what did you try?
Here's my python translation:
It seems to give a variance that just converges to 0 given a vector of
gaussian r.v.:

class stat2 (object):
    def __init__(self):
        self.n = 0
        self._mean = 0
        self.M2 = 0

    def __iadd__(self, x):
        self.n += 1
        delta = x - self._mean
        self._mean += delta/self.n
        self.M2 += delta*(x - self._mean)      # This expression uses the
new value of mean

    def mean(self):
        return self._mean

    def var(self):
        return self.M2/(self.n - 1)

More information about the Numpy-discussion mailing list