Ejemplo n.º 1
0
def test_dot_L(seed=42):
    solver = celerite.CholeskySolver()
    np.random.seed(seed)
    t = np.sort(np.random.rand(5))
    b = np.random.randn(len(t), 5)
    yerr = np.random.uniform(0.1, 0.5, len(t))

    alpha_real = np.array([1.3, 0.2])
    beta_real = np.array([0.5, 0.8])
    alpha_complex_real = np.array([0.1])
    alpha_complex_imag = np.array([0.0])
    beta_complex_real = np.array([1.5])
    beta_complex_imag = np.array([0.1])

    K = get_kernel_value(
        alpha_real, beta_real, alpha_complex_real, alpha_complex_imag,
        beta_complex_real, beta_complex_imag, t[:, None] - t[None, :]
    )
    K[np.diag_indices_from(K)] += yerr**2
    L = np.linalg.cholesky(K)
    x0 = np.dot(L, b)

    solver.compute(
        0.0, alpha_real, beta_real, alpha_complex_real, alpha_complex_imag,
        beta_complex_real, beta_complex_imag, t, yerr**2)
    x = solver.dot_L(b)
    assert np.allclose(x0, x)
Ejemplo n.º 2
0
def _test_solve(alpha_real, beta_real, alpha_complex_real, alpha_complex_imag,
                beta_complex_real, beta_complex_imag, seed=42):
    solver = celerite.CholeskySolver()
    np.random.seed(seed)
    t = np.sort(np.random.rand(500))
    diag = np.random.uniform(0.1, 0.5, len(t))
    b = np.random.randn(len(t))

    with pytest.raises(RuntimeError):
        solver.log_determinant()
    with pytest.raises(RuntimeError):
        solver.dot_solve(b)

    solver.compute(
        0.0, alpha_real, beta_real, alpha_complex_real, alpha_complex_imag,
        beta_complex_real, beta_complex_imag, t, diag
    )
    K = get_kernel_value(
        alpha_real, beta_real, alpha_complex_real, alpha_complex_imag,
        beta_complex_real, beta_complex_imag, t[:, None] - t[None, :]
    )
    K[np.diag_indices_from(K)] += diag
    assert np.allclose(solver.solve(b).T, np.linalg.solve(K, b))

    b = np.random.randn(len(t), 5)
    assert np.allclose(solver.solve(b), np.linalg.solve(K, b))
Ejemplo n.º 3
0
def test_dot(with_general, seed=42):
    solver = celerite.CholeskySolver()
    np.random.seed(seed)
    t = np.sort(np.random.rand(500))
    b = np.random.randn(len(t), 5)

    alpha_real = np.array([1.3, 0.2])
    beta_real = np.array([0.5, 0.8])
    alpha_complex_real = np.array([0.1])
    alpha_complex_imag = np.array([0.0])
    beta_complex_real = np.array([1.5])
    beta_complex_imag = np.array([0.1])

    K = get_kernel_value(alpha_real, beta_real, alpha_complex_real,
                         alpha_complex_imag, beta_complex_real,
                         beta_complex_imag, t[:, None] - t[None, :])

    if with_general:
        U = np.vander(t - np.mean(t), 4).T
        V = U * np.random.rand(4)[:, None]
        A = np.sum(U * V, axis=0) + 1e-8

        K[np.diag_indices_from(K)] += A
        K += np.tril(np.dot(U.T, V), -1) + np.triu(np.dot(V.T, U), 1)
    else:
        A = np.empty(0)
        U = np.empty((0, 0))
        V = np.empty((0, 0))

    x0 = np.dot(K, b)

    x = solver.dot(0.0, alpha_real, beta_real, alpha_complex_real,
                   alpha_complex_imag, beta_complex_real, beta_complex_imag, A,
                   U, V, t, b)
    assert np.allclose(x0, x)
Ejemplo n.º 4
0
def test_pickle(with_general, seed=42):
    solver = celerite.CholeskySolver()
    np.random.seed(seed)
    t = np.sort(np.random.rand(500))
    diag = np.random.uniform(0.1, 0.5, len(t))
    y = np.sin(t)

    if with_general:
        U = np.vander(t - np.mean(t), 4).T
        V = U * np.random.rand(4)[:, None]
        A = np.sum(U * V, axis=0) + 1e-8
    else:
        A = np.empty(0)
        U = np.empty((0, 0))
        V = np.empty((0, 0))

    alpha_real = np.array([1.3, 1.5])
    beta_real = np.array([0.5, 0.2])
    alpha_complex_real = np.array([1.0])
    alpha_complex_imag = np.array([0.1])
    beta_complex_real = np.array([1.0])
    beta_complex_imag = np.array([1.0])

    def compare(solver1, solver2):
        assert solver1.computed() == solver2.computed()
        if not solver1.computed():
            return
        assert np.allclose(solver1.log_determinant(),
                           solver2.log_determinant())
        assert np.allclose(solver1.dot_solve(y), solver2.dot_solve(y))

    s = pickle.dumps(solver, -1)
    solver2 = pickle.loads(s)
    compare(solver, solver2)

    solver.compute(0.0, alpha_real, beta_real, alpha_complex_real,
                   alpha_complex_imag, beta_complex_real, beta_complex_imag, A,
                   U, V, t, diag)
    solver2 = pickle.loads(pickle.dumps(solver, -1))
    compare(solver, solver2)

    # Test that models can be pickled too.
    kernel = terms.RealTerm(0.5, 0.1)
    kernel += terms.ComplexTerm(0.6, 0.7, 1.0)
    gp1 = GP(kernel)
    gp1.compute(t, diag)
    s = pickle.dumps(gp1, -1)
    gp2 = pickle.loads(s)
    assert np.allclose(gp1.log_likelihood(y), gp2.log_likelihood(y))
Ejemplo n.º 5
0
def _test_log_determinant(alpha_real, beta_real, alpha_complex_real,
                          alpha_complex_imag, beta_complex_real,
                          beta_complex_imag, seed=42):
    solver = celerite.CholeskySolver()
    np.random.seed(seed)
    t = np.sort(np.random.rand(5))
    diag = np.random.uniform(0.1, 0.5, len(t))

    solver.compute(
        0.0, alpha_real, beta_real, alpha_complex_real, alpha_complex_imag,
        beta_complex_real, beta_complex_imag, t, diag
    )
    K = get_kernel_value(
        alpha_real, beta_real, alpha_complex_real, alpha_complex_imag,
        beta_complex_real, beta_complex_imag, t[:, None] - t[None, :]
    )
    K[np.diag_indices_from(K)] += diag
    assert np.allclose(solver.log_determinant(), np.linalg.slogdet(K)[1])
Ejemplo n.º 6
0
def test_carma(seed=42):
    solver = celerite.CholeskySolver()
    np.random.seed(seed)
    t = np.sort(np.random.uniform(0, 5, 100))
    yerr = 0.1 + np.zeros_like(t)
    y = np.sin(t) + yerr * np.random.randn(len(t))

    carma_solver = CARMASolver(-0.5, np.array([0.1, 0.05, 0.01]),
                               np.array([0.2, 0.1]))
    carma_ll = carma_solver.log_likelihood(t, y, yerr)
    params = carma_solver.get_celerite_coeffs()

    solver.compute(0.0, params[0], params[1], params[2], params[3],
                   params[4], params[5], np.empty(0), np.empty((0, 0)),
                   np.empty((0, 0)), t, yerr**2)
    celerite_ll = -0.5 * (solver.dot_solve(y) + solver.log_determinant() +
                          len(t) * np.log(2 * np.pi))
    assert np.allclose(carma_ll, celerite_ll)
Ejemplo n.º 7
0
def _test_solve(alpha_real,
                beta_real,
                alpha_complex_real,
                alpha_complex_imag,
                beta_complex_real,
                beta_complex_imag,
                seed=42,
                with_general=False):
    solver = celerite.CholeskySolver()
    np.random.seed(seed)
    t = np.sort(np.random.rand(500))
    diag = np.random.uniform(0.1, 0.5, len(t))
    b = np.random.randn(len(t))

    with pytest.raises(RuntimeError):
        solver.log_determinant()
    with pytest.raises(RuntimeError):
        solver.dot_solve(b)

    if with_general:
        U = np.vander(t - np.mean(t), 4).T
        V = U * np.random.rand(4)[:, None]
        A = np.sum(U * V, axis=0) + 1e-8
    else:
        A = np.empty(0)
        U = np.empty((0, 0))
        V = np.empty((0, 0))

    solver.compute(0.0, alpha_real, beta_real, alpha_complex_real,
                   alpha_complex_imag, beta_complex_real, beta_complex_imag, A,
                   U, V, t, diag)
    K = get_kernel_value(alpha_real, beta_real, alpha_complex_real,
                         alpha_complex_imag, beta_complex_real,
                         beta_complex_imag, t[:, None] - t[None, :])
    K[np.diag_indices_from(K)] += diag

    if len(A):
        K[np.diag_indices_from(K)] += A
        K += np.tril(np.dot(U.T, V), -1) + np.triu(np.dot(V.T, U), 1)

    assert np.allclose(solver.solve(b).T, np.linalg.solve(K, b))

    b = np.random.randn(len(t), 5)
    assert np.allclose(solver.solve(b), np.linalg.solve(K, b))
Ejemplo n.º 8
0
def test_pickle(seed=42):
    solver = celerite.CholeskySolver()
    np.random.seed(seed)
    t = np.sort(np.random.rand(500))
    diag = np.random.uniform(0.1, 0.5, len(t))
    y = np.sin(t)

    alpha_real = np.array([1.3, 1.5])
    beta_real = np.array([0.5, 0.2])
    alpha_complex_real = np.array([1.0])
    alpha_complex_imag = np.array([0.1])
    beta_complex_real = np.array([1.0])
    beta_complex_imag = np.array([1.0])

    def compare(solver1, solver2):
        assert solver1.computed() == solver2.computed()
        if not solver1.computed():
            return
        assert np.allclose(solver1.log_determinant(),
                           solver2.log_determinant())
        assert np.allclose(solver1.dot_solve(y),
                           solver2.dot_solve(y))

    s = pickle.dumps(solver, -1)
    solver2 = pickle.loads(s)
    compare(solver, solver2)

    solver.compute(
        0.0, alpha_real, beta_real, alpha_complex_real, alpha_complex_imag,
        beta_complex_real, beta_complex_imag, t, diag
    )
    solver2 = pickle.loads(pickle.dumps(solver, -1))
    compare(solver, solver2)

    # Test that models can be pickled too.
    kernel = terms.RealTerm(0.5, 0.1)
    kernel += terms.ComplexTerm(0.6, 0.7, 1.0)
    gp1 = GP(kernel)
    gp1.compute(t, diag)
    s = pickle.dumps(gp1, -1)
    gp2 = pickle.loads(s)
    assert np.allclose(gp1.log_likelihood(y), gp2.log_likelihood(y))
Ejemplo n.º 9
0
def test_dot(seed=42):
    solver = celerite.CholeskySolver()
    np.random.seed(seed)
    t = np.sort(np.random.rand(500))
    b = np.random.randn(len(t), 5)

    alpha_real = np.array([1.3, 0.2])
    beta_real = np.array([0.5, 0.8])
    alpha_complex_real = np.array([0.1])
    alpha_complex_imag = np.array([0.0])
    beta_complex_real = np.array([1.5])
    beta_complex_imag = np.array([0.1])

    K = get_kernel_value(
        alpha_real, beta_real, alpha_complex_real, alpha_complex_imag,
        beta_complex_real, beta_complex_imag, t[:, None] - t[None, :]
    )
    x0 = np.dot(K, b)

    x = solver.dot(
        0.0, alpha_real, beta_real, alpha_complex_real, alpha_complex_imag,
        beta_complex_real, beta_complex_imag, t, b
    )
    assert np.allclose(x0, x)