Ejemplo n.º 1
0
    def erdos_renyi_modularity(self):
        """

        Erdos-Renyi modularity is a variation of the Newman-Girvan one.
        It assumes that vertices in a network are connected randomly with a constant probability :math:`p`.

        .. math:: Q(S) = \\frac{1}{m}\\sum_{c \\in S} (m_S − \\frac{mn_S(n_S −1)}{n(n−1)})

        where :math:`m` is the number of graph edges, :math:`m_S` is the number of algorithms edges, :math:`l_S` is the number of edges from nodes in S to nodes outside S.


        :return: the Erdos-Renyi modularity score

        :Example:

        >>> from cdlib.algorithms import louvain
        >>> g = nx.karate_club_graph()
        >>> communities = louvain(g)
        >>> mod = communities.erdos_renyi_modularity()

        :References:

        Erdos, P., & Renyi, A. (1959). **On random graphs I.** Publ. Math. Debrecen, 6, 290-297.
        """
        if self.__check_graph():
            return evaluation.erdos_renyi_modularity(self.graph, self)
        else:
            raise ValueError("Graph instance not specified")
Ejemplo n.º 2
0
    def test_modularity(self):
        g = nx.karate_club_graph()
        communities = louvain(g)

        mod = evaluation.newman_girvan_modularity(g, communities)
        self.assertLessEqual(mod.score, 1)
        self.assertGreaterEqual(mod.score, -0.5)

        mod = evaluation.erdos_renyi_modularity(g, communities)
        self.assertLessEqual(mod.score, 1)
        self.assertGreaterEqual(mod.score, -0.5)

        mod = evaluation.modularity_density(g, communities)
        self.assertIsInstance(mod.score, float)

        mod = evaluation.z_modularity(g, communities)
        self.assertLessEqual(mod.score, np.sqrt(g.number_of_nodes()))
        self.assertGreaterEqual(mod.score, -0.5)