示例#1
0
def test_constrained_cov_0():
    n = 10
    for _ in range(10):
        g = Graph(n)
        g.SetRandom()
        a = g.GetAdjM()[np.triu_indices(n, 1)]

        T = np.random.random((n,n))
        C = T.transpose() @ T
        C_star = constrained_cov(g.GetDOL(), C, np.eye(n))
        K = np.linalg.inv(C_star)
        b = (abs(K[np.triu_indices(n, 1)]) > 1e-10).astype(int)

        assert((a == b).all())
def test_generate_data():
    np.random.seed(123)
    for _ in range(10):
        n = 10
        m = 10000
        g = Graph(n)
        g.SetRandom()
        X = generate_data(n, m, g, threshold=1e-6)

        a = g.GetAdjM()[np.triu_indices(n, 1)]
        b = (np.abs(np.linalg.inv((X.transpose() @ X) / (m-1))[np.triu_indices(n, 1)]) > 0.1).astype(int)

        threshold = n
        assert(np.sum(a != b) < threshold)

# Larger matrices require very large number of samples for it to converge to the correct graph