<jupyter_start><jupyter_text>1. Overview<jupyter_code>n, p, w = 9, 0.5, 6<jupyter_output><empty_output><jupyter_text>Generate binomial with numpy or scipy<jupyter_code>np.random.binomial(1, p, n)
stats.binom.rvs(1, p, size=n)<jupyter_output><empty_output><jupyter_text>Density function of a binomial<jupyter_code>stats.binom.pmf(w, n, p)<jupyter_output><empty_output><jupyter_text>Grid search<jupyter_code>NUM_POINTS = 1001
p_grid = np.linspace(0, 1, NUM_POINTS)
prob_p = np.ones(NUM_POINTS)
prob_data = stats.binom.pmf(w, n, p=p_grid)
posterior = prob_data * prob_p
posterior = posterior / sum(posterior)
posterior.mean()
prob_data
aux = pd.DataFrame(posterior).reset_index().rename({0:"prob"}, axis=1)
aux['p'] = aux.index/(NUM_POINTS-1)
aux = pd.DataFrame(posterior).reset_index().rename({0:"prob"}, axis=1)
aux['p'] = aux.index/(NUM_POINTS-1)

plt.plot(aux['p'], aux['prob'])
plt.xlabel("P")
plt.ylabel("Density")
plt.title("The density of P posterior distribution")
plt.plot(aux['p'], aux['prob'])
plt.xlabel("P")
plt.ylabel("Density")
plt.title("The density of P posterior distribution")<jupyter_output><empty_output><jupyter_text>Excerise 1: 8 water in 15 tosses<jupyter_code>n, p, w = 15, 0.5, 8

NUM_POINTS = 1001
p_grid = np.linspace(0, 1, NUM_POINTS)
prob_p = np.ones(NUM_POINTS)
prob_data = stats.binom.pmf(w, n, p=p_grid)
posterior = prob_data * prob_p
posterior = posterior / sum(posterior)
samples = pd.DataFrame(np.random.choice(p_grid, 5000, p=posterior))\
            .reset_index()\
            .rename({0:'prob'}, axis=1)

print("The posterior mean:", round(np.mean(samples.prob),2))
np.percentile(np.array(samples.prob), 0.5), np.percentile(np.array(samples.prob), 99)
aux = pd.DataFrame(posterior).reset_index().rename({0:"prob"}, axis=1)
aux['p'] = aux.index/(NUM_POINTS-1)

plt.plot(aux['p'], aux['prob'])
plt.xlabel("P")
plt.ylabel("Density")
plt.title("The density of P posterior distribution")<jupyter_output><empty_output><jupyter_text>Excercise 2: different prior<jupyter_code>NUM_POINTS//2
n, p, w = 9, 0.5, 6

NUM_POINTS = 1001
p_grid = np.linspace(0, 1, NUM_POINTS)
prob_p = np.concatenate((np.zeros(NUM_POINTS//2), np.ones(NUM_POINTS - NUM_POINTS//2)))
prob_p = prob_p / sum(prob_p)
prob_data = stats.binom.pmf(w, n, p=p_grid)
posterior = prob_data * prob_p
posterior = posterior / sum(posterior)
aux = pd.DataFrame(posterior).reset_index().rename({0:"prob"}, axis=1)
aux['p'] = aux.index/(NUM_POINTS-1)

plt.plot(aux['p'], aux['prob'])
plt.xlabel("P")
plt.ylabel("Density")
plt.title("The density of P posterior distribution")<jupyter_output><empty_output><jupyter_text>Sample the posterior<jupyter_code>samples = pd.DataFrame(np.random.choice(p_grid, 5000, p=posterior))\
            .reset_index().rename({0:'prob'}, axis=1)

samples.head()
plt.plot(samples.index, samples.prob, linestyle = 'None', marker='o', alpha=0.2)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1780394873571181, 'fraction_numerical': 0.025285763768617943, 'mean_word_length': 6.6, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '126301', 'license': 'Apache-2.0', 'n_tokens_mistral': 1235, 'n_tokens_neox': 1168, 'n_words': 193, 'path': '_00_rethink_stats_week1.ipynb', 'repo_name': 'danhphan/bayesian_inferences', 'size': '2887'}