Solved – Integrate with eCDF quickly in R

numerical integrationr

I have an integral equation of the form
$$
T_1(x) = \int_0^x g(T_1(y)) \ d\hat{F}_n(y)
$$
where $\hat{F}_n$ is the empirical cdf and $g$ is a function.
I have a contraction mapping and so I am trying to solve the integral equation by using the Banach Fixed Point theorem sequence.

However, this runs very slowly in R and I am thinking it is because I am integrating using the sum() function for $x \in \hat{F}_n$ over and over again.

Is there a faster way to integrate using the empirical distribution with a function such as integrate()?

Best Answer

Defining the empirical distribution function $$ \hat{F}_n(t)=\frac{1}{n}\sum_{i=1}^n I_{[x_i,\infty)}(t) \, , $$ it follows that $$ \int_{-\infty}^\infty g(t)\,d\hat{F}_n(t) = \frac{1}{n} \sum_{i=1}^n g(x_i) \, . $$ Hence, you don't need to use integrate() to solve this problem. This kind of R code

x <- rnorm(10^6)
g <- function(t) exp(t) # say
mean(g(x))

should be super fast because it is vectorized.

Related Question