示例#1
0
def test_neg_log_mvnormal():
    mu = np.arange(10)
    cov = 0.8 * np.ones((10, 10)) + 0.2 * np.eye(10)
    neg_log_p = neg_log_mvnormal(mu, cov)
    true_rv = st.multivariate_normal(mu, cov)
    for x in np.random.randn(10, mu.shape[0]):
        assert_almost_equal(neg_log_p(x), -true_rv.logpdf(x))
示例#2
0
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)
示例#3
0
def test_leapfrog_mv():
    mu = np.arange(10)
    cov = 0.8 * np.ones((10, 10)) + 0.2 * np.eye(10)
    neg_log_p = neg_log_mvnormal(mu, cov)

    dlogp = grad(neg_log_p)
    q, p = np.zeros(mu.shape), np.ones(mu.shape)
    path_len, step_size = 1, 0.1

    # Should be reversible
    q_new, p_new, *_ = leapfrog(q, p, dlogp, path_len, step_size)
    q_new, p_new, *_ = leapfrog(q_new, p_new, dlogp, path_len, step_size)
    assert_almost_equal(q_new, q)
    assert_almost_equal(p_new, p)
示例#4
0
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)
示例#5
0
    fig, ax = plt.subplots(figsize=FIGSIZE)
    for q, p in zip(positions, momentums):
        ax.plot(q, p)

    y_min, _ = ax.get_ylim()
    ax.plot(samples, y_min + np.zeros_like(samples), "ko")
    ax.set_xlabel("Position")
    ax.set_ylabel("Momentum")

    ax.set_title("1D Gaussian trajectories in phase space!")
    plt.savefig(os.path.join(HERE, "plot2.png"))

    ### Example 3 ###
    mu = np.zeros(2)
    cov = np.array([[1.0, 0.8], [0.8, 1.0]])
    neg_log_p = AutogradPotential(neg_log_mvnormal(mu, cov))

    samples = hamiltonian_monte_carlo(1000, neg_log_p, np.zeros(2))

    ### Plot 3 ###
    fig, ax = plt.subplots(figsize=FIGSIZE)
    ax.plot(samples[:, 0], samples[:, 1], "o")
    ax.plot(mu[0], mu[1], "o", color="w", ms=20, mfc="C1")
    ax.set_title("Multivariate Gaussians!")
    plt.savefig(os.path.join(HERE, "plot3.png"))

    ### Example 4 ###
    np.random.seed(19)

    samples, positions, momentums, accepted, p_accepts = hmc_slow(
        10, neg_log_p, np.random.randn(2), path_len=4, step_size=0.01,
示例#6
0
    fig, ax = plt.subplots(figsize=FIGSIZE)
    for q, p in zip(positions, momentums):
        ax.plot(q, p)

    y_min, _ = ax.get_ylim()
    ax.plot(samples, y_min + np.zeros_like(samples), "ko")
    ax.set_xlabel("Position")
    ax.set_ylabel("Momentum")

    ax.set_title("1D Gaussian trajectories in phase space!")
    plt.savefig(os.path.join(HERE, "plot2.png"))

    ### Example 3 ###
    mu = np.zeros(2)
    cov = np.array([[1.0, 0.8], [0.8, 1.0]])
    neg_log_p = neg_log_mvnormal(mu, cov)

    samples = hamiltonian_monte_carlo(1000, neg_log_p, np.zeros(2))

    ### Plot 3 ###
    fig, ax = plt.subplots(figsize=FIGSIZE)
    ax.plot(samples[:, 0], samples[:, 1], "o")
    ax.plot(mu[0], mu[1], "o", color="w", ms=20, mfc="C1")
    ax.set_title("Multivariate Gaussians!")
    plt.savefig(os.path.join(HERE, "plot3.png"))

    ### Example 4 ###
    np.random.seed(19)

    samples, positions, momentums, accepted = hmc_slow(10,
                                                       neg_log_p,