In [1]:
%matplotlib inline

import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
In [ ]:
 
In [2]:
territories = {
    "Alaska": 0,
    "Alberta": 0,
    "Central America": 0,
    "Eastern United States": 0,
    "Greenland": 0,
    "Northwest Territory": 0,
    "Ontario": 0,
    "Eastern Canada": 0,
    "Western United States": 0,
    "Argentina": 1,
    "Brazil": 1,
    "Peru": 1,
    "Venezuela": 1,
    "Great Britain": 5,
    "Iceland": 5,
    "Northern Europe": 5,
    "Scandinavia": 5,
    "Southern Europe": 5,
    "Ukraine": 5,
    "Western Europe": 5,
    "Congo": 2,
    "East Africa": 2,
    "Egypt": 2,
    "Madagascar": 2,
    "North Africa": 2,
    "South Africa": 2,
    "Afghanistan": 3,
    "China": 3,
    "India": 3,
    "Irkutsk": 3,
    "Japan": 3,
    "Kamchatka": 3,
    "Middle East": 3,
    "Mongolia": 3,
    "Siam": 3,
    "Siberia": 3,
    "Ural": 3,
    "Yakutsk": 3,
    "Eastern Australia": 4,
    "Indonesia": 4,
    "New Guinea": 4,
    "Western Australia": 4,
}

regions = {
    "North America": 0,
    "South America": 1,
    "Africa": 2,
    "Asia": 3,
    "Australia": 4,
    "Europe": 5,
}
In [ ]:
 
In [4]:
g = nx.Graph()

for t, c_label in territories.items():
    g.add_node(t, region=c_label)
In [5]:
edges = [
    ("Alaska", "Northwest Territory"),
    ("Alaska", "Alberta"),
    ("Alaska", "Kamchatka"),
    ("Northwest Territory", "Alberta"),
    ("Northwest Territory", "Greenland"),
    ("Northwest Territory", "Ontario"),
    ("Greenland", "Iceland"),
    ("Greenland", "Ontario"),
    ("Greenland", "Eastern Canada"),
    ("Eastern Canada", "Eastern United States"),
    ("Eastern Canada", "Ontario"),
    ("Ontario", "Alberta"),
    ("Ontario", "Western United States"),
    ("Alberta", "Western United States"),
    ("Western United States", "Eastern United States"),
    ("Western United States", "Central America"),
    ("Eastern United States", "Central America"),
    ("Central America", "Venezuela"),
    ("Venezuela", "Brazil"),
    ("Venezuela", "Peru"),
    ("Peru", "Argentina"),
    ("Peru", "Brazil"),
    ("Argentina", "Brazil"),
    ("Brazil", "North Africa"),
    ("North Africa", "Egypt"),
    ("North Africa", "Western Europe"),
    ("North Africa", "Southern Europe"),
    ("North Africa", "East Africa"),
    ("North Africa", "Congo"),
    ("Congo", "South Africa"),
    ("Congo", "East Africa"),
    ("South Africa", "Madagascar"),
    ("South Africa", "East Africa"),
    ("Madagascar", "East Africa"),
    ("East Africa", "Egypt"),
    ("East Africa", "Middle East"),
    ("Egypt", "Southern Europe"),
    ("Egypt", "Middle East"),
    ("Middle East", "India"),
    ("Middle East", "Afghanistan"),
    ("Middle East", "Ukraine"),
    ("Middle East", "Southern Europe"),
    ("India", "Afghanistan"),
    ("India", "China"),
    ("India", "Siam"),
    ("Siam", "China"),
    ("Siam", "Indonesia"),
    ("China", "Mongolia"),
    ("China", "Siberia"),
    ("China", "Ural"),
    ("China", "Afghanistan"),
    ("Mongolia", "Japan"),
    ("Mongolia", "Kamchatka"),
    ("Mongolia", "Irkutsk"),
    ("Mongolia", "Siberia"),
    ("Irkutsk", "Kamchatka"),
    ("Irkutsk", "Yakutsk"),
    ("Irkutsk", "Siberia"),
    ("Japan", "Kamchatka"),
    ("Yakutsk", "Siberia"),
    ("Yakutsk", "Kamchatka"),
    ("Siberia", "Ural"),
    ("Ural", "Afghanistan"),
    ("Ural", "Ukraine"),
    ("Ukraine", "Afghanistan"),
    ("Ukraine", "Scandinavia"),
    ("Ukraine", "Northern Europe"),
    ("Ukraine", "Southern Europe"),
    ("Scandinavia", "Northern Europe"),
    ("Scandinavia", "Iceland"),
    ("Scandinavia", "Great Britain"),
    ("Northern Europe", "Southern Europe"),
    ("Northern Europe", "Western Europe"),
    ("Northern Europe", "Great Britain"),
    ("Western Europe", "Great Britain"),
    ("Great Britain", "Iceland"),
    ("Indonesia", "New Guinea"),
    ("Indonesia", "Western Australia"),
    ("New Guinea", "Western Australia"),
    ("New Guinea", "Eastern Australia"),
    ("Eastern Australia", "Western Australia"),
]
In [6]:
for c in set([c for pair in edges for c in pair]):
    if c not in territories:
        print("Error on:", c)
In [7]:
for e1, e2 in edges:
    g.add_edge(e1, e2)
In [8]:
print(len(g.nodes()), len(territories))
42 42
In [ ]:
 
In [9]:
nx.write_edgelist(g, "risk.adj", delimiter="\t")
In [ ]:
 
In [10]:
len(g.edges())
Out[10]:
81
In [ ]:
 
In [11]:
sorted([v for v in g.degree()], key=lambda x: x[1], reverse=True)
Out[11]:
[('Ukraine', 6),
 ('East Africa', 6),
 ('North Africa', 6),
 ('China', 6),
 ('Middle East', 6),
 ('Ontario', 5),
 ('Northern Europe', 5),
 ('Southern Europe', 5),
 ('Afghanistan', 5),
 ('Kamchatka', 5),
 ('Mongolia', 5),
 ('Siberia', 5),
 ('Alberta', 4),
 ('Greenland', 4),
 ('Northwest Territory', 4),
 ('Western United States', 4),
 ('Brazil', 4),
 ('Great Britain', 4),
 ('Scandinavia', 4),
 ('Egypt', 4),
 ('India', 4),
 ('Irkutsk', 4),
 ('Ural', 4),
 ('Alaska', 3),
 ('Central America', 3),
 ('Eastern United States', 3),
 ('Eastern Canada', 3),
 ('Peru', 3),
 ('Venezuela', 3),
 ('Iceland', 3),
 ('Western Europe', 3),
 ('Congo', 3),
 ('South Africa', 3),
 ('Siam', 3),
 ('Yakutsk', 3),
 ('Indonesia', 3),
 ('New Guinea', 3),
 ('Western Australia', 3),
 ('Argentina', 2),
 ('Madagascar', 2),
 ('Japan', 2),
 ('Eastern Australia', 2)]
In [ ]:
 
In [12]:
print("Density:", nx.density(g))
print("Mean Cluster Coef:", np.mean([x[1] for x in nx.clustering(g).items()]))
nx.draw(g)
Density: 0.09407665505226481
Mean Cluster Coef: 0.4920634920634921
In [13]:
A = nx.nx_agraph.to_agraph(g)
A.layout(prog='fdp')
A.draw('risk_graph.png')

graph

In [ ]:
 
In [14]:
nx.write_graphml(g, "risk.graphml")
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [15]:
ax = plt.gca()
ax.set_xlim((-1.5,1.5))

target = "Argentina"
degree = 1.5

subg = nx.ego_graph(g, target, degree)
subg.remove_node(target)

if ( degree == 1 ):
    for e in dict(subg.edges):
        if ( target not in e ):
            subg.remove_edge(e[0], e[1])
nx.draw_networkx(subg, with_labels=True, ax=ax)

ax.axis('off')

plt.show() 

print("Clustering Coefficient:", nx.clustering(g, target))
print("1.5-Degree Ego Density:", nx.density(subg))
print("Global Coefficient of Subg:", nx.transitivity(subg))
Clustering Coefficient: 1.0
1.5-Degree Ego Density: 1.0
Global Coefficient of Subg: 0
In [ ]:
 
In [ ]:
for t in territories:

    subg = nx.ego_graph(g, t)
    subg.remove_node(t)
    
    cluster_coef = nx.clustering(g, t)
    ego_dens = nx.density(subg)

    print(t, cluster_coef)
    if ( cluster_coef != ego_dens ):
        print("Fail", t, cluster_coef, ego_dens)
In [ ]:
 
In [ ]:
local_cluster_coeffs = []
for n in g.nodes():
    local_cluster_coeffs.append(nx.clustering(g, n))
print("Mean Local Clustering Coeff:", np.mean(local_cluster_coeffs))
print("Mean Local Clustering Coeff':", nx.average_clustering(g))
print("Global Clustering Coeff':", nx.transitivity(g))
In [ ]:
num_triangles = np.sum([x[1] for x in nx.triangles(g).items()])
print("Triangles:", num_triangles/3)
number_of_triads = num_triangles/nx.transitivity(g)

print("Triads:", number_of_triads)
In [ ]:
 
In [ ]:
nx.node_connectivity(subg)
In [ ]:
 
In [ ]:
for reg, reg_idx in regions.items():
    reg_nodes = [x[0] for x in nx.get_node_attributes(g, "region").items() if x[1] == reg_idx]
    subg = g.subgraph(reg_nodes)
    
    print(reg, nx.average_clustering(subg))
In [ ]:
 
In [16]:
country_bc = nx.degree_centrality(g)
nx.set_node_attributes(g, country_bc, "d_cent")
[(x, country_bc[x]) for x in sorted(country_bc, key=country_bc.get, reverse=True)]
Out[16]:
[('Ukraine', 0.14634146341463417),
 ('East Africa', 0.14634146341463417),
 ('North Africa', 0.14634146341463417),
 ('China', 0.14634146341463417),
 ('Middle East', 0.14634146341463417),
 ('Ontario', 0.12195121951219512),
 ('Northern Europe', 0.12195121951219512),
 ('Southern Europe', 0.12195121951219512),
 ('Afghanistan', 0.12195121951219512),
 ('Kamchatka', 0.12195121951219512),
 ('Mongolia', 0.12195121951219512),
 ('Siberia', 0.12195121951219512),
 ('Alberta', 0.0975609756097561),
 ('Greenland', 0.0975609756097561),
 ('Northwest Territory', 0.0975609756097561),
 ('Western United States', 0.0975609756097561),
 ('Brazil', 0.0975609756097561),
 ('Great Britain', 0.0975609756097561),
 ('Scandinavia', 0.0975609756097561),
 ('Egypt', 0.0975609756097561),
 ('India', 0.0975609756097561),
 ('Irkutsk', 0.0975609756097561),
 ('Ural', 0.0975609756097561),
 ('Alaska', 0.07317073170731708),
 ('Central America', 0.07317073170731708),
 ('Eastern United States', 0.07317073170731708),
 ('Eastern Canada', 0.07317073170731708),
 ('Peru', 0.07317073170731708),
 ('Venezuela', 0.07317073170731708),
 ('Iceland', 0.07317073170731708),
 ('Western Europe', 0.07317073170731708),
 ('Congo', 0.07317073170731708),
 ('South Africa', 0.07317073170731708),
 ('Siam', 0.07317073170731708),
 ('Yakutsk', 0.07317073170731708),
 ('Indonesia', 0.07317073170731708),
 ('New Guinea', 0.07317073170731708),
 ('Western Australia', 0.07317073170731708),
 ('Argentina', 0.04878048780487805),
 ('Madagascar', 0.04878048780487805),
 ('Japan', 0.04878048780487805),
 ('Eastern Australia', 0.04878048780487805)]
In [17]:
dc_list = [x[1] for x in country_bc.items()]
m = np.max(dc_list)
np.sum([m-x for x in dc_list])
Out[17]:
2.1951219512195124
In [ ]:
 
In [18]:
country_bc = nx.closeness_centrality(g)
nx.set_node_attributes(g, country_bc, "c_cent")
[(x, country_bc[x]) for x in sorted(country_bc, key=country_bc.get, reverse=True)]
Out[18]:
[('Ukraine', 0.3203125),
 ('Middle East', 0.3082706766917293),
 ('Afghanistan', 0.3037037037037037),
 ('Southern Europe', 0.29927007299270075),
 ('Ural', 0.2949640287769784),
 ('China', 0.29285714285714287),
 ('India', 0.2907801418439716),
 ('North Africa', 0.2867132867132867),
 ('Northern Europe', 0.2827586206896552),
 ('Scandinavia', 0.2827586206896552),
 ('East Africa', 0.2808219178082192),
 ('Egypt', 0.2789115646258503),
 ('Siberia', 0.271523178807947),
 ('Mongolia', 0.2679738562091503),
 ('Great Britain', 0.2662337662337662),
 ('Western Europe', 0.2662337662337662),
 ('Iceland', 0.26282051282051283),
 ('Siam', 0.2578616352201258),
 ('Brazil', 0.2546583850931677),
 ('Kamchatka', 0.25308641975308643),
 ('Greenland', 0.24550898203592814),
 ('Congo', 0.24550898203592814),
 ('Irkutsk', 0.24550898203592814),
 ('Alaska', 0.24404761904761904),
 ('Yakutsk', 0.24404761904761904),
 ('Northwest Territory', 0.23976608187134502),
 ('Alberta', 0.23699421965317918),
 ('Venezuela', 0.2342857142857143),
 ('Ontario', 0.23163841807909605),
 ('Western United States', 0.23163841807909605),
 ('Japan', 0.22905027932960895),
 ('Central America', 0.22282608695652173),
 ('South Africa', 0.22282608695652173),
 ('Madagascar', 0.22162162162162163),
 ('Peru', 0.21808510638297873),
 ('Eastern Canada', 0.21354166666666666),
 ('Eastern United States', 0.21243523316062177),
 ('Indonesia', 0.21243523316062177),
 ('Argentina', 0.205),
 ('New Guinea', 0.1782608695652174),
 ('Western Australia', 0.1782608695652174),
 ('Eastern Australia', 0.1524163568773234)]
In [ ]:
 
In [19]:
country_bc = nx.betweenness_centrality(g)
nx.set_node_attributes(g, country_bc, "b_cent")
[(x, country_bc[x]) for x in sorted(country_bc, key=country_bc.get, reverse=True)]
Out[19]:
[('North Africa', 0.21333372927885122),
 ('Middle East', 0.20483647450110862),
 ('China', 0.19602373913349527),
 ('Ukraine', 0.18158571780522995),
 ('Siam', 0.18048780487804877),
 ('Brazil', 0.15754302607961146),
 ('Kamchatka', 0.14193195016365748),
 ('Indonesia', 0.13902439024390245),
 ('India', 0.13616799528384896),
 ('Mongolia', 0.13543342836025765),
 ('Alaska', 0.13152544609861685),
 ('East Africa', 0.12821010628937457),
 ('Iceland', 0.11315911730545876),
 ('Greenland', 0.10636759581881539),
 ('Venezuela', 0.09547724633090487),
 ('Scandinavia', 0.08651955090979484),
 ('Central America', 0.0864325308837504),
 ('Ural', 0.08434114665821982),
 ('Southern Europe', 0.08397458909654035),
 ('Western United States', 0.08149350649350648),
 ('Alberta', 0.08056435434484213),
 ('Afghanistan', 0.07609888079400276),
 ('Siberia', 0.07002146904585929),
 ('Great Britain', 0.04668989547038328),
 ('Western Europe', 0.04585656213704994),
 ('Northwest Territory', 0.04436991869918697),
 ('Northern Europe', 0.04211720867208674),
 ('Ontario', 0.03348432055749128),
 ('Egypt', 0.02573623869355576),
 ('New Guinea', 0.02378048780487805),
 ('Western Australia', 0.02378048780487805),
 ('Eastern Canada', 0.01705284552845528),
 ('Eastern United States', 0.014186991869918696),
 ('Congo', 0.00955574912891986),
 ('Irkutsk', 0.007236300285080773),
 ('Yakutsk', 0.006829796220040122),
 ('Peru', 0.0066962305986696235),
 ('South Africa', 0.0006097560975609756),
 ('Argentina', 0.0),
 ('Madagascar', 0.0),
 ('Japan', 0.0),
 ('Eastern Australia', 0.0)]
In [20]:
nx.write_graphml(g, "risk.graphml")
In [ ]:
 
In [ ]: