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") # %% title = f"HMC Normal " + r"$q_2$" + 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[:,1], "hamiltonian_monte_carlo", f"{file_prefix}-position-samples-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") # %% title = f"MH Bivariate Normal " + r"$q_2$" + f": γ={γ}, nsample={nsample}, accepted={int(100.0*float(accepted)/float(nsample))}%" gplot.pdf_samples(title, bv.marginal(0.0, σ2), q[:,1], "hamiltonian_monte_carlo", f"{file_prefix}-samples-2") # %% title = f"MH Bivariate Normal " + r"$q_1$" + f": γ={γ}, nsample={nsample}, accepted={int(100.0*float(accepted)/float(nsample))}%" vals = q[:,0]
x = numpy.linspace(-3.0*mass, 3.0*mass, 500) hmc.univariate_pdf_plot(pdf, x, "p", f"Momentum PDF, mass={mass}", "hmc-momentum-pdf-1") # %% pdf = hmc.canonical_distribution(potential_energy(σ), kinetic_energy(mass)) hmc.pdf_contour_plot(pdf, [0.01, 0.025, 0.05, 0.1, 0.15, 0.2], xrange, yrange, ["q", "p"], "Canonical Distribution", "hmc-normal-target-phase-space-1") # %% H, p, q, accept = HMC(q0, mass, potential_energy(σ), kinetic_energy(mass), dUdq(σ), dKdp(mass), momentum_verlet_hmc, nsample, nsteps, ε) # %% title = f"HMC Normal: σ={σ}, nsample={nsample}, accepted={accept}" hmc.pdf_samples_contour(pdf, p, q, xrange, yrange, [0.01, 0.025, 0.05, 0.1, 0.15, 0.2], ["q", "p"], title, "hmc-normal-phase-space-histogram-1") # %% hmc.distribution_samples(p, q, xrange, yrange, ["q", "p"], title, "hmc-normal-phase-space-histogram-2") # %% title = f"HMC Normal Target: Δt={ε}, nsteps={nsteps}, nsample={nsample}, accepted={accept}" gplot.pdf_samples(title, target_pdf(σ), q, "hamiltonian_monte_carlo", "hmc-normal-sampled-pdf-1") # %% title = f"HMC Normal Target: Δt={ε}, nsteps={nsteps}, nsample={nsample}, accepted={accept}" time = range(0, len(q)) hmc.time_series(title, q, time, [min(q), max(q)], "hmc-normal-position-timeseries-1")