Пример #1
0
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')
Пример #2
0
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')
Пример #3
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")
Пример #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
    # simulate true and approximate Hamiltonian
    Qs, Ps = leapfrog(q0, dlogq, p0, dlogp, step_size, num_steps)
    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")