示例#1
0
def plot_repetitions_heatmap(fname):
    results = result_dict_from_file(fname)
    # acc_mean, acc_est_mean, vol, vol_est, steps_taken
    fun = lambda x: x.shape[0]
    Ds, Ns, repetitions = gen_sparse_2d_array_from_dict(results,
                                                        fun,
                                                        default_value=0)
    plt.figure()
    plot_acceptance_heatmap(Ns, Ds, repetitions)
    plt.xscale('log')
    plt.title("Repetitions")
示例#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
    Qs_est, Ps_est = leapfrog(q0, dlogq_est, p0, dlogp, step_size, num_steps)
    Hs = compute_hamiltonian(Qs, Ps, logq, logp)
    Hs_est = compute_hamiltonian(Qs_est, Ps_est, logq, logp)

    # 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")