def test_estimate_X_biased_dispersion():
    n = 50
    random_state = np.random.RandomState(42)
    X_true = random_state.rand(n, 3) * 10
    dis = euclidean_distances(X_true)
    alpha, beta = -3, 1

    fdis = beta * dis ** alpha
    fdis[np.isinf(fdis)] = 1
    disp = fdis + fdis ** 2
    p = fdis / (fdis + disp)

    counts = random_state.negative_binomial(disp, 1 - p)
    counts = np.triu(counts)
    counts[np.arange(len(counts)), np.arange(len(counts))] = 0
    counts = sparse.coo_matrix(counts, dtype=np.float)

    lengths = np.array([counts.shape[0]])
    _, mean, variance, _ = dispersion.compute_mean_variance(counts, lengths)
    mean, variance = mean[:-1], variance[:-1]
    d = dispersion.ExponentialDispersion()
    d.fit(mean, variance)

    X = negative_binomial_structure.estimate_X(counts, alpha, beta,
                                               dispersion=d,
                                               random_state=random_state)
def test_estimate_X_biased_dispersion():
    n = 50
    random_state = np.random.RandomState(42)
    X_true = random_state.rand(n, 3)
    dis = euclidean_distances(X_true)
    alpha, beta = -3, 1

    fdis = beta * dis ** alpha
    fdis[np.isinf(fdis)] = 1
    dispersion = fdis + fdis ** 2
    p = fdis / (fdis + dispersion)

    counts = random_state.negative_binomial(dispersion, 1 - p)
    counts = np.triu(counts)
    counts[np.arange(len(counts)), np.arange(len(counts))] = 0
    counts = sparse.coo_matrix(counts, dtype=np.float)

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

    X = negative_binomial_structure.estimate_X(counts, alpha, beta,
                                               dispersion=d,
                                               random_state=random_state)
def test_estimate_X():
    n = 25
    random_state = np.random.RandomState(42)
    X_true = random_state.rand(n, 3)
    dis = euclidean_distances(X_true)
    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)

    X = negative_binomial_structure.estimate_X(counts, alpha, beta,
                                               factr=10,
                                               random_state=random_state)
    assert_array_almost_equal(dis,
                              euclidean_distances(X), 2)

    X = negative_binomial_structure.estimate_X(counts.toarray(), alpha, beta,
                                               factr=10,
                                               use_zero_entries=True,
                                               random_state=random_state)
def test_estimate_X():
    n = 50
    random_state = np.random.RandomState(42)
    X_true = random_state.rand(n, 3)
    dis = euclidean_distances(X_true)
    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)

    X = negative_binomial_structure.estimate_X(counts, alpha, beta,
                                               random_state=random_state)
    assert_array_almost_equal(dis,
                              euclidean_distances(X), 2)