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):
x.append(r[i])
if x:
q.append(random.sample(x,1)[0])
z.extend(x)
if q:
print(np.mean(q)) # Why are these different?
print(np.mean(z))
else:
print(0)