def test_incomplete_cholesky_1():
    X = np.arange(9.0).reshape(3, 3)
    kernel = lambda X, Y=None: gaussian_kernel(X, Y, sigma=200.)
    temp = incomplete_cholesky(X, kernel, eta=0.8, power=2)
    R, K_chol, I, W = (temp["R"], temp["K_chol"], temp["I"], temp["W"])
    K = kernel(X)

    assert_equal(len(I), 2)
    assert_equal(I[0], 0)
    assert_equal(I[1], 2)

    assert_equal(K_chol.shape, (len(I), len(I)))
    for i in range(len(I)):
        assert_equal(K_chol[i, i], K[I[i], I[i]])

    assert_equal(R.shape, (len(I), len(X)))
    assert_almost_equal(R[0, 0], 1.000000000000000)
    assert_almost_equal(R[0, 1], 0.763379494336853)
    assert_almost_equal(R[0, 2], 0.339595525644939)
    assert_almost_equal(R[1, 0], 0)
    assert_almost_equal(R[1, 1], 0.535992421608228)
    assert_almost_equal(R[1, 2], 0.940571570355992)

    assert_equal(W.shape, (len(I), len(X)))
    assert_almost_equal(W[0, 0], 1.000000000000000)
    assert_almost_equal(W[0, 1], 0.569858199525808)
    assert_almost_equal(W[0, 2], 0)
    assert_almost_equal(W[1, 0], 0)
    assert_almost_equal(W[1, 1], 0.569858199525808)
    assert_almost_equal(W[1, 2], 1)
def test_incomplete_cholesky_2():
    X = np.arange(9.0).reshape(3, 3)
    kernel = lambda X, Y=None: gaussian_kernel(X, Y, sigma=8.)
    temp = incomplete_cholesky(X, kernel, eta=0.999)
    R, K_chol, I, W = (temp["R"], temp["K_chol"], temp["I"], temp["W"])
    K = kernel(X)

    assert_equal(len(I), 2)
    assert_equal(I[0], 0)
    assert_equal(I[1], 2)

    assert_equal(K_chol.shape, (len(I), len(I)))
    for i in range(len(I)):
        assert_equal(K_chol[i, i], K[I[i], I[i]])

    assert_equal(R.shape, (len(I), len(X)))
    assert_almost_equal(R[0, 0], 1.000000000000000)
    assert_almost_equal(R[0, 1], 0.034218118311666)
    assert_almost_equal(R[0, 2], 0.000001370959086)
    assert_almost_equal(R[1, 0], 0)
    assert_almost_equal(R[1, 1], 0.034218071400058)
    assert_almost_equal(R[1, 2], 0.999999999999060)

    assert_equal(W.shape, (len(I), len(X)))
    assert_almost_equal(W[0, 0], 1.000000000000000)
    assert_almost_equal(W[0, 1], 0.034218071400090)
    assert_almost_equal(W[0, 2], 0)
    assert_almost_equal(W[1, 0], 0)
    assert_almost_equal(W[1, 1], 0.034218071400090)
    assert_almost_equal(W[1, 2], 1)
def test_incomplete_cholesky_check_given_rank():
    kernel = lambda X, Y=None: gaussian_kernel(X, Y, sigma=20.)
    X = np.random.randn(300, 10)
    eta = 5
    K_chol = incomplete_cholesky(X, kernel, eta=eta)["K_chol"]

    assert_equal(K_chol.shape[0], eta)
def test_incomplete_cholesky_new_point():
    kernel = lambda X, Y=None: gaussian_kernel(X, Y, sigma=200.)
    X = np.random.randn(1000, 10)
    low_rank_dim = 15
    temp = incomplete_cholesky(X, kernel, eta=low_rank_dim)
    R, I, nu = (temp["R"], temp["I"], temp["nu"])

    # construct train-train kernel matrix approximation using one by one calls
    for i in range(low_rank_dim):
        r = incomplete_cholesky_new_point(X, X[i], kernel, I, R, nu)
        assert_allclose(r, R[:, i])
def test_incomplete_cholesky_3():
    kernel = lambda X, Y=None: gaussian_kernel(X, Y, sigma=200.)
    X = np.random.randn(3000, 10)
    temp = incomplete_cholesky(X, kernel, eta=0.001)
    R, K_chol, I, W = (temp["R"], temp["K_chol"], temp["I"], temp["W"])
    K = kernel(X)

    assert_equal(K_chol.shape, (len(I), (len(I))))
    assert_equal(R.shape, (len(I), (len(X))))
    assert_equal(W.shape, (len(I), (len(X))))

    assert_less_equal(np.linalg.norm(K - R.T.dot(R)), .5)
    assert_less_equal(np.linalg.norm(K - W.T.dot(K_chol.dot(W))), .5)
def test_incomplete_cholesky_asymmetric():
    kernel = lambda X, Y=None: gaussian_kernel(X, Y, sigma=1.)
    X = np.random.randn(1000, 10)
    Y = np.random.randn(100, 10)

    low_rank_dim = int(len(X) * 0.8)
    temp = incomplete_cholesky(X, kernel, eta=low_rank_dim)
    R, I, nu = (temp["R"], temp["I"], temp["nu"])

    # construct train-train kernel matrix approximation using one by one calls
    R_test = incomplete_cholesky_new_points(X, Y, kernel, I, R, nu)

    assert_allclose(kernel(X, Y), R.T.dot(R_test), atol=10e-1)
def test_incomplete_cholesky_new_points_euqals_new_point():
    kernel = lambda X, Y=None: gaussian_kernel(X, Y, sigma=200.)
    X = np.random.randn(1000, 10)
    low_rank_dim = 15
    temp = incomplete_cholesky(X, kernel, eta=low_rank_dim)
    R, I, nu = (temp["R"], temp["I"], temp["nu"])

    R_test_full = incomplete_cholesky_new_points(X, X, kernel, I, R, nu)

    # construct train-train kernel matrix approximation using one by one calls
    R_test = np.zeros(R.shape)
    for i in range(low_rank_dim):
        R_test[:, i] = incomplete_cholesky_new_point(X, X[i], kernel, I, R, nu)
        assert_allclose(R_test[:, i], R_test_full[:, i])
Ejemplo n.º 8
0
def test_compute_b_low_rank_matches_full():
    sigma = 1.
    X = np.random.randn(100, 2)
    Y = np.random.randn(50, 2)

    low_rank_dim = int(len(X) * 0.9)
    kernel = lambda X, Y: gaussian_kernel(X, Y, sigma=sigma)
    K_XY = kernel(X, Y)
    temp = incomplete_cholesky(X, kernel, eta=low_rank_dim)
    I, R, nu = (temp["I"], temp["R"], temp["nu"])
    R_test = incomplete_cholesky_new_points(X, Y, kernel, I, R, nu)

    x = _compute_b(X, Y, K_XY, sigma)
    y = _compute_b_low_rank(X, Y, R.T, R_test.T, sigma)
    assert_allclose(x, y, atol=5e-1)
Ejemplo n.º 9
0
def test_score_matching_low_rank_matches_sym():
    sigma = 1.
    lmbda = 1.
    Z = np.random.randn(100, 2)
    low_rank_dim = int(len(Z) * .9)

    kernel = lambda X, Y: gaussian_kernel(X, Y, sigma=sigma)

    temp = incomplete_cholesky(Z, kernel, eta=low_rank_dim)
    I, R, nu = (temp["I"], temp["R"], temp["nu"])
    R_test = incomplete_cholesky_new_points(Z, Z, kernel, I, R, nu)

    a_sym = score_matching_low_rank(Z, Z, sigma, lmbda, R.T, R_test.T)
    a = score_matching_sym_low_rank(Z, sigma, lmbda, R.T)

    assert_allclose(a, a_sym)
Ejemplo n.º 10
0
def apply_C_low_rank_matches_sym():
    sigma = 1.
    N_X = 100
    X = np.random.randn(N_X, 2)

    kernel = lambda X, Y: gaussian_kernel(X, Y, sigma=sigma)
    temp = incomplete_cholesky(X, kernel, eta=0.1)
    I, R, nu = (temp["I"], temp["R"], temp["nu"])

    R_test = incomplete_cholesky_new_points(X, X, kernel, I, R, nu)

    v = np.random.randn(N_X.shape[0])
    lmbda = 1.

    x = _apply_left_C_low_rank(v, X, X, R.T, R_test.T, lmbda)
    y = _apply_left_C_sym_low_rank(v, X, R.T, lmbda)
    assert_allclose(x, y)
Ejemplo n.º 11
0
def test_objective_low_rank_matches_sym():
    sigma = 1.
    lmbda = 1.
    Z = np.random.randn(100, 2)

    kernel = lambda X, Y: gaussian_kernel(X, Y, sigma=sigma)
    alpha = np.random.randn(len(Z))

    temp = incomplete_cholesky(Z, kernel, eta=0.1)
    I, R, nu = (temp["I"], temp["R"], temp["nu"])

    R_test = incomplete_cholesky_new_points(Z, Z, kernel, I, R, nu)

    b = _compute_b_low_rank(Z, Z, R.T, R_test.T, sigma)

    J_sym = _objective_sym_low_rank(Z, sigma, lmbda, alpha, R.T, b)
    J = _objective_low_rank(Z, Z, sigma, lmbda, alpha, R.T, R_test.T, b)

    assert_close(J, J_sym)
Ejemplo n.º 12
0
def test_apply_C_left_low_rank_matches_full():
    sigma = 1.
    N = 100
    X = np.random.randn(N, 2)
    Y = np.random.randn(20, 2)
    low_rank_dim = int(len(X) * .9)
    kernel = lambda X, Y=None: gaussian_kernel(X, Y, sigma=sigma)
    K_XY = kernel(X, Y)
    K = kernel(X)

    temp = incomplete_cholesky(X, kernel, eta=low_rank_dim)
    I, R, nu = (temp["I"], temp["R"], temp["nu"])
    R_test = incomplete_cholesky_new_points(X, Y, kernel, I, R, nu)

    v = np.random.randn(X.shape[0])
    lmbda = 1.

    x = (_compute_C(X, Y, K_XY, sigma) + (K + np.eye(len(X))) * lmbda).dot(v)
    y = _apply_left_C_low_rank(v, X, Y, R.T, R_test.T, lmbda)
    assert_allclose(x, y, atol=1e-1)
Ejemplo n.º 13
0
def test_objective_low_rank_matches_full():
    sigma = 1.
    lmbda = 1.
    X = np.random.randn(100, 2)
    Y = np.random.randn(10, 2)
    low_rank_dim = int(len(X) * 0.9)

    kernel = lambda X, Y: gaussian_kernel(X, Y, sigma=sigma)
    alpha = np.random.randn(len(X))

    K_XY = kernel(X, Y)
    C = _compute_C(X, Y, K_XY, sigma)
    b = _compute_b(X, Y, K_XY, sigma)
    J_full = _objective(X, Y, sigma, lmbda, alpha, K_XY=K_XY, b=b, C=C)

    temp = incomplete_cholesky(X, kernel, eta=low_rank_dim)
    I, R, nu = (temp["I"], temp["R"], temp["nu"])
    R_test = incomplete_cholesky_new_points(X, Y, kernel, I, R, nu)
    b = _compute_b_low_rank(X, Y, R.T, R_test.T, sigma)
    J = _objective_low_rank(X, Y, sigma, lmbda, alpha, R.T, R_test.T, b)

    assert_close(J, J_full, decimal=1)