Cramer von Mises test statistic I am trying to derive the Cramer von Mises test statistic 
$$nC_{n}=\frac{1}{12n}+\sum_{i=1}^{n}\left(U_{(i)}-\frac{2i-1}{2n}\right)^2$$
where $U_{(i)}=F_{0}(X_{(i)})$ the order statistics
from the original 
$$C_{n}:=\int (\hat{F}_{n}(t)-F_{0}(t))^2dF_{0}(t)$$
Could anyone help me with this? I'm especially confused as to how to write the integral as a sum here.
 A: Let $X_1,\dots,X_n$ be a random sample of size $n$ from $f(x)$.
  The empirical CDF is
  \begin{equation}
    \hat{F}_n(x) = \frac{1}{n} \sum_{i=1}^n I(X_i \leq x)
    = \begin{cases}
      0, & x < X_{1:n},\\
      i/n, & X_{i:n} \leq x < X_{i+1:n},\\
      1, & X_{n:n} \leq x.
    \end{cases}
  \end{equation}
Let $U_i = F_0(X_{i:n})$, then
\begin{align*}
    C_n &\triangleq \int \left[ \hat{F}_n (x) - F_0(x) \right]^2 \,\mathrm{d}{F_0(x)}
\\
&=\int_{-\infty}^{X_{1:n}} \left[ - F_0(x) \right]^2 \,\mathrm{d}{F_0(x)}
    + \sum_{i=1}^{n-1} \int_{X_{i:n}}^{X_{i+1:n}} \left[ \frac{i}{n} - F_0(x) \right]^2 \,\mathrm{d}{F_0(x)} + \int_{X_{n:n}}^{\infty} \left[ 1 - F_0(x) \right]^2 \,\mathrm{d}{F_0(x)}
    \\
    &= \frac{1}{3} U_1^3
    + \frac{1}{3} \sum_{i=1}^{n-1} \left[
        \left( U_{i+1} - \frac{i}{n} \right)^3
      - \left( U_{i} - \frac{i}{n} \right)^3
    \right]
    - \frac{1}{3} (U_n - 1)^3
    \\
    &= \frac{1}{3} U_1^3
    + \frac{1}{3} \sum_{i=1}^{n-1} \left[
      U_{i+1}^3 - U_{i}^3
      + \frac{3 i^2}{n^2} \left(U_{i+1} - U_i\right)
      - \frac{3 i}{n} \left(U_{i+1}^2 - U_i^2\right)
    \right]
    - \frac{1}{3} (U_n - 1)^3
    \\
    &= \frac{1}{3} U_1^3
    + \frac{1}{3} U_n^3 - \frac{1}{3} U_1^3
    + \left( U_n - \sum_{i=1}^n \frac{2 i - 1}{n^2} U_i \right)
    - \left( U_n^2 - \sum_{i=1}^n \frac{1}{n} U_i^2 \right)
    - \frac{1}{3} (U_n - 1)^3
    \\
    &= \frac{1}{3} + \frac{1}{n} \sum_{i=1}^n \left( U_i^2 - \frac{2 i - 1}{n} U_i \right)
    \\
    &= \frac{1}{3} + \frac{1}{n} \sum_{i=1}^n \left( U_i - \frac{2 i - 1}{2 n} \right)^2
    - \frac{1}{n} \sum_{i=1}^n \left( \frac{2 i - 1}{2 n} \right)^2
    \\
    &= \frac{1}{12 n^2} + \frac{1}{n} \sum_{i=1}^n \left( U_i - \frac{2 i - 1}{2 n} \right)^2.
    \qquad
    \left[\text{by}~\sum_{i=1}^n (2 i - 1)^2 = \frac{1}{3} n (4 n^2 - 1) \right]
\end{align*}
A: In fact, it is not hard to think about the proof. We have some assumptions beforehand. Let $\left\{ X_i \right\}_{1\le i \le n}$ be a sequence of iid random variables which are distributed with an absolutely continuous distribution $F_0$ with respect to the Lebesgue measure. Further, we denote $\hat{F_n}$ as the empirical distribution. Apparently, $\hat{F_n}(s) = n^{-1} \sum_k I(X_k \le s)$, where $I(\cdot)$ is the indicator function.
Since by definition, we have
$$ nC_n = n \int\left[ \hat{F_n}(s) - F_0 (s) \right]^2 dF_0 = n \int_0^1 \left( G_n(t) - t \right)^2 dt $$
where $G_n(t) = n^{-1} \sum_{k=1}^n I\left(U_k \le t \right)$ and $U_i = F_0(X_i)$ for all $ 1 \le i \le n$. As a result, $G_n(t)$ is also an empirical distribution of the iid random variable of $\left\{ U_i \right\}_{1 \le i \le n }$, where $U_i \sim U[0,1]$.
Therefore, by applying the order statistics $U_{(i)}$ with $U_{(1)} \le \cdots \le U_{(n)}$, it follows that $$ G_n(t) = n^{-1}\sum_k I( U_{k} \le t) = n^{-1}k I(U_{(k)} \le t) \text{ if } G_n(t) \in [k/n, (k+1)/n) .$$
Finally,
\begin{align*}
nC_n &= n \int_0^1 \left( G_n(t) - t \right)^2 dt \\
&= \frac{1}{n} \int_0^1 \left[ \sum_{k=1}^n ( I (U_k \le t) - t ) \right]^2 dt\\
&= \frac{1}{n} \sum_{k=0}^{n-1} \int_{k/n}^{(k+1)/n} \left( kI (U_{(k)} \le t) - t \right)^2 dt \\
\end{align*} 
