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)

______________________________________________
R-help@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.

Reply via email to