<jupyter_start><jupyter_text>Exercise 2.24We roll a standard fair die over and over. What is the expected number of rolls until the first pair of consecutive sixes appear?**Solution:** Let $X$ be number of throws until we see two consecutive sixes and let $Y$ be the outcome of the first throw. First, we conclude that$$\begin{align*}E[X | Y=6] &= \sum_{i=2}^\infty i P(X=i | Y=6) = 2 \frac{1}{6} + \sum_{i=3}^\infty i P(X=i | Y=6)= \frac{2}{6} + \frac{5}{6}(2 + E[X]),\\E[X | Y\neq 6] &= 1 + E[X].\end{align*}$$Then,$$E[X] = \sum_{y=1}^6 P(Y=y) E[X|Y=y] = \frac 5 6 (1 + E[X]) + \frac 1 6 \left(\frac{2}{6} + \frac{5}{6}(2 + E[X])\right)= \frac{35}{36} E[X] + \frac{42}{36},$$and thus$$E[X] = 42.$$We verify this result by simple sampling.<jupyter_code>num_samples = 10**7

samples = np.random.randint(low=1, high=7, size=num_samples)
consecutive_sixes = np.array([6,6])

i = 0
lengths = []
while i < len(samples) - 1:
    if np.array_equal(samples[i:i+2], consecutive_sixes):
        lengths.append(i)
        i += 1
    i += 1
    
np.diff(lengths).mean()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20680147058823528, 'fraction_numerical': 0.04779411764705882, 'mean_word_length': 4.136792452830188, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '534972', 'license': 'MIT', 'n_tokens_mistral': 466, 'n_tokens_neox': 437, 'n_words': 117, 'path': 'chapter02/exercise24.ipynb', 'repo_name': 'soerenberg/probability-and-computing-exercises', 'size': '1088'}