def laplace_approx(G, delta, D, as_log_prob=True, diag=False):
    """
    Log of Laplace approximation of G-Wishart normalization constant

    Log of the Laplace approximation of the normalization constant of the G-Wishart
    distribution outlined by Lenkoski and Dobra (2011, doi:10.1198/jcgs.2010.08181)
    """
    from utils.Graph import Graph
    G = Graph(len(G), G)
    G = igraph.Graph.Adjacency(G.GetAdjM().tolist(), mode=1)
    df = delta
    rate = D

    if rate is None:
        # If the rate matrix is the identity matrix (I_p), then the mode of the
        # G-Wishart distribution is (df - 2) * I_p such that the Laplace approximation simplifies.
        p = G.vcount()
        n_e = G.ecount()

        return 0.5 * (
            (p*(df - 1.0) + n_e)*np.log(df - 2.0) - p*(df - 2.0) \
                + p*np.log(4.0 * np.pi) + n_e*np.log(2.0 * np.pi)
        )

    return log_gwish_norm_laplace_cpp(G.__graph_as_capsule(), df, rate, diag)
Ejemplo n.º 2
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