I have a matrix z (3 x 20000). Consider each row as a random variable and each column as one simulation. I wrote the following function in R using apply command to find the empirical cumulative distribution function (EMP.CDF) in 3 dimensions. This k-variate empirical CDF was explained on the page 2 of this pdf, under the section of "The Multivariate ECDF".
EMP.CDF=function(z) {
# z is a matrix (3 x 20000) and each row is a realization of a random variable
q1=z[1,];q2=z[2,];q3=z[3,]
# qi = the realization of the ith random variable, i=1,2,3
# Now I am going to evaluate the empirical cumulative distribution function at
# each column of z
# Given each column, the function should return an empirical
# cumulative probability.
d=apply(z,2, function(x) sum(q1<=x[1] & q2<=x[2] & q3<=x[3])/(length(q1)))
return(d)}
> z=matrix(0,3,20000)
> z[1,]=runif(20000,1,2)
> z[2,]=runif(20000,3,5)
> z[3,]=runif(20000,7,9)
> system.time(EMP.CDF(z))
user system elapsed
30.18 0.01 30.39
In above code k=3. Is there any way I can vectorize the above function to reduce the system time?