# How to find the generating function of a compound random variable?

I have the following compound binomial random variable:

$$B_n \sim \operatorname{Binomial}(X_n, 1-p)$$, where $$X_n$$ is itself another random variable.

This means that $$(B_n \mid X_n = x_n) \sim\operatorname{Binomial} (x_n, 1-p)$$

I understand that $$E(B_n)$$ = $$E(E(B_n | X_n))$$

= $$E((1-p)*X_n)$$ = $$(1-p) * E(X_n)$$

And then I can calculate $$E(X_n)$$ based on whatever the distribution of the random variable $$X_n$$ is.

Now, I am trying to calculate the generating function of $$B_n$$:

$$E(t^{B_n})$$

But I am getting stuck in the way the Binomial random variable is actually $$B_n$$ | $$X_n$$

(so just the unconditional $$B_n$$ is not Binomial)

Given this, what other way can I use to calculate $$E(t^{B_n})$$ ? Thanks for any help.

If you know the generating function of a Binomial random variable, then you get that $$E(t^{B_n} \, | \, X_n) = (p+(1-p)t)^{X_n}$$ If you know the distribution of $$X_n$$. then you may use the law of total expectation to get: $$E(t^{B_n}) = E[E(t^{B_n} \, | \, X_n)] = E\left[ (p+(1-p)t)^{X_n} \right]$$