Beispiel #1
0
def test_consistency(oterm, mean, data):
    x, diag, y, t = data

    # Setup the original GP
    original_gp = original_celerite.GP(oterm, mean=mean)
    original_gp.compute(x, np.sqrt(diag))

    # Setup the new GP
    term = terms.OriginalCeleriteTerm(oterm)
    gp = celerite2.GaussianProcess(term, mean=mean)
    gp.compute(x, diag=diag)

    # "log_likelihood" method
    assert np.allclose(original_gp.log_likelihood(y), gp.log_likelihood(y))

    # "predict" method
    for args in [
            dict(return_cov=False, return_var=False),
            dict(return_cov=False, return_var=True),
            dict(return_cov=True, return_var=False),
    ]:
        assert all(
            np.allclose(a, b) for a, b in zip(
                original_gp.predict(y, **args),
                gp.predict(y, **args),
            ))
        assert all(
            np.allclose(a, b) for a, b in zip(
                original_gp.predict(y, t=t, **args),
                gp.predict(y, t=t, **args),
            ))

    # "sample" method
    seed = 5938
    np.random.seed(seed)
    a = original_gp.sample()
    np.random.seed(seed)
    b = gp.sample()
    assert np.allclose(a, b)

    np.random.seed(seed)
    a = original_gp.sample(size=10)
    np.random.seed(seed)
    b = gp.sample(size=10)
    assert np.allclose(a, b)

    # "sample_conditional" method, numerics make this one a little unstable;
    # just check the shape
    a = original_gp.sample_conditional(y, t=t)
    b = gp.sample_conditional(y, t=t)
    assert a.shape == b.shape

    a = original_gp.sample_conditional(y, size=10)
    b = gp.sample_conditional(y, size=10)
    assert a.shape == b.shape
Beispiel #2
0
def test_consistency(oterm, mean, data):
    x, diag, y, t = data

    # Setup the original GP
    original_gp = original_celerite.GP(oterm, mean=mean)
    original_gp.compute(x, np.sqrt(diag))

    # Setup the new GP
    term = terms.OriginalCeleriteTerm(oterm)
    gp = celerite2.GaussianProcess(term, mean=mean)
    gp.compute(x, diag=diag)

    # "log_likelihood" method
    assert np.allclose(original_gp.log_likelihood(y), gp.log_likelihood(y))

    # Apply inverse
    assert np.allclose(
        np.squeeze(original_gp.apply_inverse(y)), gp.apply_inverse(y)
    )

    conditional_t = gp.condition(y, t=t)
    mu, cov = original_gp.predict(y, t=t, return_cov=True)
    assert np.allclose(conditional_t.mean, mu)
    assert np.allclose(conditional_t.variance, np.diag(cov))
    assert np.allclose(conditional_t.covariance, cov)

    conditional = gp.condition(y)
    mu, cov = original_gp.predict(y, return_cov=True)
    assert np.allclose(conditional.mean, mu)
    assert np.allclose(conditional.variance, np.diag(cov))
    assert np.allclose(conditional.covariance, cov)

    # "sample" method
    seed = 5938
    np.random.seed(seed)
    a = original_gp.sample()
    np.random.seed(seed)
    b = gp.sample()
    assert np.allclose(a, b)

    np.random.seed(seed)
    a = original_gp.sample(size=10)
    np.random.seed(seed)
    b = gp.sample(size=10)
    assert np.allclose(a, b)

    # "sample_conditional" method, numerics make this one a little unstable;
    # just check the shape
    a = original_gp.sample_conditional(y, t=t)
    b = conditional_t.sample()
    assert a.shape == b.shape

    a = original_gp.sample_conditional(y, size=10)
    b = conditional.sample(size=10)
    assert a.shape == b.shape
Beispiel #3
0
def test_consistency(oterm):
    # Check that the coefficients are all correct
    term = _convert_kernel(oterm)
    for v1, v2 in zip(oterm.get_all_coefficients(), term.get_coefficients()):
        assert np.allclose(v1, v2)
    for v1, v2 in zip(
            terms.OriginalCeleriteTerm(oterm).get_coefficients(),
            term.get_coefficients(),
    ):
        assert np.allclose(v1, v2)

    # Make sure that the covariance matrix is right
    np.random.seed(40582)
    x = np.sort(np.random.uniform(0, 10, 50))
    diag = np.random.uniform(0.1, 0.3, len(x))
    assert np.allclose(oterm.get_value(x), term.get_value(x))

    tau = x[:, None] - x[None, :]
    K = term.get_value(tau)
    assert np.allclose(oterm.get_value(tau), K)

    # And the power spectrum
    omega = np.linspace(-10, 10, 500)
    assert np.allclose(oterm.get_psd(omega), term.get_psd(omega))

    # Add in the diagonal
    K[np.diag_indices_from(K)] += diag

    # Matrix vector multiply
    y = np.sin(x)
    value = term.dot(x, diag, y)
    assert np.allclose(y, np.sin(x))
    assert np.allclose(value, np.dot(K, y))

    # Matrix-matrix multiply
    y = np.vstack([x]).T
    value = term.dot(x, diag, y)
    assert np.allclose(value, np.dot(K, y))