Anybody knows what functions can be used to calculate variance/covariance with complex numbers? var and cov don't seem to work:> a1 V1 0.00810014+0.00169366i V2 0.00813054+0.00158251i V3 0.00805489+0.00163295i V4 0.00809141+0.00159533i V5 0.00813976+0.00161850i> var(a)1 1 1.141556e-09 Warning message: In var(a) : imaginary parts discarded in coercion> cov(a)1 1 1.141556e-09 Warning message: In cov(a) : imaginary parts discarded in coercion Thanks in advance, Gang
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:How about: 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) } complex.cov <- function(x,y) { mx <- mean(x) my <- mean(y) n <- length(x) mean((x-mx)*(y-my))*n/(n-1) } complex.var(z) complex.cov(y,z) ## check that they agree with the usual defs for y,z real: z <- 1:5 var(z)-complex.var(z) y <- c(6,5,4,2,3) cov(y,z)-complex.cov(y,z)
Charles C. Berry
2010-Mar-27 23:07 UTC
[R] Calculate variance/covariance with complex numbers
On Sat, 27 Mar 2010, Gang Chen wrote:> Anybody knows what functions can be used to calculate > variance/covariance with complex numbers? var and cov don't seem to > work:How about?> xri <- matrix(rnorm(10000)+1i*rnorm(10000),nc=2) > crossprod(xri-colMeans(xri))/(nrow(xri)-1)HTH, Chuck> >> a > 1 > V1 0.00810014+0.00169366i > V2 0.00813054+0.00158251i > V3 0.00805489+0.00163295i > V4 0.00809141+0.00159533i > V5 0.00813976+0.00161850i > >> var(a) > 1 > 1 1.141556e-09 > Warning message: > In var(a) : imaginary parts discarded in coercion > >> cov(a) > 1 > 1 1.141556e-09 > Warning message: > In cov(a) : imaginary parts discarded in coercion > > Thanks in advance, > Gang > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
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: >> How about: >> >> 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, > > RolfProbably. 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>