anlak.com

Yet another biased coin problem

I have been asked a question by a colleague recently, I liked the question because it demonstrates how Bayesian inference works in a nice way. Even though the question sounds simple it requires complex operations. Here is the question:
A biased coin is tossed $N$ times and heads came 4 times. If the probability of heads coming up is $q$, what is the most likely value of $N$?
And here goes my answer. The probability of heads coming at the first 4 tosses and the rest tails is $q^4(1-q)^{N-4}$, there are ${N \choose 4}$ combinations where 4 tosses are head. So the probability of heads coming 4 times in $N$ tosses is: $$ {N \choose 4} q^4(1-q)^{N-4} $$ We are looking for $N$ that maximizes this expression (aka objective function). More formally: $$ \newcommand{\argmax}[1]{\arg\,\max\limits_#1\,} N^* = \argmax{N}{N \choose 4} q^4(1-q)^{N-4} $$ We would normally take the derivative with respect to $N$ and set equal to zero to find min/max. But this is hard to differentiate. So instead we take $\log$ of the expression first then take the derivative. Notice because $\log$ is monotonically increasing function it won't change where the maximum is. Furthermore we can get rid of additive terms that do not include $N$. So we have: $$ \begin{align*} N^* &= \argmax{N} \log{N \choose 4} + 4\log(q) + (N-4)\log(1-q) \\ N^* &= \argmax{N} \log(N) + \log(N-1) + \log(N-2) + \log(N-3) +\\ &\qquad{} (N-4)\log(1-q) \end{align*} $$ We take the derivative of our objective function w.r.t $N$ and set equal to zero: $$ \frac1{N} + \frac1{N-1} + \frac1{N-2} + \frac1{N-3} + \log(1-q) = 0 $$ This looks better. The closed form solution for $N$ is rather ugly, you can check it here. But there are various numerical ways like Newton's method to solve this kind of equation. There are 4 roots of $N$, because we know it needs to be tossed at least 4 times we pick a solution that is greater than 3. Lastly, even though $N$ is discrete we treated it like it is a continuous variable. In the end we will have a fractional value, like 9.4127 for $N$. We need to check both 9 and 10, and select whichever maximizes the objective function. For example, if we solve it for a fair coin, i.e. $q=0.5$, we get $N\approx 7.48449$. We plug both 7 and 8 to our objective function and we see that they are equal. Which means 7 and 8 are both equally likely.

No comments:

Post a Comment