# [R] Calculate variance/covariance with complex numbers

Ben Bolker bolker at ufl.edu
Sun Mar 28 21:58:33 CEST 2010

```
Rolf Turner wrote:
> On 28/03/2010, at 11:50 AM, Ben Bolker wrote:
>
>> Gang Chen <gangchen6 <at> gmail.com> writes:
>>
>>> Anybody knows what functions can be used to calculate
>>> variance/covariance with complex numbers? var and cov don't seem to
>>> work:
>>
>> y <- complex(real=5:1,imag=2:6)
>> z <- complex(real=1:5,imag=6:10)
>>
>> complex.var <- function(x) {
>>  mx <- mean(x)
>>  n <- length(x)
>>  mean((x-mx)^2)*n/(n-1)
>> }
>
> 	<SNIP>
>
> Don't you want variances to be positive (non-negative) reals?
>
> IIRC the population variance of a complex random variable is
>
> 	E(|X - mu|^2) not E((X - mu)^2)
>
> So shouldn't your complex.var function have Mod(x-mx)^2 where
> you currently have (x-mx)^2 ???
>
> 	cheers,
>
> 		Rolf

Probably.  To be honest, I wasn't thinking about it that carefully.
It crossed my mind to wonder what the appropriate definition was of the
variance for complex numbers, but I didn't bother to go find out.
I guess it goes to show (yet again) that advice may be worth what you
pay for it ...

cheers
Ben

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 261 bytes
Desc: OpenPGP digital signature
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100328/aba2374d/attachment.bin>
```