8

Given two integers $N \geq M \gt 0$ and $0<p<1$. Suppose I keep tossing a coin with a head probability $p$ until I get $M$ heads in the most recent $N$ tosses. What is the expected number of tosses?

The game is stopped immediately if $M$ heads are reached before $N$ tosses are attempted.

The question is not from my class or exercise; It just appears in various occasions in my everyday life that pique my curiosity. An example is the "mastery" in Minesweeper online, which is defined to be the number of wins out of consecutive 100 games. In the case of a global quest, players are asked to reach certain mastery from scratch, e.g., to win 70 out of 100 consecutive intermediate games. A skilled player with a winning probability of 0.8 is likely to finish the quest in less than 100 games, while a newbie with a winning probability of 0.6 is likely to make hundreds or thousands of attempts to complete the quest. Some players would like to estimate the expected number of games they need to play, hence it comes the above question.

Intuitively, when $M\ll pN$, the expected number of games should be very close to $M/p$. As $M$ approaches or exceeds $pN$, the expected number of games grows very quickly. So far I haven't find a way to solve the question for a general $M$. But the question does reduce to some well-known problems for some specific $M$.

Let $E(M, N)$ denote the expectation:

  1. first head $$ E(1,N) = \frac{1}{p}$$

  2. first N-consecutive heads $$E(N, N) = \frac{p^{-N}-1}{1-p}$$

  3. $M=2$ seems to be another case where a relation can be built without too much effort. Let $X_{M,N}$ denote the number of tosses to stop the game and $Y$ denote the number of tosses to reach the first head, then

    $$E(2,N) = \sum_{y} E(X_{2,N}|Y=y) P(Y=y)$$

    The conditional probability can be calculated by considering the next $N-1$ tosses. Let $q=1-p$,

    \begin{align} E(X_{2,N}|Y=y) &= p\sum_{k=0}^{N-2} q^k (y+k+1) + q^{N-1} [y+N-1+E(2,N)] \\ & = y + \frac{1-q^{N-1}}{p} + q^{N-1}E(2,N) \end{align}

    which yields $$ E(2,N) = \frac{2-q^{N-1}}{p(1-q^{N-1})} $$

    But I don't think the above approach for $M=2$ can be generalized to $M\geq 3$.

  4. $M=N-1$ is another case where we only need to consider a state with $N$ possibilities, instead of $2^N$. Let $Z_{r}^{N}$ denote the rest number of tosses to finish the game of $M=N-1$ with a most recent history of tail-($r$ consecutive heads)-tail:

    $$ T\underbrace{H...H}_{r}T | (Z_{r}^N ~ \text{tosses to finish}) $$

    We have

    $$ E(Z_{r}^{N}) = p^{N-r-1}(N-r-1) + q\sum_{k=0}^{N-r-2}p^k\left[k+1+E(Z_{k}^{N})\right] $$

    Note that $E(Z_{0}^{N})=E(N-1, N)$ is the quantity we are looking for. Unfortunately I haven't found an explicit expression, though it's just a system of $N-1$ linear equations:

    $$ (I - qA) \pmb{E} = \pmb{b} $$

    where, for $i,j=0,\ldots,N-2$,

    $$ A_{ij} = \left\{\begin{matrix} p^j & i+j\leq N-2 \\ 0 & i+j\gt N-2 \end{matrix} \right. $$

    and $$b_i = \frac{1-p^{N-1-i}}{q} $$

    With the help of sympy I got the following results for $p=2/3$: \begin{align} E(2,3) &= 51/16 & \approx 3.19 \\ E(3,4) &= 1551/296 & \approx 5.24\\ E(4,5) &= 156615/19936 & \approx 7.86\\ E(5,6) &= 38234649/3388448 & \approx 11.28 \\ E(6,7) &= 31121052081/1963106560 & \approx 15.85 \end{align}

  5. As pointed out by @VarunVejalla, a small reduction from $2^N$ can be achieved by considering where the heads are in the sequence of the last $N$ flips. This can be done by generalizing the method for $M=N-1$. Let $Z_{r_1 \ldots r_{N-M}}^{N}$ denote the rest number of tosses to stop the game with a most recent history of $r_i$ consecutive heads separated apart by a tail each, i.e.,

    $$ T\underbrace{H...H}_{r_1}T\underbrace{H...H}_{r_2}T\ldots T \underbrace{H...H}_{r_{N-M}}T | (Z_{r_1\ldots r_{N-M}}^{N} ~ \text{tosses to finish}) $$ where $0\leq r_i\leq M-1$ and $\sum_{i=1}^{N-M}r_i\leq M-1$. We have

    $$E(Z_{r_1\ldots r_{N-M}}^{N}) - q\sum_{k=0}^{M-1-\sum r_i} p^k E(Z_{r_2\ldots r_{N-M}k}^{N}) = \frac{1-p^{M-\sum r_i}}{q} $$

    The above system of linear equations works for any $M<N$, but its size equals the number of non-negative integer solutions to $\sum_{i=1}^{N-M}r_i\leq M-1$, which roughly scales as $O(N^{\textrm{min}(M, N-M)})$. Does anyone have any thoughts on its closed-form solution (if it exists)?

jinzx10
  • 109
  • There's almost definitely a more elegant way to do this, but for small $N$, you could set up a system of $2^N$ linear equations and solve it. – Varun Vejalla Dec 28 '23 at 09:10
  • 1
    For $p={\Large{\frac{2}{3}}}$, here's some data . . . $$ e(1,1)=\frac{3}{2}\ e(1,2)=\frac{3}{2}\ e(2,2)=\frac{15}{4}\ e(1,3)=\frac{3}{2}\ e(2,3)=\frac{51}{16}\ e(3,3)=\frac{57}{8}\ e(1,4)=\frac{3}{2}\ e(2,4)=\frac{159}{52}\ e(3,4)=\frac{1551}{296}\ e(4,4)=\frac{195}{16} $$ – quasi Dec 28 '23 at 11:47
  • 1
    @quasi Thanks for the data! I've updated the post with e(1, N), e(2, N) and e(N, N) and they do agree. But I haven't figured out a way to get e(3, N)... Could you give some clue on how you get e(3,4)? – jinzx10 Dec 29 '23 at 17:34
  • 3
    OK, $M=1, 2, N-1 $ and $ N$ have been solved. But I feel the rest is fundamentally more complicated than these 4 specific cases. – jinzx10 Dec 31 '23 at 15:31
  • A small reduction (adding on to my comment) from the system of $2^N$ linear equations. By just considering where the heads are in the sequence of the last $N$ flips, you can reduce it to a system of $\sum_{h=0}^{M-1}\binom{N}{h}=O(N^{M-1})$ linear equations. It could be that some sort of pattern shows up in the corresponding matrices. – Varun Vejalla Jan 01 '24 at 09:22
  • I agree with @VarunVejalla on that we can try to write iterative equations by dividing the cases w.r.t. the last N results. I think we can start with computing $K(r, s, t)$, which is the number of possible length-$r$ binary array where we cannot find any length-$s$ subarray containing at least $t$ ones. – Vezen BU Jan 03 '24 at 03:35

2 Answers2

4

(Too long for a comment)

This is indeed an interesting problem, but I guess a closed-form for $E(M,N)$, if exists, will not be easily described for a generic $M$. For example, Mathematica gives

$$ E(3, 3) = \frac{-q^2+3 q-3}{(q-1)^3}, $$

$$ E(3, 4) = \frac{-q^5+2 q^4-q^3-q^2+3 q-3}{(q-1)^3 \left(q^3+q+1\right)}, $$

$$ E(3, 5) = \frac{-q^9+3 q^8-2 q^7-2 q^5+4 q^4-q^3-3 q^2+3}{(q-1)^3 \left(q^7-q^6-q^5-2 q^2-2 q-1\right)}, $$

$$ E(3, 6) = \frac{-q^{14}+4 q^{13}-6 q^{12}+3 q^{11}+3 q^{10}-6 q^9+7 q^8-6 q^7+3 q^5-3 q^2+3}{(q-1)^3 \left(q^{12}-2 q^{11}+q^{10}+q^9-q^8-q^7-2 q^6-2 q^3-2 q^2-2 q-1\right)} $$

$$ E(3, 7) = \frac{-q^{20}+5 q^{19}-10 q^{18}+11 q^{17}-9 q^{16}+8 q^{15}-8 q^{14}+11 q^{13}-17 q^{12}+16 q^{11}-10 q^{10}+5 q^9+q^8-5 q^6+2 q^5+3 q^3-3}{(q-1)^3 \left(q^{18}-3 q^{17}+3 q^{16}-2 q^{15}+2 q^{14}-q^{13}-q^{12}-2 q^{11}+3 q^{10}+q^9+2 q^8+3 q^4+3 q^3+3 q^2+2 q+1\right)}. $$

There seems no pattern for $E(3, N)$, as we can glimpse from the lack of the symmetry in the location of zeros (red dots) and poles (blue dots) of $E(3, N)$ as a function of $q$:

zeros and poles of E(3, N)

This makes a striking distinction from the case of $E(2, N)$, where the zeros and poles are located by a circular symmetry:

enter image description here

Sangchul Lee
  • 167,468
  • Looks very cool! Could you explain a bit more what these dots exactly are? (I see! They are the zeros and poles. Sorry for missing the point.) – Vezen BU Jan 03 '24 at 05:57
2

Here's a Maple implementation of $e(m,n)$ via the systems of equations approach.

enter image description here

quasi
  • 58,772