示例#1
0
def plot_acceptance_heatmap(Ns, Ds, acc):
    masked_array = np.ma.array(acc, mask=np.isnan(acc))
    cmap = matplotlib.cm.jet
    cmap.set_bad('w', 1.)

    plt.pcolor(Ns, Ds, masked_array, cmap=cmap)
    plt.yscale("log")
    plt.xlim([np.min(Ns), np.max(Ns)])
    plt.ylim([np.min(Ds), np.max(Ds)])
    plt.xlabel(r"$n$")
    plt.ylabel(r"$d$")
    plt.colorbar()
示例#2
0
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
示例#3
0
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
示例#4
0
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')
示例#5
0
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()
示例#6
0
    plot_array(Xs_p, Ys_p, np.exp(M))
    plot_2d_trajectory(Ps_est)
    plt.gca().xaxis.set_visible(False)
    plt.gca().yaxis.set_visible(False)
    plt.savefig(fname_base + "_momentum_kmc.eps", axis_inches="tight")

    # ylim for Hamiltonian plots
    ylim = [np.min([Hs.min(), Hs_est.min()]), np.max([Hs.max(), Hs_est.max()])]

    plt.figure()
    plt.title("Hamiltonian")
    plt.plot(Hs)
    plt.ylim(ylim)
    plt.gca().xaxis.set_visible(False)
    plt.gca().get_yaxis().get_major_formatter().set_useOffset(False)
    plt.xlabel("Leap-frog step")
    plt.ylabel(r"$H(p,q)$")
    plt.savefig(fname_base + "_hamiltonian_hmc.eps", axis_inches="tight")

    plt.figure()
    plt.title("Hamiltonian")
    plt.plot(Hs_est)
    plt.ylim(ylim)
    plt.gca().get_yaxis().get_major_formatter().set_useOffset(False)
    plt.gca().xaxis.set_visible(False)
    plt.xlabel("Leap-frog step")
    plt.ylabel(r"$H(p,q)$")
    plt.savefig(fname_base + "_hamiltonian_kmc.eps", axis_inches="tight")

    # ylim for acceptance plots
    ylim = [np.exp(np.min([log_acc.min(), log_acc_est.min()])), 1.]
示例#7
0
    plot_2d_trajectory(Ps_est)
    plt.gca().xaxis.set_visible(False)
    plt.gca().yaxis.set_visible(False)
    plt.tight_layout()
    plt.savefig(fname_base + "_momentum_kmc.eps")

    # ylim for Hamiltonian plots
    ylim = [np.min([Hs.min(), Hs_est.min()]), np.max([Hs.max(), Hs_est.max()])]

    plt.figure()
    plt.title("Hamiltonian")
    plt.plot(Hs)
    plt.ylim(ylim)
    plt.gca().xaxis.set_visible(False)
    plt.gca().get_yaxis().get_major_formatter().set_useOffset(False)
    plt.xlabel("Leap-frog step")
    plt.ylabel(r"$H(p,q)$")
    plt.tight_layout()
    plt.savefig(fname_base + "_hamiltonian_hmc.eps")

    plt.figure()
    plt.title("Hamiltonian")
    plt.plot(Hs_est)
    plt.ylim(ylim)
    plt.gca().get_yaxis().get_major_formatter().set_useOffset(False)
    plt.gca().xaxis.set_visible(False)
    plt.xlabel("Leap-frog step")
    plt.ylabel(r"$H(p,q)$")
    plt.tight_layout()
    plt.savefig(fname_base + "_hamiltonian_kmc.eps")
示例#8
0
            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()