K = hmc.bivariate_normal_K(m1, m2) dUdq = hmc.bivariate_normal_dUdq(γ, σ1, σ2) dKdp = hmc.bivariate_normal_dKdp(m1, m2) momentum_generator = hmc.bivariate_normal_momentum_generator(m1, m2) potential_pdf = hmc.potential_distribution(U) momentum_pdf = hmc.momentum_distribution(K) file_prefix = "hmc-bivariate-normal-γ-0.0" # %% H, p, q, accepted = hmc.HMC(q0, U, K, dUdq, dKdp, hmc.momentum_verlet_integrator, momentum_generator, nsample, tmax, ε) # %% bv.contour_plot(0.0, 0.0, σ1, σ2, γ, [0.01, 0.025, 0.05, 0.1, 0.15, 0.2], "hamiltonian_monte_carlo", f"{file_prefix}-target-distribution-1") # %% title = f"HMC Normal" + f": γ={γ}, nsample={nsample}, accepted={int(100.0*float(accepted)/float(nsample))}%, " + r"$t_{max}$=" + f"{format(tmax, '2.2f')}" hmc.pdf_samples_contour(potential_pdf, q[:,0], q[:,1], q1range, q2range, [0.01, 0.025, 0.05, 0.1, 0.15, 0.2], [r"$q_1$", r"$q_2$"], title, f"{file_prefix}-position-samples-contour-1") # %% title = f"HMC Normal" + f": γ={γ}, nsample={nsample}, accepted={int(100.0*float(accepted)/float(nsample))}%, " + r"$t_{max}$=" + f"{format(tmax, '2.2f')}" hmc.pdf_samples_contour(momentum_pdf, p[:,0], p[:,1], p1range, p2range, [0.01, 0.025, 0.05, 0.1, 0.15, 0.2], [r"$p_1$", r"$p_2$"], title, f"{file_prefix}-momentum-samples-contour-1") # %% title = f"HMC Normal " + r"$q_1$" + f": γ={γ}, nsample={nsample}, accepted={int(100.0*float(accepted)/float(nsample))}%, " + r"$t_{max}$=" + f"{format(tmax, '2.2f')}" gplot.pdf_samples(title, bv.marginal(0.0, 1.0), q[:,0], "hamiltonian_monte_carlo", f"{file_prefix}-position-samples-1")
μ1 = 0.0 μ2 = 0.0 σ1 = 1.0 σ2 = 1.0 γ = 0.0 target_pdf = bv.metropolis_hastings_target_pdf(μ1, μ2, σ1, σ2, γ) q1range = [-3.1*σ1, 3.1*σ1] q2range = [-3.1*σ2, 3.1*σ2] file_prefix = "mh-bivariate-normal-γ-0.0" # %% bv.contour_plot(μ1, μ2, σ1, σ2, γ, [0.01, 0.05, 0.1, 0.15, 0.2], "hamiltonian_monte_carlo", "metropolis-hastings-bivariate-normal-target-pdf") # %% q, accepted = mh.component_metropolis_hastings(target_pdf, mh.normal_proposal, mh.normal_generator, stepsize, nsample=nsample, x0=x0) accepted = accepted / 2.0 # %% title = f"MH Bivariate Normal" + f": γ={γ}, nsample={nsample}, accepted={int(100.0*float(accepted)/float(nsample))}%" hmc.pdf_samples_contour(bv.pdf(μ1, μ2, σ1, σ2, γ), q[:,0], q[:,1], q1range, q2range, [0.01, 0.05, 0.1, 0.15, 0.2], [r"$q_1$", r"$q_2$"], title, f"{file_prefix}-samples-contour") # %% title = f"MH Bivariate Normal " + r"$q_1$" + f": γ={γ}, nsample={nsample}, accepted={int(100.0*float(accepted)/float(nsample))}%" gplot.pdf_samples(title, bv.marginal(0.0, σ1), q[:,0], "hamiltonian_monte_carlo", f"{file_prefix}-samples-1")
file_prefix = "gibbs-bivariate-normal-γ-0.95" # %% def gibbs_sample(nsample, x0, f_xy_sample, f_yx_sample): samples = numpy.zeros((nsample, 2)) samples[0] = x0 for i in range(1, nsample): samples[i, 0] = f_xy_sample(samples[i-1, 1]) samples[i, 1] = f_yx_sample(samples[i, 0]) return samples # %% bv.contour_plot(μ1, μ2, σ1, σ2, γ, [0.01, 0.1, 0.2, 0.3, 0.4], "hamiltonian_monte_carlo", "gibbs-bivariate-normal-target-pdf") # %% q = gibbs_sample(nsample, x0, bv_q1q2_generator, bv_q2q1_generator) # %% title = f"Gibbs Bivariate Normal" + f": γ={γ}, nsample={nsample}" hmc.pdf_samples_contour(bv.pdf(μ1, μ2, σ1, σ2, γ), q[:,0], q[:,1], q1range, q2range, [0.01, 0.1, 0.2, 0.3, 0.4], [r"$q_1$", r"$q_2$"], title, f"{file_prefix}-samples-contour") # %% title = f"Gibbs Bivariate Normal "+ r"$q_1$" + f": γ={γ}, nsample={nsample}" gplot.pdf_samples(title, bv.marginal(0.0, σ1), q[:,0], "hamiltonian_monte_carlo", f"{file_prefix}-samples-1")