def plot_mcmc_result(result, D1, D2): plt.ioff() samples = result.samples accepted = result.accepted log_pdf = result.log_pdf avg_ess = result.posterior_statistics["avg_ess"] min_ess = result.posterior_statistics["min_ess"] time = result.time_taken_sampling time_set_up = result.time_taken_set_up # compute autocorrelation of the first dimension acorrs = autocorr(samples[:, D1]) # visualise summary plt.figure(figsize=(8, 12)) plt.subplot(521) plt.plot(samples[:, D1]) plt.subplot(522) plt.plot(samples[:, D2]) plt.subplot(523) plt.hist(samples[:, D1]) plt.subplot(524) plt.hist(samples[:, D2]) plt.subplot(525) plt.plot(np.cumsum(accepted) / np.arange(1, len(samples) + 1)) plt.subplot(526) plt.plot(acorrs) plt.subplot(527) plt.plot(samples[:, D1], samples[:, D2]) plt.subplot(528) plt.plot(samples[:, D1], samples[:, D2], '.') plt.subplot(529) plt.plot(log_pdf, '-') # print quantile summary print("Average acceptance probability: %.2f" % np.mean(accepted)) print("Average ESS: %.2f" % avg_ess) print("Minimum ESS: %.2f" % min_ess) print("Average ESS/s: %.2f" % (avg_ess / time)) print("Minimum ESS/s: %.2f" % (min_ess / time)) print("Average ESS/s including set up: %.2f" % (avg_ess / (time + time_set_up))) plt.show()
def plot_diagnosis(agg, D): plt.ioff() samples = agg.result.samples accepted = agg.result.accepted avg_quantile_errors = agg.result.posterior_statistics["avg_quantile_error"] avg_ess = agg.result.posterior_statistics["avg_ess"] norm_of_mean = agg.result.posterior_statistics["norm_of_mean"] time = agg.result.time_taken_sampling time_set_up = agg.result.time_taken_set_up # compute autocorrelation of the dimension with heavier tails acorrs = autocorr(samples[:, 1]) # visualise summary plt.figure(figsize=(8, 12)) plt.subplot(421) plt.plot(samples[:, 0]) plt.subplot(422) plt.plot(samples[:, 1]) plt.subplot(423) plt.hist(samples[:, 0]) plt.subplot(424) plt.hist(samples[:, 1]) plt.subplot(425) plt.plot(np.cumsum(accepted) / np.arange(1, len(samples) + 1)) plt.subplot(426) plt.plot(acorrs) plt.subplot(427) plt.plot(samples[:, 0], samples[:, 1]) plt.subplot(428) plt.plot(samples[:, 0], samples[:, 1], '.') # print quantile summary print("Average quantile errors: %.2f" % avg_quantile_errors) print("Average acceptance probability: %.2f" % np.mean(accepted)) print("Average ESS: %.2f" % avg_ess) print("Average ESS/s: %.2f" % (avg_ess / time)) print("Average ESS/s including set up: %.2f" % (avg_ess / (time + time_set_up))) print("Average norm of mean: %.2f" % norm_of_mean) plt.show()
# accept-reject r = np.random.rand() accepted[i] = r < acc_prob[i] accepted_est[i] = r < acc_prob_est[i] if accepted[i]: q_current = proposals[i] if accepted_est[i]: q_current_est = proposals_est[i] # update state samples[i] = q_current samples_est[i] = q_current_est # compute autocorrelation of the dimension with heavier tails acorrs = autocorr(samples[:, 1]) acorrs_est = autocorr(samples_est[:, 1]) # visualise summary plt.figure(figsize=(8, 12)) plt.subplot(421) plt.plot(samples[:, 0]) plt.subplot(422) plt.plot(samples[:, 1]) plt.subplot(423) plt.hist(samples[:, 0]) plt.subplot(424) plt.hist(samples[:, 1]) plt.subplot(425) plt.plot(np.cumsum(accepted) / np.arange(1, num_iterations + 1)) plt.subplot(426)
logger.info("Average acceptance probability: %.2f" % np.mean(accepted)) logger.info("Average ESS: %.2f" % avg_ess) logger.info("Minimum ESS: %.2f" % min_ess) total_time_s = time + time_set_up total_time_m = total_time_s / 60 logger.info("Total time: %.2f sec, %.2f min" % (total_time_s, total_time_m)) logger.info("Average ESS/s: %.2f" % (avg_ess / total_time_s)) logger.info("Minimum ESS/s: %.2f" % (min_ess / total_time_s)) # store marginal samples marginal_samples[alg_idx].append(samples) # compute autocorrelation of the first dimension acorrs[i, :] = autocorr(samples[:, 0])[:len_acorr] med = np.median(acorrs, 0) lower = np.percentile(acorrs, 20, 0) upper = np.percentile(acorrs, 80, 0) ax_acor.plot(med, "-", color=colors[alg_idx]) ax_acor.plot(lower, '--', color=colors[alg_idx]) ax_acor.plot(upper, '--', color=colors[alg_idx]) ax_acor.set_xlabel("Lag") ax_acor.set_ylabel("Autocorrelation") line1 = Line2D([0, 0], [0, 0], color=colors[0]) line2 = Line2D([0, 0], [0, 0], color=colors[1]) line3 = Line2D([0, 0], [0, 0], color=colors[2]) line4 = Line2D([0, 0], [0, 0], color=colors[3])