<jupyter_start><jupyter_text><jupyter_code>%%capture
pip install arviz
import numpy as np
import pymc3 as pm
import theano.tensor as tt
import arviz as az
import matplotlib.pyplot as plt
import seaborn as sns<jupyter_output><empty_output><jupyter_text>9.1 Paintball<jupyter_code>def StrafingSpeed(alpha, beta, x):
    theta = tt.arctan2(x-alpha, beta)
    speed = beta / tt.cos(theta)**2
    return speed
with pm.Model() as m_9_3:

    obs_beta = pm.Data('obs_beta', [10])

    alpha = pm.Uniform('alpha', lower=0, upper=31, observed=10)
    beta = pm.Uniform('beta', lower=1, upper=51, observed=obs_beta)
    location = pm.Uniform('location', lower=0, upper=31)

    speed = pm.Deterministic('speed', StrafingSpeed(alpha, beta, location))
    like = pm.Potential('like', -np.log(speed)) # Equivalent to 1/speed
traces_m_9_3 = []
for beta in (10, 20, 40):
    with m_9_3:
        pm.set_data({'obs_beta': [beta]})
        traces_m_9_3.append(pm.sample(5000, progressbar=False))
sns.kdeplot(traces_m_9_3[0]['location'], label='beta = 10', color='darkblue')
sns.kdeplot(traces_m_9_3[1]['location'], label='beta = 20')
sns.kdeplot(traces_m_9_3[2]['location'], label='beta = 40', color='lightblue')
plt.xlim([0,30])
plt.xlabel('Distace')
plt.ylabel('Prob');<jupyter_output><empty_output><jupyter_text>9.5 Joint distributionsResults are very different from those of the book. Posterior is much more narrow.<jupyter_code>with pm.Model() as m_9_5:

    alpha = pm.Uniform('alpha', lower=0, upper=31)
    beta = pm.Uniform('beta', lower=1, upper=51)
    location = pm.Uniform('location', lower=0, upper=31, observed=[15, 16, 18, 21])

    speed = pm.Deterministic('speed', StrafingSpeed(alpha, beta, location))
    like = pm.Potential('like', -tt.log(speed))

    trace_m_9_5 = pm.sample(5000)
bins = np.linspace(0, 51, 50)
plt.hist(trace_m_9_5['alpha'], cumulative=True, bins=bins, density=True, histtype='step', lw=2, label='alpha')
plt.hist(trace_m_9_5['beta'],  cumulative=True, bins=bins, density=True, histtype='step', lw=2, color='lightblue', label='beta')
plt.ylabel('Prob')
plt.xlabel('Distance')
plt.legend(loc=4);
fig, ax = plt.subplots(1, 2, figsize=(12,4))
pm.plot_posterior(trace_m_9_5['alpha'], credible_interval=.5, ax=ax[0])
pm.plot_posterior(trace_m_9_5['beta'], credible_interval=.5, ax=ax[1])
sns.kdeplot(trace_m_9_5['alpha'], trace_m_9_5['beta'], shade=True, n_levels=3, shade_lowest=False);
plt.ylabel('beta')
plt.xlabel('alpha');<jupyter_output><empty_output><jupyter_text>9.6 Conditional Distributions<jupyter_code>with pm.Model() as m_9_6:

    obs_beta = pm.Data('obs_beta', 10)

    alpha = pm.Uniform('alpha', lower=0, upper=31)
    beta = pm.Uniform('beta', lower=1, upper=51, observed=obs_beta)
    location = pm.Uniform('location', lower=0, upper=31, observed=[15, 16, 18, 21])

    speed = pm.Deterministic('speed', StrafingSpeed(alpha, beta, location))
    like = pm.Potential('like', -np.log(speed))
traces_m_9_6 = []
for beta in (10, 20, 40):
    with m_9_6:
        pm.set_data({'obs_beta': beta})
        traces_m_9_6.append(pm.sample(5000, progressbar=False))
sns.kdeplot(traces_m_9_6[0]['alpha'], label='beta = 10', color='darkblue')
sns.kdeplot(traces_m_9_6[1]['alpha'], label='beta = 20')
sns.kdeplot(traces_m_9_6[2]['alpha'], label='beta = 40', color='lightblue')
plt.xlim([0,30])
plt.xlabel('Distace')
plt.ylabel('Prob');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17631423508564678, 'fraction_numerical': 0.04991139988186651, 'mean_word_length': 6.026970954356846, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '667775', 'license': 'MIT', 'n_tokens_mistral': 1427, 'n_tokens_neox': 1309, 'n_words': 243, 'path': 'ThinkBayes_Chapter_9.ipynb', 'repo_name': 'ricardoV94/ThinkBayesPymc3', 'size': '3386'}