def test_compute_b_matches_sym():
    sigma = 1.
    Z = np.random.randn(10, 2)
    
    K = gaussian_kernel(Z, sigma=sigma)
    b = develop_gaussian.compute_b_sym(Z, K, sigma=sigma)
    b_sym = gaussian.compute_b(Z, Z, K, sigma=sigma)
    assert_allclose(b, b_sym)
def test_compute_b_matches_sym():
    sigma = 1.
    Z = np.random.randn(10, 2)

    K = gaussian_kernel(Z, sigma=sigma)
    b = develop_gaussian.compute_b_sym(Z, K, sigma=sigma)
    b_sym = gaussian.compute_b(Z, Z, K, sigma=sigma)
    assert_allclose(b, b_sym)
def test_compute_b_sym_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 = develop_gaussian.compute_b_sym(Z, K, sigma)
    y = develop_gaussian_low_rank.compute_b_sym(Z, R.T, sigma)
    assert_allclose(x, y, atol=5e-1)
def test_compute_b_sym_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 = develop_gaussian.compute_b_sym(Z, K, sigma)
    y = develop_gaussian_low_rank.compute_b_sym(Z, R.T, sigma)
    assert_allclose(x, y, atol=5e-1)
def test_compute_b_sym_against_paper():
    sigma = 1.
    D = 1
    Z = np.random.randn(1, D)
    K = gaussian_kernel(Z, sigma=sigma)
    b = develop_gaussian.compute_b_sym(Z, K, sigma)

    # compute by hand, well, it's just -k since rest is zero (look at it)
    x = Z[0]
    k = K[0, 0]
    b_paper = 2. / sigma * (k * (x**2) + (x**2) * k - 2 * x * k * x) - k

    assert_equal(b, b_paper)
def test_objective_sym_same_as_from_estimation():
    sigma = 1.
    lmbda = 1.
    Z = np.random.randn(100, 2)

    K = gaussian_kernel(Z, sigma=sigma)
    a = develop_gaussian.fit_sym(Z, sigma, lmbda, K)
    C = develop_gaussian.compute_C_sym(Z, K, sigma)
    b = develop_gaussian.compute_b_sym(Z, K, sigma)
    J = develop_gaussian.objective_sym(Z, sigma, lmbda, a, K, b, C)

    J2 = develop_gaussian.objective_sym(Z, sigma, lmbda, a, K)
    assert_almost_equal(J, J2)
def test_compute_b_sym_against_paper():
    sigma = 1.
    D = 1
    Z = np.random.randn(1, D)
    K = gaussian_kernel(Z, sigma=sigma)
    b = develop_gaussian.compute_b_sym(Z, K, sigma)
    
    # compute by hand, well, it's just -k since rest is zero (look at it)
    x = Z[0]
    k = K[0, 0]
    b_paper = 2. / sigma * (k * (x ** 2) + (x ** 2) * k - 2 * x * k * x) - k
    
    assert_equal(b, b_paper)
def test_objective_sym_same_as_from_estimation():
    sigma = 1.
    lmbda = 1.
    Z = np.random.randn(100, 2)
    
    K = gaussian_kernel(Z, sigma=sigma)
    a = develop_gaussian.fit_sym(Z, sigma, lmbda, K)
    C = develop_gaussian.compute_C_sym(Z, K, sigma)
    b = develop_gaussian.compute_b_sym(Z, K, sigma)
    J = develop_gaussian.objective_sym(Z, sigma, lmbda, a, K, b, C)
    
    J2 = develop_gaussian.objective_sym(Z, sigma, lmbda, a, K)
    assert_almost_equal(J, J2)
def test_objective_matches_sym_precomputed_KbC():
    sigma = 1.
    lmbda = 1.
    Z = np.random.randn(100, 2)
    K = gaussian_kernel(Z, sigma=sigma)

    alpha = np.random.randn(len(Z))
    C = develop_gaussian.compute_C_sym(Z, K, sigma)
    b = develop_gaussian.compute_b_sym(Z, K, sigma)

    K = gaussian_kernel(Z, sigma=sigma)
    J_sym = develop_gaussian.objective_sym(Z, sigma, lmbda, alpha, K, b, C)
    J = gaussian.objective(Z, Z, sigma, lmbda, alpha, K_XY=K, b=b, C=C)

    assert_equal(J, J_sym)
def test_objective_matches_sym_precomputed_KbC():
    sigma = 1.
    lmbda = 1.
    Z = np.random.randn(100, 2)
    K = gaussian_kernel(Z, sigma=sigma)
    
    alpha = np.random.randn(len(Z))
    C = develop_gaussian.compute_C_sym(Z, K, sigma)
    b = develop_gaussian.compute_b_sym(Z, K, sigma)
    
    K = gaussian_kernel(Z, sigma=sigma)
    J_sym = develop_gaussian.objective_sym(Z, sigma, lmbda, alpha, K, b, C)
    J = gaussian.objective(Z, Z, sigma, lmbda, alpha, K_XY=K, b=b, C=C)
    
    assert_equal(J, J_sym)
def test_compute_b_against_initial_notebook():
    D = 2
    sigma = 1.
    Z = np.random.randn(100, D)
    K = gaussian_kernel(Z, sigma=sigma)

    # build matrix expressions from notes
    m = Z.shape[0]
    D = Z.shape[1]
    S = Z * Z

    b = np.zeros(m)
    for l in np.arange(D):
        s_l = S[:, l]
        x_l = Z[:, l]
        b += 2. / sigma * (K.dot(s_l) \
                        + np.diag(s_l).dot(K).dot(np.ones(m)) \
                        - 2 * np.diag(x_l).dot(K).dot(x_l)) - K.dot(np.ones(m))

    b_test = develop_gaussian.compute_b_sym(Z, K, sigma)

    assert_allclose(b, b_test)
def test_compute_b_against_initial_notebook():
    D = 2
    sigma = 1.
    Z = np.random.randn(100, D)
    K = gaussian_kernel(Z, sigma=sigma)
    
    # build matrix expressions from notes
    m = Z.shape[0]
    D = Z.shape[1]
    S = Z * Z
    
    b = np.zeros(m)
    for l in np.arange(D):
        s_l = S[:, l]
        x_l = Z[:, l]
        b += 2. / sigma * (K.dot(s_l) \
                        + np.diag(s_l).dot(K).dot(np.ones(m)) \
                        - 2 * np.diag(x_l).dot(K).dot(x_l)) - K.dot(np.ones(m))
    
    b_test = develop_gaussian.compute_b_sym(Z, K, sigma)
    
    assert_allclose(b, b_test)