def point_1(): model_1 = load_stan_model('04-assignment/model_1.stan') model_2 = load_stan_model('04-assignment/model_2.stan') model_3 = load_stan_model('04-assignment/model_3.stan') y = [1] * 17 + [0] * 3 data = {'N': len(y), 'y': y} fit_1, summary_1, _ = fit_stan_model(model_1, data, iter=iterations, seed=random_seed) fit_2, summary_2, _ = fit_stan_model(model_2, data, iter=iterations, seed=random_seed) fit_3, summary_3, _ = fit_stan_model(model_3, data, iter=iterations, seed=random_seed) recreate_figure_64([fit_1, fit_2, fit_3], y) plt.tight_layout() sns.despine() plt.savefig('04-assignment/figure-64.png')
def main(): with open('06-assignment/data.json') as json_file: data = json.load(json_file) data_df = pd.read_csv('06-assignment/data.csv') #plot_data(data_df) model = load_stan_model('06-assignment/hierarchical_model_student.stan') fit, fit_summary, fit_model = fit_stan_model(model, data, iter=10000, warmup=1000) fit_summary.to_csv('06-assignment/student/fit_summary.csv') point_1(fit, fit_summary) point_2(fit, fit_summary) point_3() point_4(fit, fit_summary)
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
import pandas as pd import matplotlib.pyplot as plt import seaborn as sns sns.set_style('whitegrid') sns.set_context('paper') from utils.utils import load_stan_model, fit_stan_model, \ plot_trace, plot_posterior, \ compare_distributions, roc_curve from utils.hdi import hdi_from_book as hdi from pprint import pprint import json if __name__ == '__main__': with open('07-assignment/data.json') as json_file: data = json.load(json_file) data_df = pd.read_csv('07-assignment/data.csv') model_directory = Path('07-assignment/normal_model/') model = load_stan_model(model_directory / 'normal_model.stan') fit, fit_summary, fit_model = fit_stan_model(model, data, iter=35000, warmup=5000) fit_summary.to_csv(model_directory / 'fit_summary.csv', float_format='%.5f') pickle.dump(fit.extract(), open(model_directory / 'fit.pkl', 'wb'))
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()