Take a random 12 digit random binary string, each bit equiprobable 0 or 1. Select a bit that is preceded by 3 0s equiprobably at random. The probability that the bit is 1 is ~66%. Why? Why is this probability 50% if we calculate the sample average of all bits preceded by 3 0s instead of the sample average of a randomly selected bit that is preceded by 3 0s?

import numpy as np
import random

ntrials = 10000
n = 12
q = []
z = []

for _ in range(0, ntrials):
    r = np.random.randint(0,2,n)
    x = []
    for i in range(3, n):
        if np.all(r[i-3:i] == 0):
    if x:

if q:
    print(np.mean(q)) # Why are these different?
  • $\begingroup$ What happens if the initial string has no $000$ substrings? The issue here is probably that we are dealing with a conditional probability. $\endgroup$ – Jack D'Aurizio Apr 11 '18 at 23:54
  • $\begingroup$ Since you created a small program to do that, why you don't create all possible 2^12 binary strings, and then count the frequency of $0001$ and $0000$ in each of those strings, instead of doing a Monte Carlo approach. That will give you an hint. $\endgroup$ – Marco Bellocchi Apr 11 '18 at 23:54
  • 1
    $\begingroup$ I think the problem occurs at the boundaries of your binary string(beginning and ending) . The more you increase $n$ the closest both probabilities are to 0.5 $\endgroup$ – Max Ft Apr 12 '18 at 0:01
  • $\begingroup$ Related: math.stackexchange.com/questions/2317508/… and math.stackexchange.com/a/2044100/6460 $\endgroup$ – Henry Apr 12 '18 at 6:52
  • $\begingroup$ Related: the "hot hand" twitter.com/jordancurve/status/983620991871004672 $\endgroup$ – Ethan Bolker Apr 12 '18 at 16:19

It is perhaps easier to enumerate $5$ bit cases

Strings with three $0$s followed by something are

00000, 00001, 00010, 00011, 10000, 10001

where the possible bits following a 000 are respectively

0 & 0, 0 & 1,     1,     1,     0,     1    

So you have a choice in setting up the two methods, affecting the probabilities:

  • take the simple average over each possible bit of $0,0,0,1,1,1,0,1$ to get $0.5$
  • take the average of the averages for each possible string of $0, 0.5,1,1,0,1$ to get about $0.5833$; this corresponds to conditioning on a string containing 000 and then choosing at random a eligible bit from that string

Why is the average of averages higher with $5$ or $12$ bits or other lengths greater than $4$? Because $1$ is more like to appear more often in strings alone or with few alternatives, while $0$ can more often appear multiple times in stings with more than three consecutive $0$s and averaging averages under weights these cases

  • $\begingroup$ Looking at the words of length $5$ we have $8$ "cases of $000$ followed by something". In $4$ of these cases the "something" is $1$. $\endgroup$ – Christian Blatter Apr 13 '18 at 18:19
  • $\begingroup$ @ChristianBlatter - yes, and that is my first bullet point. But if you select one of the $6$ strings with equal probability and then select one of its cases of $000$ with equal probability (my second bullet point) then you get a different answer $\endgroup$ – Henry Apr 13 '18 at 20:33

I didn't look at your program, and what it tried to do. In any case your $60\%$ claim made in the text is simply wrong since it contradicts the assumed independence of the digits. (This dream of yours has been the ruin of thousands of gamblers $\ldots$)

If you are not convinced: The following little program looks at all binary strings of length $12$ and counts how often $111$ is followed by a $0$, resp., by a $1$. The resulting numbers are equal.

enter image description here

  • $\begingroup$ Yes I agree, I did found the same results indeed $\endgroup$ – Marco Bellocchi Apr 12 '18 at 11:16
  • 1
    $\begingroup$ The question actually makes two claims: that selecting a run of 000 has a 50% probability of being followed by a 1, which you have confirmed; and that selecting a random full string with at least one 000 run and then choosing one of the 000 runs and looking at the next bit gives a higher probability of getting a 1 $\endgroup$ – Henry Apr 12 '18 at 22:09
  • $\begingroup$ Fair point Henry, thanks for calarifying. $\endgroup$ – Marco Bellocchi Apr 13 '18 at 16:11

Let's consider the case where you select the value following one 0 (same problem easier for my example). Let us consider a length 3 binary string. The equally likely string with at least one x and their corresponding output are the following:

$101-1\\ 011-1\\ 100 -0\\ 010-1\\ 001-\{0,1\}\\ 000-\{0,0\}$

$z = \{1,1,0,1,0,1,0,0\}$ which gives 0.5

The problem with random.sample(x,1)[0] is that the element $\{0,0\}$ is actually replaced with 0 in x. So that you underestimate the number of 1's. the element $\{1,0\}$ works fine because it is balanced.

This problem is very specific to the number of consecutive zero you look at and the length of the string. In the case where the number of 0s and the length of the string change you could have cases where the unbalanced sets offset each other (especially for n >> number of 0s).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.