def Q_modularity(network: Network, cluster_mapping: Dict) -> float: """Computes the Q-modularity of a network for a given cluster mapping """ A = network.adjacency_matrix() m = network.number_of_edges() q = 0.0 for v in network.nodes.uids: for w in network.nodes.uids: if cluster_mapping[v] == cluster_mapping[w]: q += A[network.nodes.index[v], network.nodes.index[w]] - \ network.degrees()[v] * network.degrees()[w]/(2*m) return q/(2*m)
def degree_assortativity(network: Network, mode: str = 'total', weight: Weight = None) -> float: """Calculates the degree assortativity coefficient of a network. Parameters ---------- network : Network The network in which to calculate the Molloy-Reed fraction """ A = network.adjacency_matrix(weight=weight) m = np.sum(A) d = network.degrees(weight) if network.directed and mode == 'in': d = network.indegrees(weight) elif network.directed and mode == 'out': d = network.outdegrees(weight) elif network.directed and mode == 'total': d = network.degrees(weight) elif not network.directed: m = m / 2. idx = network.nodes.index cov: float = 0. var: float = 0. for i in network.nodes.keys(): for j in network.nodes.keys(): cov += (A[idx[i], idx[j]] - (d[i] * d[j]) / (2 * m)) * d[i] * d[j] if i != j: var -= (d[i] * d[j]) / (2 * m) * d[i] * d[j] else: var += (d[i] - (d[i] * d[j]) / (2 * m)) * d[i] * d[j] return cov / var