[R] Calculate variance/covariance with complex numbers
Ben Bolker
bolker at ufl.edu
Sat Mar 27 23:50:27 CET 2010
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)
More information about the R-help
mailing list