This model of social learning was independently introduced in Clifford and Sudbury (1973), Holley and Liggett (1975). The setting and dynamic process are the following:
It was shown that agents' states reach a consensus over any connected graph $G$ (Coalescing Random Walks).
In [ ]:
import numpy as np
import scipy as sp
import networkx as nx
import matplotlib.pyplot as plt
from numpy.linalg import eig
from scipy.integrate import odeint
%matplotlib inline
Implement the Voter Model and check how the number of agents' states changes with time. Run it for Karate club and Political Blogs networks.
In [ ]:
G = nx.karate_club_graph()
# Continue your code here
#..
opinions = np.random.uniform( size = G.number_of_nodes( ) )
In [ ]:
opinions = np.random.uniform( size = G.number_of_nodes( ) )
In [ ]:
def voter( G, O, niter = 10000 ) :
tick = np.full( G.number_of_nodes(), np.inf, np.float )
kiter = 0.0
while kiter < niter :
## pick a random node
v = np.random.choice( G.nodes( ) )
## pick a random neighbour
w = np.random.choice( G[ v ].keys( ), size = 1 )
if O[ v ] != O[ w ] :
O[ v ] = O[ w ]
if not np.isfinite( tick[ v ] ) :
tick[ v ] = kiter
kiter += 1
return O, tick
In [ ]:
voter( G, opinions )
In [ ]:
# Some preprocessing for polblogs
A = nx.adj_matrix(nx.read_gml('./data/polblogs.gml'))
G = nx.Graph( A )
for n, d in G.degree_iter( ):
if d == 0:
G.remove_node( n )
mapping = { }
i = 0
for n in G.nodes_iter():
mapping[ n ] = i
i += 1
G = nx.relabel_nodes( G, mapping )
Try to write a code that identifies the path of opinion adoption starting from a particular node
In [ ]:
# Continue your code here
#..
def voter( G, opinions ) :
kiter = 0.0
while kiter < niter :
## pick a random node
v = np.random.choice( G.nodes( ) )
## pick a random neighbour
w = np.random.choice( G[ v ].keys( ), size = 1 )
if O[ v ] != O[ w ] :
O[ v ] = O[ w ]
if not np.isfinite( tick[ v ] ) :
tick[ v ] = kiter
kiter += 1