/ Published in: Python
Also works with weighted degree or any other property you might think of.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
def assortativity(graph, degrees=None): if degrees is None: degrees = graph.degree() degrees_sq = [deg**2 for deg in degrees] m = float(graph.ecount()) num1, num2, den1 = 0, 0, 0 for source, target in graph.get_edgelist(): num1 += degrees[source] * degrees[target] num2 += degrees[source] + degrees[target] den1 += degrees_sq[source] + degrees_sq[target] num1 /= m den1 /= 2*m num2 = (num2 / (2*m)) ** 2 return (num1 - num2) / (den1 - num2)