In this experiment, we first roll a fair 6-sided die, and let that number be n. Then we flip a coin n times with probability of heads = p. X is the number of heads obtained.
I know that E(X) would be np, but since n is dependent on the die roll, finding the sum of P(X=1), P(X=2), ... manually I got 1/6(p) + 1/6(2p) + 1/6(3p) + ... eventually getting 21p/6 for the mean. Is this calculation correct?
And then to find the variance E(X^2) - (E(X))^2, would it be 91p/6 - 49p^2/4?
Thanks
You can compute the expected number of heads using the law of total expectation:
https://en.wikipedia.org/wiki/Law_of_total_expectation
Let $A$ denote the outcome of the die roll (the number showing on the face-up side of the die after it's been rolled) and let $X$ denote the outcome of the coin flips.
Computing $E[X ~|~ A=a]$ is trivial (i.e., what is the expected number of heads given that the value on the rolled die is '$a$'?).
And then you just need to compute $$ E[X] = E[E[X ~|~ A]] = \sum_{a=1}^6 P(A=a) \cdot E[X ~|~ A=a], $$ where $E[E[X ~|~ A]]$ means the expected value of $E[X~|~A]$ where the expectation is over the possible values that $A$ takes on.
In other words, think of $E[X~|~A]$ as a random variable whose value depends on the realized value of $A$. Therefore, we can compute the expected value of this random variable, where this expectation is denoted as $E[E[X~|~A]]$.