def point_2(fit, fit_summary): posterior_tau_task_6 = fit.extract('tau')['tau'] task_6_mean = np.mean(posterior_tau_task_6) task_6_hdi = hdi(posterior_tau_task_6) posterior_tau_task_5 = pickle.load( open('05-assignment/tau_posterior_for_assignment_6.pkl', 'rb'))['tau'] task_5_mean = np.mean(posterior_tau_task_5) task_5_hdi = hdi(posterior_tau_task_5) tau_diff = posterior_tau_task_6 - posterior_tau_task_5 diff_mean = np.mean(tau_diff) diff_hdi = hdi(tau_diff) diff_effect = tau_diff / np.std(tau_diff) print(f'τ difference mean: {diff_mean:.2f}, HDI: {diff_hdi}') print( f'Effect on τ mean: {np.mean(diff_effect):.2f}, HDI: {hdi(diff_effect)}' ) plt.figure() plot_dist_mean_hdi(posterior_tau_task_6, color='b', label='Task 6') plot_dist_mean_hdi(posterior_tau_task_5, color='r', label='Task 5') plt.legend() sns.despine(left=True) plt.xlabel('Posterior τ') plt.ylabel('Probability density') plt.savefig('06-assignment/normal/posterior_tau_comparison.png')
def point_1(fit, fit_summary): adult_posterior_mu = fit.extract('mu[1]')['mu[1]'] child_posterior_correction = fit.extract('mu[2]')['mu[2]'] child_posterior_mu = adult_posterior_mu + child_posterior_correction adult_mean = np.mean(adult_posterior_mu) child_mean = np.mean(child_posterior_mu) diff_mean = np.mean(child_posterior_correction) adult_hdi = hdi(adult_posterior_mu, 0.95) child_hdi = hdi(child_posterior_mu, 0.95) diff_hdi = hdi(child_posterior_correction, 0.95) child_correction_effect_size = child_posterior_correction / np.std(child_posterior_correction) print(f'Adult HDI: {adult_hdi}') print(f'Child HDI: {child_hdi}') print(f'Difference HDI: {diff_hdi}') plt.figure() sns.distplot(adult_posterior_mu, bins=20, norm_hist=True, label='Adult log posterior μ') sns.distplot(child_posterior_mu, bins=20, norm_hist=True, label='Child log posterior μ') plt.axvline(adult_mean, color='b', lw=2, label='Adult mean') plt.axvline(adult_hdi[0], color='b', lw=2, linestyle='--', label='Adult 95% HDI') plt.axvline(adult_hdi[1], color='b', lw=2, linestyle='--') plt.axvline(child_mean, color='r', lw=2, label='Child mean') plt.axvline(child_hdi[0], color='r', lw=2, linestyle='--', label='Child 95% HDI') plt.axvline(child_hdi[1], color='r', lw=2, linestyle='--') plt.legend() sns.despine(left=True) plt.xlabel('Posterior log reaction time') plt.ylabel('Probability density') plt.xlim(5.4, 6.4) plt.savefig('06-assignment/student/posterior_log_reaction_times.png') plt.figure() sns.distplot(child_posterior_correction, bins=20, norm_hist=True, label='Posterior difference') plt.axvline(diff_mean, color='b', lw=2, label='Difference mean') plt.axvline(diff_hdi[0], color='b', lw=2, linestyle='--', label='Difference 95% HDI') plt.axvline(diff_hdi[1], color='b', lw=2, linestyle='--') plt.legend() sns.despine(left=True) plt.xlabel('Difference in posterior log reaction times') plt.ylabel('Probability density') plt.xlim(-0.1, 0.9) plt.savefig('06-assignment/student/posterior_log_reaction_difference.png') plt.figure() plot_dist_mean_hdi(child_correction_effect_size, color='b', label='Effect size') plt.legend() sns.despine(left=True) plt.xlabel('Effect size') plt.ylabel('Probability density') plt.xlim(-0.5, 9) plt.savefig('06-assignment/student/effect_size.png')
def point_2(fit, summary, fit_model): num_posterior_samples = (fit.sim['iter'] - fit.sim['warmup']) * fit.sim['chains'] #random_new_person_index = np.random.randint(num_posterior_samples) num_new_people = 10000 random_person_samples = np.zeros((num_new_people, )) for i in range(num_new_people): new_person_mu = fit.extract('mu')['mu'][np.random.randint( num_posterior_samples)] new_person_tau = fit.extract('tau')['tau'][np.random.randint( num_posterior_samples)] new_person_sigma = fit.extract('sigma')['sigma'][np.random.randint( num_posterior_samples)] new_person_theta = np.random.normal(new_person_mu, new_person_tau) sample_log_reaction_times = np.random.normal(new_person_theta, new_person_sigma, (1, )) #sample_reaction_times = np.exp(sample_log_reaction_times) random_person_samples[i] = np.exp(sample_log_reaction_times) random_person_hdi = hdi(random_person_samples, 0.95) print( f'Expected value of randomly drawn people: {np.mean(random_person_samples):.2f}' ) print(f'HDI of randomly drawn people samples: {random_person_hdi}') plt.figure() sns.distplot(random_person_samples, bins=20, norm_hist=True) plt.plot(random_person_hdi, [0, 0], 'k', linewidth=5) plt.savefig('05-assignment/point_2.png') pickle.dump(np.log(random_person_samples), open('05-assignment/random_person_samples.pkl', 'wb'))
def plot_dist_mean_hdi(data, label='', color='k'): data_hdi = hdi(data) sns.distplot(data, bins=20, norm_hist=True, label=f'{label} posterior') plt.axvline(np.mean(data), color=color, lw=2, label=f'{label} mean') plt.axvline(data_hdi[0], color=color, lw=2, linestyle='--', label=f'{label} 95% HDI') plt.axvline(data_hdi[1], color=color, lw=2, linestyle='--')
def point_1(data): model = load_stan_model('05-assignment/hierarchical_model.stan', True) fit, summary, fit_model = fit_stan_model(model, data, iter=10000, warmup=1000) the_dude_posterior = fit.extract('exp_theta')['exp_theta'][:, 3] mu_posterior = fit.extract('exp_mu')['exp_mu'] random_posteriors = fit.extract('exp_theta')['exp_theta'][:, [10, 1]] sns.distplot(the_dude_posterior, bins=30, norm_hist=True, label='The Dude θ') sns.distplot(mu_posterior, bins=30, norm_hist=True, label='Group θ') sns.distplot(random_posteriors[:, 0], bins=30, norm_hist=True) sns.distplot(random_posteriors[:, 1], bins=30, norm_hist=True) print(f'The Dude\'s mean: {np.mean(the_dude_posterior):.2f}') print(f'The group mean: {np.mean(mu_posterior):.2f}') plt.axvline(np.mean(the_dude_posterior), color='b', lw=2, linestyle='--', label='The Dude mean') plt.axvline(np.mean(mu_posterior), color='r', lw=2, linestyle='--', label='Group mean') the_dude_hdi = hdi(the_dude_posterior, 0.95) mu_hdi = hdi(mu_posterior, 0.95) print(f'The Dude\'s HDI: {the_dude_hdi}') print(f'The group HDI: {mu_hdi}') plt.legend() sns.despine() plt.savefig('05-assignment/point_1.png') pickle.dump(fit.extract('tau'), open('05-assignment/tau_posterior_for_assignment_6.pkl', 'wb')) return fit, summary, fit_model
def point_2(fit, fit_summary): posterior_tau_task_6 = fit.extract('tau')['tau'] task_6_mean = np.mean(posterior_tau_task_6) task_6_hdi = hdi(posterior_tau_task_6) posterior_tau_task_5 = pickle.load(open('05-assignment/tau_posterior_for_assignment_6.pkl', 'rb'))['tau'] task_5_mean = np.mean(posterior_tau_task_5) task_5_hdi = hdi(posterior_tau_task_5) tau_diff = posterior_tau_task_6 - posterior_tau_task_5 diff_mean = np.mean(tau_diff) diff_hdi = hdi(tau_diff) plt.figure() plot_dist_mean_hdi(posterior_tau_task_6, color='b', label='Task 6') plot_dist_mean_hdi(posterior_tau_task_5, color='r', label='Task 5') plt.legend() sns.despine(left=True) plt.xlabel('Posterior τ') plt.ylabel('Probability density') plt.savefig('06-assignment/student/posterior_tau_comparison.png')
def point_2(): model = load_stan_model('04-assignment/model_3.stan') y = [1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1] z = [1, 0, 0, 0, 0, 0, 0, 1, 1, 0] data_y = {'N': len(y), 'y': y} data_z = {'N': len(z), 'y': z} fit_y, summary_y, _ = fit_stan_model(model, data_y, iter=iterations, seed=random_seed) fit_z, summary_z, _ = fit_stan_model(model, data_z, iter=iterations, seed=random_seed) hdi_y = hdi(fit_y['theta']) hdi_z = hdi(fit_z['theta']) print( f'Expected probability for getting heads with data y: {summary_y.loc["theta", "mean"]:.2f}' ) print(f'HDI of y: {hdi_y[0]:.2f} - {hdi_y[1]:.2f}') print(f'Probability of θ > 0.5: {np.mean(fit_y["theta"] > 0.5)}') d_theta = fit_y['theta'] - fit_z['theta'] hdi_d_theta = hdi(d_theta) probability_higher, point_of_even = compare_distributions( fit_y['theta'], fit_z['theta']) plt.figure() sns.distplot(fit_y['theta'], bins=60, hist=True, label='θ_y') sns.distplot(fit_z['theta'], bins=60, hist=True, label='θ_z') plt.plot(hdi_y, [0, 0], 'b', alpha=0.5, linewidth=5, label='y 95% HDI') plt.plot(hdi_z, [0, 0], 'r', alpha=0.5, linewidth=5, label='z 95% HDI') plt.axvline(point_of_even, color='r', linestyle='--', alpha=0.5) plt.xlabel('θ') plt.ylabel('Density') plt.legend() plt.tight_layout() sns.despine() plt.savefig('04-assignment/comparison.png') print( f'Expected probability that y and z are drawn from the same coin: {1 - np.mean(d_theta > 0):.2f}' ) print( f'True positive rate of choosing coin y: {1 - probability_higher:.2f}') print( f'HDI of difference between y and z: {hdi_d_theta[0]:.2f} - {hdi_d_theta[1]:.2f}' ) plt.figure() sns.distplot(d_theta, hist=True) plt.plot(hdi_d_theta, [0, 0], 'k', linewidth=5, label='95% HDI') plt.xlabel('dθ') plt.ylabel('Density') plt.legend() plt.tight_layout() sns.despine() plt.savefig('04-assignment/figure-point-2.png') plt.show()