Simulate node attribute assortativity given an edgelist

Viewed 21

I am interested in simulating a small world network where there is assortativity for a given node attribute. I want to first generate the small world network, G = (V,E), and then generate attributes for the nodes such that the assortativity is x.

So the problem can be stated as: Given some edgelist, assign nodes to have a value in [0,1] such that the assortativity of this value is some x.

Here is some Python code:

import networkx as nx
import random

def make_assort(G, col, assort):
  # TODO 
  # Returns a dict of attributes for graph G that gives
  # an assortaivity coefficent of `assort` for `col`
  vals =  nx.get_node_attributes(G, col)
  return vals

# Generate graph
G = nx.watts_strogatz_graph(n = 100, k = 4, p = 0.5)

# Generate initial preferences
random.seed(10)
prefs = {n:random.uniform(0,1) for n in range(len(G))}
nx.set_node_attributes(G, prefs, "pref")
print("Current assortativity", nx.numeric_assortativity_coefficient(G, "pref"))

# Generate new preferences with assortivity = alpha
new_vals = make_assort(G, "pref", assort=0.5)
nx.set_node_attributes(G, new_vals, "pref")
print("New assortativity", nx.numeric_assortativity_coefficient(G, "pref"))
0 Answers
Related