forked from maksim2042/PyCon2012
-
Notifications
You must be signed in to change notification settings - Fork 1
/
friedkin.py
93 lines (70 loc) · 2.09 KB
/
friedkin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
# encoding: utf-8
"""
friedkin.py
Created by Maksim Tsvetovat on 2011-08-08.
Copyright (c) 2011 __MyCompanyName__. All rights reserved.
"""
import sys
import os
import networkx as net
import matplotlib.pyplot as plot
import matplotlib.colors as colors
import random as r
class Person(object):
def __init__(self, id):
#Start with a single initial preference
self.id=id
self.i = r.random()
self.a = self.i
#we value initial opinion and subsequent information equally
self.alpha=0.9
def __str__(self):
return(str(self.id))
def step(self):
#loop through the neighbors and aggregate their preferences
neighbors=g[self]
#all nodes in the list of neighbors are equally weighted, including self
w=1/float((len(neighbors)+1))
s=w*self.a
for node in neighbors:
s+=w*node.a
# update my beliefs = initial belief plus sum of all influences
self.a=(1-self.alpha)*self.i + self.alpha*s
class Influencer(Person):
def __init__(self,id):
self.id=id
self.i = r.random()
self.a = 1 ## opinion is strong and immovable
def step(self):
pass
density=0.6
g=net.Graph()
time=100
## create a network of Person objects
for i in range(10):
p=Person(i)
g.add_node(p)
##this will be a simple random graph
for x in g.nodes():
for y in g.nodes():
if r.random()<=density: g.add_edge(x,y)
influencers=4
connections=4
##add the influencers to the network and connect each to 3 other nodes
for i in range(influencers):
inf=Influencer("Inf"+str(i))
for x in range(connections):
g.add_edge(r.choice(g.nodes()), inf)
col=[n.a for n in g.nodes()]
pos=net.spring_layout(g)
net.draw_networkx(g,pos=pos, node_color=col,cmap=plot.cm.Reds)
plot.figure(2)
for i in range(time):
for node in g.nodes():
node.step()
col=[n.a for n in g.nodes()]
print col
plot.plot(col)
plot.figure(i)
net.draw_networkx(g, pos=pos ,node_color=col, cmap=plot.cm.Reds)