def test_negative_binomial_obj_sparse_dispersion_biased():
    n = 10
    random_state = np.random.RandomState(42)
    X = random_state.rand(n, 3)
    dis = euclidean_distances(X)
    alpha, beta = -3, 1

    counts = beta * dis ** alpha
    _, mean, variance, _ = dispersion.compute_mean_variance(
        counts**2,
        np.array([counts.shape[0]]))
    mean, variance = mean[:-1], variance[:-1]

    d = dispersion.ExponentialDispersion()
    d.fit(mean, variance)

    counts = np.triu(counts)
    counts[np.arange(len(counts)), np.arange(len(counts))] = 0
    counts = sparse.coo_matrix(counts)

    obj_sparse = negative_binomial_structure.negative_binomial_obj(
        X, counts, dispersion=d, alpha=alpha, beta=beta)
    obj_dense = negative_binomial_structure.negative_binomial_obj(
        X, counts.toarray(), dispersion=d, alpha=alpha, beta=beta)

    obj_ = negative_binomial_structure.negative_binomial_obj(
        random_state.rand(*X.shape),
        counts, dispersion=d, alpha=alpha, beta=beta)
    assert(obj_sparse < obj_)
    assert_almost_equal(obj_sparse, obj_dense, 6)
def test_negative_binomial_obj_sparse():
    n = 10
    random_state = np.random.RandomState(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)

    obj = negative_binomial_structure.negative_binomial_obj(
        X, counts, alpha=alpha, beta=beta)

    # Create a random structure
    rand_X = random_state.rand(*X.shape)

    # Compute the objective function for both sparse and dense
    obj_sparse = negative_binomial_structure.negative_binomial_obj(
        rand_X,
        counts, alpha=alpha, beta=beta)
    obj_dense = negative_binomial_structure.negative_binomial_obj(
        rand_X,
        counts.toarray(), alpha=alpha, beta=beta)

    # The objective for a random point should be larger than for the solution
    assert(obj < obj_sparse)
    # The objective in sparse and dense should be equal
    assert_almost_equal(obj_sparse, obj_dense)
def test_negative_binomial_obj_dense():

    lengths = np.array([5, 5])
    n = lengths.sum()

    random_state = np.random.RandomState(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

    obj = negative_binomial_structure.negative_binomial_obj(
        X, counts, alpha=alpha, beta=beta)

    obj_ = negative_binomial_structure.negative_binomial_obj(
        random_state.rand(*X.shape),
        counts, alpha=alpha, beta=beta)
    assert(obj < obj_)

    obj = negative_binomial_structure.negative_binomial_obj(
        X, counts, alpha=alpha, beta=beta, use_zero_counts=True)

    obj_ = negative_binomial_structure.negative_binomial_obj(
        random_state.rand(*X.shape),
        counts, alpha=alpha, beta=beta, use_zero_counts=True)
    assert(obj < obj_)
def test_negative_binomial_obj_sparse_dispersion_biased():
    n = 10
    random_state = np.random.RandomState(42)
    X = random_state.rand(n, 3)
    dis = euclidean_distances(X)
    alpha, beta = -3, 1

    counts = beta * dis ** alpha

    return True
    from minorswing import dispersion
    mean, variance = dispersion.compute_mean_variance(
        counts**2,
        np.array([counts.shape[0]]))
    mean, variance = mean[:-1], variance[:-1]
    d = dispersion.Dispersion()
    d.fit(mean, variance)

    counts = np.triu(counts)
    counts[np.arange(len(counts)), np.arange(len(counts))] = 0
    counts = sparse.coo_matrix(counts)

    obj = negative_binomial_structure.negative_binomial_obj(
        X, counts, dispersion=d, alpha=alpha, beta=beta)

    obj_ = negative_binomial_structure.negative_binomial_obj(
        random_state.rand(*X.shape),
        counts, dispersion=d, alpha=alpha, beta=beta)
    assert(obj < obj_)
def test_negative_binomial_obj_sparse():
    n = 10
    random_state = np.random.RandomState(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)

    obj = negative_binomial_structure.negative_binomial_obj(
        X, counts, alpha=alpha, beta=beta)

    obj_ = negative_binomial_structure.negative_binomial_obj(
        random_state.rand(*X.shape),
        counts, alpha=alpha, beta=beta)
    assert(obj < obj_)