social() %matplotlib inline import numpy as np from matplotlib import pyplot as plt import seaborn as sns # Some initial styling rc={'axes.labelsize': 16.0, 'font.size': 16, 'legend.fontsize': 14.0, 'axes.titlesize': 18, 'xtick.labelsize': 12.0, 'ytick.labelsize': 12.0} sns.set(context = 'poster', style = 'whitegrid', rc=rc) import scipy.stats as stats # Create some data dist = stats.beta n_trials = [0,1,2,3,4,5,8,15,50,500] data = stats.bernoulli.rvs(0.5, size = n_trials[-1]) x = np.linspace(0,1,100) # Plot the data for k, N in enumerate(n_trials): sx = plt.subplot(len(n_trials) / 2, 2, k+1) plt.xlabel("$p$, probability of heads") \ if k in [0, len(n_trials) - 1] else None heads = data[:N].sum() y = dist.pdf(x, 1 + heads, 1 + N - heads) plt.plot(x, y, label='observed %d tosses, \n %d heads' % (N, heads)) plt.fill_between(x, 0, y, color='blue', alpha=0.4) plt.vlines(0.5, 0, 4, color='k', linestyles='--', lw=1) leg = plt.legend() leg.get_frame().set_alpha(0.4) plt.subplots_adjust(wspace=None, hspace=0.4) plt.suptitle('Bayesian updating of posterior probabilities', y = 1.02) p = np.linspace(0, 1, 50) plt.plot(p, 2 * p / (1 + p)) plt.scatter(0.2, (2*0.2) / 1.2, s=140) plt.xlim(0, 1) plt.ylim(0, 1) plt.xlabel('Prior, $P(A) = p$') plt.ylabel('Posterior, $P(A|X)$, with $P(A) = p$') plt.title('Are there bugs in my code?') color = ["#9b59b6", "#3498db"] prior = [0.2, 0.8] posterior = [1. /3, 2./3] plt.bar([0, 0.7], prior, alpha=0.7, width=0.25, label='prior distribution', color=color[0]) plt.bar([0 + 0.25, 0.7 + 0.25], posterior, alpha=0.7, width=0.25, label='posterior distribution', color=color[1]) plt.xticks([0.2, 0.95], ['Bugs Absent', 'Bugs Present']) plt.ylabel('Probability') plt.legend(loc='upper left') from IPython.core.display import HTML def css_styling(): styles = open("/users/ryankelly/desktop/custom_notebook2.css", "r").read() return HTML(styles) css_styling() def social(): code = """ Tweet Follow @Ryanmdk submit to reddit """ return HTML(code)