# Numerically calculating the integral of the discrepancy between the empirical and theoretical distribution functions

Having a sample of observations, $$x_i, i=1,..,n$$, to test the null hypothesis stating that the data follows a specific distribution $$F_0 (x)$$, the Cram\'{e}r-von-Mises goodness-of-fit test statistic is,

$$$$\label{e1} \omega^2_n=n\int_{-\infty}^{+\infty} \left ( {\hat {F}_n(x)} - F_0 (x)\right )^2 {\mathrm {d}}F_0(x),$$$$ where $$\hat {F}_n(x)$$ is the empirical distribution function. If the test distribution $$F_0$$ has unknown parameter(s), the statistic $$\omega_n ^{2}$$ is estimated as [e.g., \cite{Evans2008}], $$$$\label{e2} \hat{\omega}_n ^{2}=\frac {1}{12n}+\sum _{i=1}^{n}\left[{\frac {2i-1}{2n}}-{\hat F}_0(x_{i:n})\right]^{2},$$$$ where $$x_{1:n}$$, $$x_{2:n}$$, $$\cdots$$, $$x_{n:n}$$ are sample order statistics. On the other hand, in some literature the statistic \cite{Cramer1928}, known as $$L_2-$$ distance,
$$$$\label{e3} L_2=\int_{-\infty}^{+\infty} \left ( {\hat {F}_n(x)} - {\hat F}_0(x)\right)^2 {\mathrm {d}}x,$$$$ has been used as a goodness-of-fit criteria to test the above mention null hypothesis, which measures the discrepancy between the empirical and theoretical distribution functions. This distance has the disadvantage that it is not distribution-free; thus if we want to apply this distance for testing goodness-of-fit, then the critical values depend on $$F_0$$.

My question now is how to calculate (using a program in R) this test statistics and even more difficult question how to calculate its corresponding p-value? To be more specific consider the following simulated sample

> set.seed(12345)
> x<-rexp(30,rate=2)
> hatLambda<-1/mean(x)
> hatLambda
[1] 1.692009 #so we fit Exp. dist. with this rate to the observed sample in x


Now I want to calculate the $$L_2-$$ distance, which show the distance between fitted exponential distribution and the empirical distribution, numerically. As asked before how to calculate the corresponding p-value (second priority)?

To more point please

1) I am already aware that calculating $$\hat{\omega}_n ^{2}$$ is so simple, but the reason for trying more difficult task when calculating $$L_2-$$ distance is comparing my results with some already available results.

2) when i hopefully get the answer, I will calculate $$L_2-$$ distance for testing available data against more complicated distributions like Pareto, lognormal, etc.

Many thanks in advance.