Calculating assortativity coefficient in igraph


/ Published in: Python
Save to your folder(s)

Also works with weighted degree or any other property you might think of.


Copy this code and paste it in your HTML
  1. def assortativity(graph, degrees=None):
  2. if degrees is None: degrees = graph.degree()
  3. degrees_sq = [deg**2 for deg in degrees]
  4.  
  5. m = float(graph.ecount())
  6. num1, num2, den1 = 0, 0, 0
  7. for source, target in graph.get_edgelist():
  8. num1 += degrees[source] * degrees[target]
  9. num2 += degrees[source] + degrees[target]
  10. den1 += degrees_sq[source] + degrees_sq[target]
  11.  
  12. num1 /= m
  13. den1 /= 2*m
  14. num2 = (num2 / (2*m)) ** 2
  15.  
  16. return (num1 - num2) / (den1 - num2)

Report this snippet


Comments


You need to login to view comments.