def test_hamiltonian_monte_carlo(integrator): # This mostly tests consistency. Tolerance chosen by experiment # Do statistical tests on your own time. np.random.seed(1) neg_log_p = AutogradPotential(neg_log_normal(2, 0.1)) samples, *_ = hamiltonian_monte_carlo(100, neg_log_p, np.array(0.0), integrator=integrator) assert_allclose(2.0, np.mean(samples), atol=0.007) assert_allclose(0.1, np.std(samples), atol=0.16)
def test_hamiltonian_monte_carlo_mv(): np.random.seed(1) mu = np.arange(2) cov = 0.8 * np.ones((2, 2)) + 0.2 * np.eye(2) neg_log_p = AutogradPotential(neg_log_mvnormal(mu, cov)) samples, *_ = hamiltonian_monte_carlo(100, neg_log_p, np.zeros(mu.shape), path_len=2.0) assert_allclose(mu, np.mean(samples, axis=0), atol=0.21) assert_allclose(cov, np.cov(samples.T), atol=0.31)
def test_leapfrog(): neg_log_p = AutogradPotential(neg_log_normal(2, 0.1)) q, p = np.array(0.0), np.array(2.0) path_len, step_size = 1, 0.1 V, dVdq = neg_log_p(q) # Should be reversible q_new, p_new, _, dVdq = leapfrog(q, p, dVdq, neg_log_p, path_len, step_size) q_new, p_new, _, _ = leapfrog(q_new, p_new, dVdq, neg_log_p, path_len, step_size) assert_almost_equal(q_new, q) assert_almost_equal(p_new, p)
def test_leapfrog_mv(): mu = np.arange(10) cov = 0.8 * np.ones((10, 10)) + 0.2 * np.eye(10) neg_log_p = AutogradPotential(neg_log_mvnormal(mu, cov)) q, p = np.zeros(mu.shape), np.ones(mu.shape) path_len, step_size = 1, 0.1 V, dVdq = neg_log_p(q) # Should be reversible q_new, p_new, _, dVdq = leapfrog(q, p, dVdq, neg_log_p, path_len, step_size) q_new, p_new, _, _ = leapfrog(q_new, p_new, dVdq, neg_log_p, path_len, step_size) assert_almost_equal(q_new, q) assert_almost_equal(p_new, p)
"font.family": "serif", "figure.facecolor": "#fffff8", "axes.facecolor": "#fffff8", "figure.constrained_layout.use": True, "font.size": 14.0, "hist.bins": "auto", "lines.linewidth": 3.0, "lines.markeredgewidth": 2.0, "lines.markerfacecolor": "none", "lines.markersize": 8.0, } ) ### Example 1 ### samples = hamiltonian_monte_carlo( 2000, AutogradPotential(neg_log_normal(0, 0.1)), initial_position=0.0 ) ### Plot 1 ### fig, ax = plt.subplots(figsize=FIGSIZE) ax.hist(samples, bins="auto") ax.axvline(0, color="C1", linestyle="--") ax.set_title("1D Gaussians!") plt.savefig(os.path.join(HERE, "plot1.png")) ### Example 2 ### samples, positions, momentums, accepted, p_accepts = hmc_slow( 50, AutogradPotential(neg_log_normal(0, 0.1)), 0.0, step_size=0.01 ) ### Plot 2 ###