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_banana_result_mean_N_D(results, D, stat_idx, normalise_by_time=False, **kwargs): fun = lambda x:None Ds, Ns, _ = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.mean(x[:, 0]) _, _, time_taken_set_up = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.mean(x[:, 1]) _, _, time_taken_sampling = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.mean(x[:, stat_idx]) _, _, avg = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, stat_idx], 25) _, _, lower = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, stat_idx], 75) _, _, upper = gen_sparse_2d_array_from_dict(results, fun) D_ind = Ds == D time_total = time_taken_sampling[:,D_ind]# + time_taken_set_up[:,D_ind] time_total = time_total.ravel() print_table(time_total, "Time total") normaliser = time_total if normalise_by_time else 1.0 avg = avg[:, D_ind].ravel()/normaliser err = np.array([np.abs(avg-lower[:, D_ind].ravel()/normaliser), np.abs(avg-upper[:, D_ind].ravel()/normaliser)]) plt.plot(Ns, avg, kwargs['color']) plt.errorbar(Ns, avg, err, color=kwargs['color']) plt.grid(True) try: plt.title(kwargs['title']) except KeyError: pass try: plt.ylim(kwargs['ylim']) except KeyError: pass try: plt.xlim(kwargs['xlim']) except KeyError: pass try: plt.xlabel(kwargs['xlabel']) except KeyError: pass
def plot_banana_result_mean_D(results, D, stat_idx, normalise_by_time=False, **kwargs): fun = lambda x:None Ds, _ = gen_sparse_1d_array_from_dict(results, fun) fun = lambda x: np.mean(x[:,0]) _, time_taken_set_up = gen_sparse_1d_array_from_dict(results, fun) fun = lambda x: np.mean(x[:,1]) _, time_taken_sampling = gen_sparse_1d_array_from_dict(results, fun) fun = lambda x: np.mean(x[:,stat_idx]) _, avg = gen_sparse_1d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, stat_idx], 25) _, lower = gen_sparse_1d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, stat_idx], 75) _, upper = gen_sparse_1d_array_from_dict(results, fun) D_ind = Ds == D time_total = time_taken_sampling[D_ind] + time_taken_set_up[D_ind] time_total = time_total.ravel() print_table(time_total, "Time total") normaliser = time_total if normalise_by_time else 1.0 avg = avg[D_ind].ravel()/normaliser xlim=plt.xlim() plt.plot(xlim, [avg,avg], color=kwargs['color']) plt.plot(xlim, [lower,lower], '--', color=kwargs['color']) plt.plot(xlim, [upper,upper], '--', color=kwargs['color']) plt.grid(True) try: plt.title(kwargs['title']) except KeyError: pass try: plt.ylim(kwargs['ylim']) except KeyError: pass try: plt.xlabel(kwargs['xlabel']) except KeyError: pass
def plot_fixed_D(results, results_hmc, D): fun = lambda x: None _, _ = gen_sparse_1d_array_from_dict(results_hmc, fun) fun = lambda x: np.mean(x[:, 0]) _, time_taken_set_up_hmc = gen_sparse_1d_array_from_dict(results_hmc, fun) fun = lambda x: np.mean(x[:, 1]) _, time_taken_sampling_hmc = gen_sparse_1d_array_from_dict(results_hmc, fun) fun = lambda x: np.mean(x[:, 2]) _, accept_hmc = gen_sparse_1d_array_from_dict(results_hmc, fun) fun = lambda x: np.mean(x[:, 3]) _, avg_quantile_error_hmc = gen_sparse_1d_array_from_dict(results_hmc, fun) fun = lambda x: np.mean(x[:, 4]) _, avg_ess_hmc = gen_sparse_1d_array_from_dict(results_hmc, fun) fun = lambda x: None Ds, Ns, _ = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.mean(x[:, 0]) _, _, time_taken_set_up = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.mean(x[:, 1]) _, _, time_taken_sampling = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.mean(x[:, 2]) _, _, accept = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, 2], 25) _, _, accept_lower25 = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, 2], 75) _, _, accept_upper75 = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, 2], 5) _, _, accept_lower5 = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, 2], 95) _, _, accept_upper95 = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.mean(x[:, 3]) _, _, avg_quantile_error = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, 3], 25) _, _, avg_quantile_error_lower25 = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, 3], 75) _, _, avg_quantile_error_upper75 = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, 3], 5) _, _, avg_quantile_error_lower5 = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, 3], 95) _, _, avg_quantile_error_upper95 = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.mean(x[:, 4]) _, _, avg_ess = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, 4], 25) _, _, avg_ess_lower25 = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, 4], 75) _, _, avg_ess_upper75 = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, 4], 5) _, _, avg_ess_lower5 = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, 4], 95) _, _, avg_ess_upper95 = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.mean(x[:, 5]) _, _, avg_norm_of_mean = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, 5], 25) _, _, avg_norm_of_mean_lower25 = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, 5], 75) _, _, avg_norm_of_mean_upper75 = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, 5], 5) _, _, avg_norm_of_mean_lower5 = gen_sparse_2d_array_from_dict(results, fun) fun = lambda x: np.percentile(x[:, 5], 95) _, _, avg_norm_of_mean_upper95 = gen_sparse_2d_array_from_dict(results, fun) D_ind = Ds == D time_total = time_taken_sampling[:,D_ind] + time_taken_set_up[:,D_ind] time_total_hmc = time_taken_sampling_hmc[D_ind] + time_taken_set_up_hmc[D_ind] plt.figure() plt.plot([Ns.min(), Ns.max()], [accept_hmc[D_ind], accept_hmc[D_ind]], 'r') plt.plot(Ns, accept[:, D_ind], 'b') plt.plot(Ns, accept_lower25[:, D_ind], 'b-.') plt.plot(Ns, accept_upper75[:, D_ind], 'b-.') plt.fill_between(Ns, accept_lower5[:, D_ind].ravel(), accept_upper95[:, D_ind].ravel(), color="grey", alpha=.5) plt.title("Avg. acc. prob.") plt.ylim([0,1.1]) plt.grid(True) plt.xlabel(r"$n$") plt.figure() plt.plot([Ns.min(), Ns.max()], [avg_quantile_error_hmc[D_ind], avg_quantile_error_hmc[D_ind]], 'r') plt.plot(Ns, avg_quantile_error[:, D_ind]) plt.plot(Ns, avg_quantile_error_lower25[:, D_ind], 'b-.') plt.plot(Ns, avg_quantile_error_upper75[:, D_ind], 'b-.') plt.fill_between(Ns, avg_quantile_error_lower5[:, D_ind].ravel(), avg_quantile_error_upper95[:, D_ind].ravel(), color="grey", alpha=.5) plt.title("Avg. quantile error") plt.grid(True) plt.xlabel(r"$n$") plt.figure() # plt.plot([Ns.min(), Ns.max()], np.array([avg_ess_hmc[D_ind], avg_ess_hmc[D_ind]])/time_total_hmc, 'r') plt.plot(Ns, avg_ess[:, D_ind]/time_total, 'b') plt.plot(Ns, avg_ess_lower25[:, D_ind]/time_total, 'b-.') plt.plot(Ns, avg_ess_upper75[:, D_ind]/time_total, 'b-.') plt.fill_between(Ns, (avg_ess_lower5[:, D_ind]/time_total).ravel(), (avg_ess_upper95[:, D_ind]/time_total).ravel(), color="grey", alpha=.5) plt.title("Avg. ESS/s") plt.grid(True) plt.xlabel(r"$n$") plt.figure() # plt.plot([Ns.min(), Ns.max()], [avg_quantile_error_hmc[D_ind], avg_quantile_error_hmc[D_ind]], 'r') plt.plot(Ns, avg_norm_of_mean[:, D_ind]) plt.plot(Ns, avg_norm_of_mean_lower25[:, D_ind], 'b-.') plt.plot(Ns, avg_norm_of_mean_upper75[:, D_ind], 'b-.') plt.fill_between(Ns, avg_norm_of_mean_lower5[:, D_ind].ravel(), avg_norm_of_mean_upper95[:, D_ind].ravel(), color="grey", alpha=.5) plt.title(r"Avg. $\Vert \mathbb E \mathbf{x} \Vert$") plt.grid(True) plt.xlabel(r"$n$") plt.show()
# 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) plt.gca().yaxis.set_visible(False) plt.savefig(fname_base + "_momentum_hmc.eps", axis_inches="tight") plt.figure()
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')
# plot_mcmc_result(result, D1=1, D2=6) # compute how MMD evolves 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)