def plot_trajectory_result_necessary_data(fname, accs_at_least=[0.5]): results = result_dict_from_file(fname) fun = lambda x: np.mean(x[:, 1]) Ds, Ns, avg_accept_est = gen_sparse_2d_array_from_dict(results, fun) plt.figure() for acc_at_least in accs_at_least: N_at_least = np.zeros(len(Ds)) for i, D in enumerate(Ds): w = np.where(avg_accept_est[i, :] > acc_at_least)[0] if len(w) > 0: N_at_least[i] = np.min(Ns[w]) logger.info("%.2f acc. for D=%d at N=%d" % (acc_at_least, D, N_at_least[i])) else: logger.info("Did not reach %.2f acc. for D=%d" % (acc_at_least, D)) plt.plot(Ds, N_at_least) plt.yscale('log') # plt.xscale('log') plt.legend(["%.2f acc." % acc_at_least for acc_at_least in accs_at_least], loc="lower right") plt.grid(True) fname_base = fname.split(".")[-2] plt.savefig(fname_base + "_data_needs_kmc.eps", axis_inches='tight')
def plot_trajectory_result_heatmap(fname): results = result_dict_from_file(fname) # acc_mean, acc_est_mean, vol, vol_est, steps_taken fun = lambda x: np.mean(x[:, 1]) Ds, Ns, avg_accept_est = gen_sparse_2d_array_from_dict(results, fun) plt.figure() plot_acceptance_heatmap(Ns, Ds, avg_accept_est) plt.xscale('log') fname_base = fname.split(".")[-2] plt.savefig(fname_base + "_kmc.eps", axis_inches='tight')
def plot_trajectory_result_mean_fixed_N(fname, N): results = result_dict_from_file(fname) # acc_mean, acc_est_mean, vol, vol_est, steps_taken fun = lambda x: np.mean(x[:, 1]) Ds, Ns, avg_accept_est_mean = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.mean(x[:, 0]) _, _, avg_accept_mean = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, 1], 25) _, _, avg_accept_est_lower_25 = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, 1], 75) _, _, avg_accept_est_upper_25 = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, 1], 5) _, _, avg_accept_est_lower_5 = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, 1], 95) _, _, avg_accept_est_upper_95 = gen_sparse_2d_array_from_dict(results, fun) N_ind = np.where(Ns == N)[0][0] plt.figure() plt.plot(Ds, avg_accept_mean[:, N_ind], 'r') plt.plot(Ds, avg_accept_est_mean[:, N_ind], 'b') plt.plot(Ds, avg_accept_est_lower_25[:, N_ind], 'b-.') plt.plot(Ds, avg_accept_est_lower_5[:, N_ind], color="grey") plt.plot(Ds, avg_accept_est_upper_95[:, N_ind], color="grey") plt.fill_between(Ds, avg_accept_est_lower_5[:, N_ind], avg_accept_est_upper_95[:, N_ind], color="grey", alpha=.5) plt.plot(Ds, avg_accept_est_upper_25[:, N_ind], 'b-.') plt.plot(Ds, avg_accept_mean[:, N_ind], 'r') plt.xscale("log") plt.grid(True) plt.xlim([Ds.min(), Ds.max()]) plt.xlabel(r"$d$") plt.title(r"n=%d" % N) ylim = plt.ylim() plt.ylim([ylim[0], 1.01]) plt.legend(["HMC", "KMC median", "KMC 25\%-75\%", "KMC 5\%-95\%"], loc="lower left") fname_base = fname.split(".")[-2] plt.savefig(fname_base + "_N=%d.eps" % N, axis_inches='tight')
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]) ax_acor.legend((line1, line2, line3, line4), ["KMC", "RW", "HABC naive", "HABC friction"]) ax_acor.grid(True) plt.sca(ax_acor) plt.savefig("abc_target_autocorr_with_friction.eps", bbox_inches="tight") # KDE for alg_idx, fnames in enumerate([ fnames_kmc, fnames_rw, fnames_habc, fnames_habc_friction, ]): if len(fnames) <= 0: continue all_samples = np.vstack(marginal_samples[alg_idx]) sns.kdeplot(all_samples[:, 0], shade=True,
# compute acceptance probabilities log_acc = compute_log_accept_pr(q0, p0, Qs, Ps, logq, logp) log_acc_est = compute_log_accept_pr(q0, p0, Qs_est, Ps_est, logq, logp) # normalise Hamiltonians Hs -= Hs.mean() Hs_est -= Hs_est.mean() plt.figure() plot_array(Xs_q, Ys_q, np.exp(G)) plot_2d_trajectory(Qs) plt.title("HMC") plt.gca().xaxis.set_visible(False) plt.gca().yaxis.set_visible(False) plt.savefig(fname_base + "_hmc.eps", axis_inches="tight") plt.figure() plot_array(Xs_q, Ys_q, np.exp(G_est)) plt.plot(Z[:, 0], Z[:, 1], 'bx') plot_2d_trajectory(Qs_est) plt.title("KMC") plt.gca().xaxis.set_visible(False) plt.gca().yaxis.set_visible(False) plt.savefig(fname_base + "_kmc.eps", axis_inches="tight") plt.figure() plt.title("Momentum") plot_array(Xs_p, Ys_p, np.exp(M)) plot_2d_trajectory(Ps) plt.gca().xaxis.set_visible(False)
# compute acceptance probabilities log_acc = compute_log_accept_pr(q0, p0, Qs, Ps, logq, logp) log_acc_est = compute_log_accept_pr(q0, p0, Qs_est, Ps_est, logq, logp) # normalise Hamiltonians Hs -= Hs.mean() Hs_est -= Hs_est.mean() plt.figure() plot_array(Xs_q, Ys_q, np.exp(G)) plot_2d_trajectory(Qs) plt.title("HMC") plt.gca().xaxis.set_visible(False) plt.gca().yaxis.set_visible(False) plt.tight_layout() plt.savefig(fname_base + "_hmc.eps") plt.figure() plot_array(Xs_q, Ys_q, np.exp(G_est)) plt.plot(Z[:, 0], Z[:, 1], 'bx') plot_2d_trajectory(Qs_est) plt.title("KMC") plt.gca().xaxis.set_visible(False) plt.gca().yaxis.set_visible(False) plt.tight_layout() plt.savefig(fname_base + "_kmc.eps") plt.figure() plot_array(Xs_q, Ys_q, G_norm) plt.quiver(X, Y, U, V, color='m') plot_2d_trajectory(Qs)
for j, N in enumerate(Ns): logger.info( "MMD of %d benchmark samples against %d MCMC samples" % (len(benchmark_samples), N)) samples_so_far = result.samples[warmup:(warmup + N)] MMDs[i, j] = k.estimateMMD(benchmark_samples, samples_so_far) med = np.median(MMDs, 0) lower = np.percentile(MMDs, 20, 0) upper = np.percentile(MMDs, 80, 0) plt.plot(Ns, med, "-", color=colors[alg_idx]) plt.plot(Ns, lower, '--', color=colors[alg_idx]) plt.plot(Ns, upper, '--', color=colors[alg_idx]) # err = np.array([np.abs(med-lower),np.abs(med-upper)]) # plt.plot(Ns, med, color=colors[alg_idx]) # plt.errorbar(Ns, med, err, color=colors[alg_idx]) plt.yscale("log") 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]) plt.legend((line1, line2, line3), ["KMC", "KAMH", "RW"]) plt.ylabel(r"MMD from ground truth") plt.xlabel("Iterations") plt.grid(True) plt.savefig("gp_target_results.eps", bbox_inches='tight') plt.show()