# step = pm.NUTS() # Instantiate NUTS sampler # trace = pm.sample(5000, step, start=start, progressbar=False) # create an array with the posterior sample theta_sample = trace['theta'] print theta_sample plt.subplot(1, 2, 1) plt.plot(theta_sample[:500], np.arange(500), marker='o') plt.xlim(0, 1) plt.xlabel(r'$\theta$') plt.ylabel('Position in Chain') plt.subplot(1, 2, 2) mcmc_info = plot_post(theta_sample, xlab=r'$\theta', show_mode=False) # Posterior prediction: # For each step in the chain, use posterior theta to flip a coin: y_pred = np.zeros(len(theta_sample)) for i, p_head in enumerate(theta_sample): y_pred[i] = np.random.choice([0, 1], p=[1 - p_head, p_head]) # Jitter the 0,1 y values for plotting purposes: y_pred_jittered = y_pred + np.random.uniform(-.05, .05, size=len(theta_sample)) # Now plot the jittered values: plt.figure() plt.plot(theta_sample[:500], y_pred_jittered[:500], 'ro') plt.xlim(-.1, 1.1) plt.ylim(-.1, 1.1)
# Evidence for model, p(D). # Compute a,b parameters for beta distribution that has the same mean # and stdev as the sample from the posterior. This is a useful choice # when the likelihood function is Bernoulli. a = mean_traj * ((mean_traj*(1 - mean_traj)/std_traj**2) - 1) b = (1 - mean_traj) * ((mean_traj*(1 - mean_traj)/std_traj**2) - 1) # For every theta value in the posterior sample, compute # dbeta(theta,a,b) / likelihood(theta)*prior(theta) # This computation assumes that likelihood and prior are proper densities, # i.e., not just relative probabilities. This computation also assumes that # the likelihood and prior functions were defined to accept a vector argument, # not just a single-component scalar argument. wtd_evid = beta.pdf(accepted_traj, a, b) / (likelihood(accepted_traj, my_data) * prior(accepted_traj)) p_data = 1 / np.mean(wtd_evid) # Display p(D) in the graph plt.plot(0, label='p(D) = %.3e' % p_data, alpha=0) # Display the posterior. ROPE = np.array([0.76, 0.8]) mcmc_info = plot_post(accepted_traj, xlab='theta', show_mode=False, comp_val=0.9, ROPE=ROPE) # Uncomment next line if you want to save the graph. plt.savefig('BernMetropolisTemplate.png') plt.show()