<jupyter_start><jupyter_text>Coin flip problemThere are 10 coins in a bag. 9 are fair, and 1 is a double-headed coin. If you randomly choose a coin from the bag, and get 5 heads in a row, what is the probability that the next flip will be heads also?<jupyter_code># Test procedure for each trial:
#   * Choose a coin (10% chance of unfair, 90% chance of fair)
#   * Flip that coin 5 times; if any flip is not heads, reject the sample and go to next trial
#   * If all 5 flips are heads, count this sample, and flip the coin a last time, if heads, count as success

import random

p_f = 0.9
p_h_f = 0.5
trials = 1000000

def fair_coin():
    return random.random() <= 0.5

def unfair_coin():
    return True

def get_coin():
    if random.random() <= p_f:
        return fair_coin
    else:
        return unfair_coin

got_5_heads = 0
got_6th_flip = 0
for i in range(trials):
    coin_got_heads = get_coin()

    reject_sample = False

    for flip in range(5):
        if not coin_got_heads():
            # This coin didn't get 5 heads in a row, so reject it
            reject_sample = True
            break

    if reject_sample:
        continue

    got_5_heads += 1

    if coin_got_heads():
        got_6th_flip += 1

prob = got_6th_flip / got_5_heads
print('P(h6|h1, h2, h3, h4, h5) = {}'.format(prob))<jupyter_output>P(h6|h1, h2, h3, h4, h5) = 0.8917532593889862
{'fraction_non_alphanumeric': 0.08454810495626822, 'fraction_numerical': 0.047376093294460644, 'mean_word_length': 2.878531073446328, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 4, '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': '147839', 'license': 'MIT', 'n_tokens_mistral': 513, 'n_tokens_neox': 468, 'n_words': 186, 'path': 'coin_problem_1.ipynb', 'repo_name': 'calebmadrigal/probability-with-python', 'size': '1372'}