Computing and plotting the posterior

Now we will use Python to compute and plot the posterior distribution based on the analytical expression we have just derived. In the following code, you will see there is actually one line that computes the results while the others are there just to get a nice plot:

plt.figure(figsize=(10, 8))n_trials = [0, 1, 2, 3, 4, 8, 16, 32, 50, 150]data = [0, 1, 1, 1, 1, 4, 6, 9, 13, 48]theta_real = 0.35beta_params = [(1, 1), (20, 20), (1, 4)]dist = stats.betax = np.linspace(0, 1, 200)for idx, N in enumerate(n_trials):    if idx == 0:        plt.subplot(4, 3, 2)        plt.xlabel('θ')    else:        plt.subplot(4, 3, idx+3)        plt.xticks([])    y = data[idx]    for (a_prior, b_prior) in beta_params: p_theta_given_y = dist.pdf(x, a_prior + y, b_prior ...

Get Bayesian Analysis with Python - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.