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

Robert Kern robert.kern at gmail.com
Thu May 8 19:16:03 EDT 2008


On Thu, May 8, 2008 at 6:05 PM, Neal Becker <ndbecker2 at gmail.com> wrote:
> Robert Kern wrote:
>
>> On Thu, May 8, 2008 at 5:45 PM, Neal Becker <ndbecker2 at 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

You may not be able to convert this one to apply to complex numbers.
The recurrence relation may not hold. In the two-pass algorithm for
complex numbers, remember that you are summing

  (x[i] - mean).conj * (x[i] - mean)

each of which is real.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco



More information about the NumPy-Discussion mailing list