I'm a programmer writing an app that runs an event that has a p chance of success.
The program should run N times or stop after M successes have been achieved (M<=N). Whichever comes first. I want to calculate the expected number of successes.
I found the same question here. But I think the proposed answer could be wrong and I want to see if there's a more accurate one.
Let's take an example -
The chance of success is 50% - p = 0.5
The maximum number of runs is 10 - N = 10
The program stops if 3 successes have been achieved - M = 3
In the proposed answer to the same question, the author wrote the following -
$$E(X) = \sum_{k = 0}^M k \binom{N}{k}p^k(1 - p)^{N - k} + M \sum_{k = M}^{N - 1} \binom{k}{M} p^M (1 - p)^{k - M}$$
Stating that the first summation calculates the case in which the program reaches exactly N events, and the second summation calculates all of the cases in which M successes have been achieved in less than N events. Which when added together are supposed to give the final desired result.
It seems to me that the first summation includes cases in the calculation that shouldn't be included. For example, let's say that X represents a failure and V is a success -
X X X V X V X V X X
Since the first summation includes all cases in which there are 3 successes out of 10, the above example will be included in the calculation. Where in reality that example should not be included since the program would stop after the third success -
X X X V X V X V | Stop
I've tried to think of my own solution and came up with this -
$$E(X) = p\sum_{i = M-1}^{N-1} \binom{i}{M-1}p^{M-1}(1 - p)^{i - (M - 1)}$$
To explain how I got to this formula - I've tried to calculate the expected number of successes minus one when running all the way from a size group of M-1 to N-1 and then multiply it by the chances of getting the Mth success.
So -
Chances of getting 2 successes out of 2 tries
+
Chances of getting 2 successes out of 3 tries
+
Chances of getting 2 successes out of 4 tries
All the way to
Chances of getting 2 successes out of 9 tries
Summing all of those up together and multiply them by p which is the chance of getting the (final) Mth success.
So if we take the numbers from the example it would be -
$$E(X) = 0.5\sum_{i = 2}^{9} \binom{i}{2}0.5^{2}(1 - 0.5)^{i - 2}$$
So the question is -
Is this a more accurate solution or is there something that I might have missed?
I don't believe either answer is correct. In the situation you've described, if $\ s<M\ $, then the you will achieve exactly $\ s\ $ successes if and only if you achieve that number of successes after $\ N\ $ trials. The probability of this is $$ {N\choose s}p^s(1-p)^{N-s}\ . $$ The only other possible outcome is that you will achieve exactly $\ M\ $ successes, which must therefore occur with probability $$ 1-\sum_{s=0}^{M-1}{N\choose s}p^s(1-p)^{N-s}=\sum_{s=M}^N{N\choose s}p^s(1-p)^{N-s}\ . $$ Thus, if $\ X\ $ is the number of successes, then $$ P(X=s)=\cases{{N\choose s}p^s(1-p)^{N-s}&if $\ s<M$\\ \sum_\limits{k=M}^N{N\choose k}p^k(1-p)^{N-k}&if $\ s=M\ $.} $$ Therefore \begin{align} E(X)&=\sum_{s=0}^MsP(X=s)\\ &=\sum_{s=0}^{M-1}{N\choose s}sp^s(1-p)^{N-s}+M\sum_\limits{k=M}^N{N\choose k}p^k(1-p)^{N-k} \end{align}