Expected number of trials until $k$ failures or number of trials reach $n$

binomial distributioncombinatoricsprobability

I am trying to find the expected number of trails until the $k$-th failure
or until the number of trials hit $n$, whichever comes first. The success probability of the trial is $p$.

My calculation gave me the below result.
$$
\sum_{i=0}^{k-1} i{{n}\choose{i}} p^{n-i}(1-p)^{i} + \sum_{i=k}^n i { k-1+i \choose i} p^{i}(1-p)^k
$$

But it seems the result is
$$
\sum_{i=0}^{k-1} i{{n}\choose{i}} p^{n-i}(1-p)^{i} + \sum_{i=k}^n i { i-1 \choose k-1} p^{i-k}(1-p)^k
$$

I don't know what is wrong with my result and how I can get the correct result.

Best Answer

Let $T$ denote the number of trials until the $k$-th failure. The distribution of $T$ is given by $$ \mathsf{P}(T=i)=\binom{i-1}{k-1}(1-p)^kp^{i-k},\quad i\ge k. $$ Then (assuming that $n\ge k$) the expectation of $T\wedge n$ is $$ \mathsf{E}[T\wedge n]=\sum_{i=k}^\infty (i\wedge n)\cdot \mathsf{P}(T=i)=\sum_{i=k}^n i\cdot \mathsf{P}(T=i)+n\cdot \mathsf{P}(T>n), $$ and $$ \mathsf{P}(T>n)=\sum_{i=0}^{k-1}\binom{n}{i}(1-p)^ip^{n-i}, $$ i.e., it is the probability that the number of failures in the first $n$ trials is less than $k$. Alternatively, $$ \mathsf{P}(T>n)=1-\sum_{i=k}^{n}\binom{i-1}{k-1}(1-p)^kp^{i-k}, $$