Summation of random variables Consider a random variable $X_n$ taking values in $\mathbb{N}\cup\{\infty\}$.
We have $\mathbb{E}(X_n)<\infty$
For any function $f$ and random variable $Y_n$, has the following sum finite number of term (in the sense up to $X_n<\infty$ a.s.)?
$$
\mathbb{E}(\sum_{m=0}^{X_n}f(Y_m))
$$
Thank you for your help.
 A: Let's suppose the independence between $X$ and the sequence $(Y_i)$
It is called Wald's equation
Two proofs
1) With a conditional expectation
You can write : $$\mathbb{E}\left[\sum_{i=1}^{X} Y_i\right] = \mathbb{E}\left[\mathbb{E}[\sum_{i=1}^{X} Y_i|X]\right] \tag{*}\label{*}$$
Let's compute $\mathbb{E}[\sum_{i=1}^{X} Y_i|X]$. Since the sequence $(Y_i)$ is independent from $X_n$ you have the equality :
$$\mathbb{E}[\sum_{i=1}^{X} Y_i|X_n] = f(X)$$
where $f(x) = \mathbb{E}[\sum_{i=1}^{x} Y_i] = x \mathbb{E}[Y_1]$  (the upper-bound is fixed)
Therefore $$\mathbb{E}[\sum_{i=1}^{X} Y_i|X] = X E[Y_1]$$
Using $\eqref{*}$ you get $$\mathbb{E}\left[\sum_{i=1}^{X} Y_i\right] = E[X]E[Y_1]$$
2) With a simple decomposition
$$\sum_{i=1}^X Y_i = \sum_{i = 1}^{\infty} Y_i1_{X \leq n}$$
And $$\mathbb{E}\left[\sum_{i=1}^{X} Y_i\right] = E[Y_1]\mathbb{E}\left[\sum_{i=1}^{\infty} 1_{X \leq n}\right]$$
You can show that $$\mathbb{E}\left[\sum_{i=1}^{\infty} 1_{X \leq n}\right] = E[X]$$  (if $X = n$ the sum is equal to $n$)
Edit :
As Snoop said there is version when $X = \tau$ is a stopping time with respect to the filtration $(\mathcal{F_i})_i = (\sigma(Y_1,...Y_i))_i$
$$\sum_{i=1}^{\tau} Y_i = \sum_{i=1}^{+\infty} Y_i1_{i \leq \tau} = \sum_{i=1}^{+\infty} Y_i1_{\tau > i-1}$$
And you know that $Y_i$ and $1_{\tau > i-1}$ are independent.
Indeed, $\tau$ is a stopping time so $$\{\tau > i-1\} \in \mathcal{F}_{i-1} = \sigma(Y_1,...Y_{i-1})$$
Then $$E[\sum_{i=1}^{\tau} Y_i] = \sum_{i=1}^{\infty} E[Y_i]E[1_{\tau > i-1}] = E[Y_1]E[\sum_{i=1}^{\infty} 1_{\tau > i-1}] = E[Y_1]E[\tau]$$
