# How to calculate the binomial probability when expected frequency is a random variable

I am trying to write a simple likelihood function to calculate the binomial probability of $X$ successes from $N$ trials. The problem is that the expected proportion of successes ($p_X$) is itself a binomially-distributed variable with $Y$ successes and $M$ trials.

That is, there a simple way to calculate $\text{E}(X)$ for $X\sim \text{Bin}(N, Y/M)$ when $Y\sim \text{Bin}(M, p)$?

Is it just the product of the two probabilities?

• A binomial variable takes on values in ${0,1,2,\dots,M}$, and as such can't be an "expected proportion of successes". Do you perhaps mean that $Y/M$ is the expected proportion of successes? If so, this is an interesting model; could you explain a little how it comes about? – jbowman Nov 16 '13 at 1:21
• Yes, Y/M is the expected proportion of successes. I am a geneticist and observe a mutation at frequency q from N individuals in population 1. From population 2, I observe the same mutation with frequency q from M individuals. I would like to calculate the probability of observing q (from N observations) conditional on the 'true' frequency being p. Of course, p is estimated with error so the binomial distribution doesn't seem quite right. – Alan Nov 16 '13 at 1:39
• Can you ask about your actual problem (at this stage you may want to ask a new question)? I'd give a different answer to that. Note that either you condition on the observed $p$ (as you do ... and then don't ... in your question here), or you throw out your present formulation of the problem and actually deal with what now seems to be the underlying question (which seems to be 'do the samples suggest the two population frequencies differ?') ... a completely different problem. – Glen_b -Reinstate Monica Nov 21 '13 at 5:19

This can be done using the Law of Total Expectation.

$$\text{E} (X) = \text{E}_Y [ \text{E}_{X | Y} ( X | Y)]$$

Now $\text{E}_{X|Y}(X|Y)] = N\cdot p_x = N\cdot Y/M$, so

\begin{eqnarray} \text{E} (X) &=& \text{E}_Y [ N\cdot Y/M]\\ &=& \frac{N}{M}\cdot\text{E}_Y [Y]\\ &=& \frac{N}{M}\cdot (Mp)\\ &=& Np \end{eqnarray}

• Thanks. I understand that E(X) = p; but the probability that E(X)=p is not just Bin(N,p), if p is a random variable itself, right? – Alan Nov 16 '13 at 16:02
• Looks like you're getting muddled up. In the notation we have above, $p$ is the success probability for $Y$ and $Y/M$ (which is $p_X$) is the success probability for $X$. (You must distinguish those two probabilities; they're different values.) $E(X)$ is the expectation of $X$ unconditional on information about $Y$. $E(X|Y)$ is the expectation of $X$ given you know the value of $Y$. Are you now making not just $p_X$ random but also $p$? Then what's it's distribution? – Glen_b -Reinstate Monica Nov 16 '13 at 16:55
• Alan, my earlier version omitted an important term; my apologies. Please take a look at the corrected answer. (That omission may have been part of the problem!) – Glen_b -Reinstate Monica Nov 16 '13 at 17:52
• p is binomial, too. – Alan Nov 16 '13 at 21:59
• Can you change your question to reflect the distribution for $p$ as well, because as it's written up there presently, $p$ is not random; my answer only answers the question that's actually there. Nevertheless, recursive application of the same law, properly applied, yields the answer for that case as well. – Glen_b -Reinstate Monica Nov 16 '13 at 23:26