Example #1
0
def test_score_matching_sym_low_rank_time():
    sigma = 1.
    lmbda = 1.
    N = 20000
    Z = np.random.randn(N, 2)

    R = incomplete_cholesky_gaussian(Z, sigma, eta=0.1)["R"]
    score_matching_sym_low_rank(Z, sigma, lmbda, L=R.T, cg_tol=1e-1)
Example #2
0
def test_compute_b_low_rank_matches_sym():
    sigma = 1.
    X = np.random.randn(10, 2)
    R = incomplete_cholesky_gaussian(X, sigma, eta=0.1)["R"]

    x = _compute_b_low_rank(X, X, R.T, R.T, sigma)
    y = _compute_b_low_rank_sym(X, R.T, sigma)
    assert_allclose(x, y)
Example #3
0
def test_score_matching_sym_low_rank_matches_full():
    sigma = 1.
    lmbda = 1.
    Z = np.random.randn(100, 2)

    a = score_matching_sym(Z, sigma, lmbda)

    R = incomplete_cholesky_gaussian(Z, sigma, eta=0.1)["R"]
    a_cholesky_cg = score_matching_sym_low_rank(Z, sigma, lmbda, L=R.T)
    assert_allclose(a, a_cholesky_cg, atol=3)
Example #4
0
def test_compute_b_sym_low_rank_matches_full():
    sigma = 1.
    Z = np.random.randn(100, 2)
    low_rank_dim = int(len(Z) * .9)
    K = gaussian_kernel(Z, sigma=sigma)
    R = incomplete_cholesky_gaussian(Z, sigma, eta=low_rank_dim)["R"]

    x = _compute_b_sym(Z, K, sigma)
    y = _compute_b_low_rank_sym(Z, R.T, sigma)
    assert_allclose(x, y, atol=5e-1)
Example #5
0
def test_apply_C_left_sym_low_rank_matches_full():
    sigma = 1.
    N = 10
    Z = np.random.randn(N, 2)
    K = gaussian_kernel(Z, sigma=sigma)
    R = incomplete_cholesky_gaussian(Z, sigma, eta=0.1)["R"]

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

    x = (_compute_C_sym(Z, K, sigma) + lmbda * (K + np.eye(len(K)))).dot(v)
    y = _apply_left_C_sym_low_rank(v, Z, R.T, lmbda)
    assert_allclose(x, y, atol=1e-1)
Example #6
0
def test_objective_sym_low_rank_matches_full():
    sigma = 1.
    lmbda = 1.
    Z = np.random.randn(100, 2)

    K = gaussian_kernel(Z, sigma=sigma)
    a_opt = score_matching_sym(Z, sigma, lmbda, K)
    J_opt = _objective_sym(Z, sigma, lmbda, a_opt, K)

    L = incomplete_cholesky_gaussian(Z, sigma, eta=0.01)["R"].T
    a_opt_chol = score_matching_sym_low_rank(Z, sigma, lmbda, L)
    J_opt_chol = _objective_sym_low_rank(Z, sigma, lmbda, a_opt_chol, L)

    assert_almost_equal(J_opt, J_opt_chol, delta=2.)
Example #7
0
def test_objective_sym_low_rank_optimum():
    sigma = 1.
    lmbda = 1.
    Z = np.random.randn(100, 2)

    L = incomplete_cholesky_gaussian(Z, sigma, eta=0.1)["R"].T
    a = score_matching_sym_low_rank(Z, sigma, lmbda, L)
    b = _compute_b_low_rank_sym(Z, L, sigma)
    J_opt = _objective_sym_low_rank(Z, sigma, lmbda, a, L, b)

    for _ in range(10):
        a_random = np.random.randn(len(Z))
        J = _objective_sym_low_rank(Z, sigma, lmbda, a_random, L)
        assert J >= J_opt
Example #8
0
from kmc.score_matching.kernel.incomplete_cholesky import incomplete_cholesky_gaussian
from kmc.score_matching.kernel.kernels import gaussian_kernel
from kmc.score_matching.lite.gaussian_rkhs import score_matching_sym,\
    score_matching_sym_low_rank
import matplotlib.pyplot as plt
import numpy as np

if __name__ == "__main__":
    sigma = 1.
    lmbda = 1.
    N = 1000
    Z = np.random.randn(N, 2)

    a = score_matching_sym(Z, sigma, lmbda)
    R = incomplete_cholesky_gaussian(Z, sigma, eta=0.1)["R"]
    print("Low rank dimension: %d/%d" % (R.shape[0], N))
    a_chol = score_matching_sym_low_rank(Z, sigma, lmbda, L=R.T)

    # compute log-density and true log density
    Xs = np.linspace(-4, 4)
    Ys = np.linspace(-4, 4)
    D = np.zeros((len(Xs), len(Ys)))
    D_chol = np.zeros(D.shape)
    G = np.zeros(D.shape)
    for i in range(len(Xs)):
        for j in range(len(Ys)):
            x = np.array([[Xs[i], Ys[j]]])
            k = gaussian_kernel(Z, x)[:, 0]
            D[j, i] = k.dot(a)
            D_chol[j, i] = k.dot(a_chol)
            G[j, i] = -np.linalg.norm(x)**2