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

Robert Kern robert.kern@gmail....
Thu May 8 18:16:03 CDT 2008

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