How can I find the expected value and variance of the Wilcoxon Sum of Ranks test? Given two samples $X$ and $Y$ where $X$ has $X_1,...,X_n$ independent r.v's with unknown distribution $F$ and $Y$ has $Y_1,...,Y_m$ independent r.v's with unknown distribution $G$, find the expected value and variance of 
$$R=\sum_{i=1}^{n+m}iA_i$$
(where $A_i$ is $1$ iff the $i$th smallest value of $X$ and $Y$ belongs to $X$ and is zero otherwise) assuming that $F=G$

If $F=G$ then I believe that implies that we have $n+m$ independent and identically distributed random variables, and therefore $\mathbb{P}(A_i=1)=\frac {n}{n+m}$ making $\mathbb{E}[R]=\frac {n(n+m+1)}{2}$. The variance causes me more trouble because $R$ is a sum of random variables and I am therefore not sure if using $\mathbb{E}\left[\binom{R}{2}\right]$ to calculate $\mathbb{E}[R^2]$ would be a good idea or if I need to expand $R^2$ and then use linearity of expectation. 

I am not so certain about either of these results and even if they were correct, I am not satisfied with the way I obtained them. Is there a more rigorous and systematic way of getting to these answers? What if I had had to compute the expected value and variance of $R$ when $F\ne G$?

I would like to know how to obtain the $\mathbb{P}(A_i=1)$ so that I can calculate $\mathbb{E}[A_i]$ and consequently the expectation of $R$. I would like to do this formally, not just by saying that "because all the random variables in $X$ and $Y$ are equally likely to be the $i$th smallest, then $\mathbb{P}(A_i=1)=\frac {n}{n+m}$". I feel like this argument is a little hand wavy and dismissive, at least I would like to know if it could be derived from identities or other formulas. 
 A: Let $F$ be the distribution of the sample $\mathbf{X}=(X_{1},...,X_{n1})$ and $G$ be the distribution of the sample $\mathbf{Y}=(Y_{1},...,Y_{n2})$. Furthermore, assume that both $F$ and $G$ are continuous distributions and that $X_{i}$ is independent of $Y_{j}$ for all $i,j$. Let $\mathbf{S}=(X_{1},...,X_{n1},Y_{1},...,Y_{n2})$ denote the pooled sample and $ \mathbf{R}=rank(\mathbf{S)}. $ Let us denote $N=n_{1}+n_{2}$. Finally, let us consider the r.v. $R_{X}$, the sum of the ranks of the observations from $\mathbf{X} $ in the pooled sample, defined as $$ R_{X}= \sum_{i=1}^{n1} R_{i}$$
We know that, under $H_{0}: F = G $, the $R_{i}$'s are uniformly distributed on the set $\{1,...,N\}$.
Now $E[R_{i}] = \frac{1}{N} \sum_{i=1}^{N} R_{i} = \frac{1}{N} \frac{N (N+1)}{2}=\frac{N+1}{2} $. In a similar way, it is easy to demonstrate that $var(R_{i})= \frac{N^2 -1}{12} $, i.e. by computing $E[R_{i}^2] = \frac{(N+1)(2N+1)}{6} $ and using the definition of the variance $var(R_{i}) = E[R_{i}^2] -\big( E[R_{i}] \big)^2$.
Furthermore, the covariance $cov(R_{i},R_{j}) = -\frac{(N+1)}{12}$. You can proove it by posing $var(\sum_{i=1}^{N} R_{i}) = \sum_{i=1}^{N} var(R_{i}) + \sum_{i=1}^{N} \sum_{j=1}^{N-1} cov(R_{i}, R_{j})$ for $i \neq j$. And solve for $cov(R_{i}, R_{j})$. Recall that $ \sum_{i=1}^{N} R_{i}$ is a constant (equal to $\frac{N(N+1)}{2}$), and hence, the left hand side of the previous equation, $var(\sum_{i=1}^{N} R_{i})$, is $0$.
I answer your question now. To compute $E[R_{X}]$, we have:
\begin{align*} 
E[R_{X}] =& \ \sum_{i=1}^{n_{1}} E[R_{i}] \\
=& \  \  n_{1} \frac{N+1}{2} \\
=&  \  \  n_{1}   \frac{ n_{1}+n_{2} +1}{2}
\end{align*}
To compute $var(R_{X})$, we have:
$$var(R_{X})= \sum_{i=1}^{n_{1}}var(R_{i}) + \sum_{i=1}^{n_{1}} \sum_{j=1}^{n_{1}-1}cov(R_{i},R_{j}) $$ where $ var(R_{i})= \frac{N^2 -1}{12} $ and $ cov(R_{i},R_{j})=- \frac{(N+1)}{12} $. By replacing your results in this equation you get
\begin{align*} 
var(R_{X}) =& \ \ n_{1} \ \frac{N^2 -1}{12} + n_{1} \ (n_{1} -1) \bigg(-\frac{N+1}{12} \bigg)\\
=& \ \ \frac{n_{1}}{12} \ \bigg[  N^2 -1 - (n_{1}-1) \ (N+1) \bigg] \\
=& \ \ \frac{n_{1} \ (N+1)}{12} \bigg[ N -1 - (n_{1} -1) \bigg]  \\
=& \ \ \frac{n_{1} \ n_{2}\ (N+1)}{12} .
\end{align*}
As a side remark, we will note that the Mann-Whitney-Wilcoxon test (or Wilcoxon rank sum test) test statistic $W$ is defined as $W := 2 R_{X} - n_{1} (N+1)$ so that it may have 0 as its mean.
This is an old post and you surely are working on something else, but I hope it helps anyway.
