예제 #1
0
def test_negative_binomial_grad_sparse():
    n = 10
    random_state = np.random.RandomState(seed=42)
    X = random_state.rand(n, 3)

    dis = euclidean_distances(X)
    alpha, beta = -3, 1

    counts = beta * dis**alpha

    grad_dense = negative_binomial.negative_binomial_gradient(X, counts)
    grad_sparse = negative_binomial.negative_binomial_gradient(
        X, sparse.coo_matrix(np.triu(counts, 1)))
    assert_array_almost_equal(grad_dense, grad_sparse)
    assert_array_almost_equal(np.zeros(grad_dense.shape), grad_dense)
예제 #2
0
def test_negative_binomial_grad_biased():
    random_state = np.random.RandomState(seed=42)
    n = 50
    X = random_state.rand(n, 3)
    bias = 0.1 + random_state.rand(n)
    bias = bias.reshape(n, 1)

    counts = euclidean_distances(X)**(-3)
    counts[np.isinf(counts) | np.isnan(counts)] = 0
    counts *= bias.T * bias

    counts_dense = counts
    counts_sparse = sparse.coo_matrix(np.triu(counts, 1))
    grad_dense = negative_binomial.negative_binomial_gradient(X,
                                                              counts_dense,
                                                              bias=bias)
    grad_sparse = negative_binomial.negative_binomial_gradient(X,
                                                               counts_sparse,
                                                               bias=bias)
    assert_array_almost_equal(grad_dense, grad_sparse)
    assert_array_almost_equal(np.array([0, 0.]), grad_dense)
예제 #3
0
def test_negative_binomial_grad_dense():
    n = 10
    random_state = np.random.RandomState(seed=42)
    X = random_state.rand(n, 3)
    dis = euclidean_distances(X)
    alpha, beta = -3, 1

    counts = beta * dis**alpha
    grad_dense = negative_binomial.negative_binomial_gradient(
        X, counts, use_zero_counts=True)
    # Checking that the gradient at the solution is 0
    assert np.all(grad_dense == 0)
예제 #4
0
def test_negative_binomial_grad_sparse():
    n = 10
    random_state = np.random.RandomState(seed=42)
    X = random_state.rand(n, 3)

    dis = euclidean_distances(X)
    alpha, beta = -3, 1

    counts = beta * dis ** alpha
    counts = np.triu(counts)
    counts[np.arange(len(counts)), np.arange(len(counts))] = 0
    counts = sparse.coo_matrix(counts)

    # grad_dense = negative_binomial.negative_binomial_gradient(X, counts)
    grad_sparse = negative_binomial.negative_binomial_gradient(
        X, sparse.coo_matrix(counts))
    # assert_array_almost_equal(grad_dense, grad_sparse)
    assert_array_almost_equal(np.zeros(grad_sparse.shape),
                              grad_sparse)
예제 #5
0
def test_negative_binomial_grad_biased():
    random_state = np.random.RandomState(seed=42)
    n = 50
    X = random_state.rand(n, 3)
    bias = 0.1 + random_state.rand(n)
    bias = bias.reshape(n, 1)

    counts = euclidean_distances(X)**(-3)
    counts[np.isinf(counts) | np.isnan(counts)] = 0
    counts *= bias * bias.T

    counts_dense = counts
    counts_sparse = sparse.coo_matrix(np.triu(counts))
    # grad_dense = negative_binomial.negative_binomial_gradient(
    #    X, counts_dense, -3,
    #    bias=bias,
    #    beta=1)
    grad_sparse = negative_binomial.negative_binomial_gradient(
        X, counts_sparse, -3,
        bias=bias,
        beta=1)
    assert_array_almost_equal(np.array([0, 0.]), grad_sparse)