<jupyter_start><jupyter_text>Chapter 3 - Distributions  Distributions  Probability Mass Functions<jupyter_code>from empiricaldist import Pmf
coin = Pmf()
coin['head'] = 1/2
coin['tails'] = 1/2
coin
die = Pmf.from_seq([1,2,3,4,5,6])
die
letters = Pmf.from_seq(list('Mississippi'))
letters
letters['s']
try:
    letters['t']
except KeyError as e:
    print(type(e))
letters('s')
letters('t')
die([1,4,7])<jupyter_output><empty_output><jupyter_text>The Cookie Problem Revisited<jupyter_code>prior = Pmf.from_seq(['Bowl 1', 'Bowl 2'])
prior
likelihood_vanilla = [0.75, 0.5]
posterior = prior * likelihood_vanilla
posterior
posterior.normalize()
posterior
posterior('Bowl 1')
posterior = posterior * likelihood_vanilla
posterior.normalize()
posterior
Likelihood_chocolate = [0.25, 0.5]
posterior = posterior * Likelihood_chocolate
posterior.normalize()
posterior<jupyter_output><empty_output><jupyter_text>101 Bowls<jupyter_code>import numpy as np
hypos = np.arange(101)
prior = Pmf(1, hypos)
prior.normalize()
prior.head()
likelihood_vanilla = hypos/100
likelihood_vanilla[:5]
posterior1 = prior * likelihood_vanilla
posterior1.normalize()
posterior1.head()
from utils import decorate
def decorate_bowls(title):
    decorate(xlabel='Bowl #',
            ylabel='PMF',
            title=title)
prior.plot(label='prior', color='C5')
posterior1.plot(label='posterior', color='C4')
decorate_bowls('Posterior after one vanilla cookie')
posterior2 = posterior1 * likelihood_vanilla 
posterior2.normalize()
posterior2.plot(label='posterior', color='C4')
decorate_bowls('Posterior after two vanilla cookies')
likelihood_chocolate = 1 - hypos/100
posterior3 = posterior2 * likelihood_chocolate
posterior3.normalize()
posterior3.plot(label='posterior', color='C4')
decorate_bowls('Posterior after 2 vanilla, 1 chocolate')
posterior3.idxmax()
posterior3.max_prob()<jupyter_output><empty_output><jupyter_text>The Dice Problem<jupyter_code>hypos = [6,8,12]
prior = Pmf(1/3, hypos)
prior
prior.qs
prior.ps
likelihood1 = 1/6, 1/8, 1/12
posterior = prior * likelihood1
posterior.normalize()
posterior
likelihood2 = 0, 1/8, 1/12
posterior *= likelihood2
posterior.normalize()
posterior<jupyter_output><empty_output><jupyter_text>Updating Dice<jupyter_code>def update_dice(pmf, data):
    """Update pmf based on new data."""
    hypos = pmf.qs
    likelihood = 1/hypos
    impossible = (data > hypos)
    likelihood[impossible] = 0
    pmf *= likelihood
    pmf.normalize()
pmf = prior.copy()
pmf
update_dice(pmf, 1)
update_dice(pmf, 7)
pmf<jupyter_output><empty_output><jupyter_text>Exercises  A.<jupyter_code>pmfa = prior.copy()
pmfa
update_dice(pmfa, 1)
update_dice(pmfa, 3)
update_dice(pmfa, 5)
update_dice(pmfa, 7)
pmfa

# Also
pmfa1 = prior.copy()
for data in [1,3,5,7]:
    update_dice(pmfa1, data)
    
pmfa1<jupyter_output><empty_output><jupyter_text>B.<jupyter_code>pmfb = Pmf([1/15, 2/15, 3/15, 4/15, 5/15],[4,6,8,12,20])
update_dice(pmfb, 7)
pmfb
# Also

# Solution

# Notice that I don't bother to normalize the prior.
# The `Pmf` gets normalized during the update, so we
# don't have to normalize it before.

ps = [1,2,3,4,5]
qs = [4,6,8,12,20]
pmf = Pmf(ps, qs)
update_dice(pmf, 7)
pmf<jupyter_output><empty_output><jupyter_text>C.<jupyter_code>hypos = ['BlackWhite','RedGreenBlue']
prior = Pmf(1/2, hypos)
likelihood = 1/2, 1/3
posterior = prior * likelihood
posterior.normalize()
posterior<jupyter_output><empty_output><jupyter_text>D.<jupyter_code>hypos = ['identical','fraternal']
prior = Pmf([1/3, 2/3], hypos)
likelihood = 1, 0.5
posterior = prior * likelihood
posterior.normalize()
posterior<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1383595691797846, 'fraction_numerical': 0.04114885390776029, 'mean_word_length': 6.4526748971193415, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 38, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '544643', 'license': 'MIT', 'n_tokens_mistral': 1542, 'n_tokens_neox': 1410, 'n_words': 277, 'path': 'book_think-bayes-2ed/03_tb2_ch3.ipynb', 'repo_name': 'pydatawrangler/biwp', 'size': '3621'}