PaC Exercise 3.6
Posted on February 18, 2021
import numpy as np
Exercise 3.6
For a coin that comes up head independently with probability p on each flip, what is the variance in the number of flips until the kth head appears?
Solution: Let X be the number of flips until the kth head appears and let Yi, i = 1, …, k, be the number of throws until we see the ith head. Then each Yi is geometric distributed with parameter p and all Yis are independent.
Therefore,
$$
\mathrm{Var}(X) = \sum_{i=1}^k \mathrm{Y_i} = k \frac{(1-p)}{p^2},
$$
For example, for k = 11 and p = 0.35 this yields 21.224…. We verify this by simple sampling.
= 5*10**6
num_samples = 4
k= 0.35
p
= np.random.choice(a=[0,1], p=[1-p, p], size=num_samples)
samples
= np.cumsum(samples) % k
ith_heads
= np.array([k-1, 0])
seq
= np.where([np.array_equal(a, seq)
indices for a in np.stack([ith_heads[:-1], ith_heads[1:]], axis=1)]) # where do we see the $k$th head?
0, indices]).var() np.diff(np.c_[
21.133861223328264