forked from jcatw/ergm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigurations.py
More file actions
180 lines (147 loc) · 4.13 KB
/
configurations.py
File metadata and controls
180 lines (147 loc) · 4.13 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""
Useful configuration functions for defining ERGMs.
"""
import numpy as np
from scipy.misc import comb
import itertools
def n_edges(G):
"""
Compute the number of edges in G.
Args:
G: A 2d numpy array representing an adjacency matrix.
Returns:
A float.
"""
return np.sum(G)
def n_mutual(G):
"""
Compute the number of mutual edges in G.
Args:
G: A 2d numpy array representing an adjacency matrix.
Returns:
A float.
"""
sum = 0.
for i in xrange(G.shape[0]):
for j in xrange(G.shape[1]):
sum += G[i,j] * G[j,i]
return sum
def two_in_stars(G):
"""
Compute the number of two-in-stars in G.
Args:
G: A 2d numpy array representing an adjacency matrix.
Returns:
A float.
"""
sum = 0.
incomings = G.sum(0) #sum over rows
for incoming in incomings:
sum += comb(incoming, 2)
return sum
def two_out_stars(G):
"""
Compute the number of two-out-stars in G.
Args:
G: A 2d numpy array representing an adjacency matrix.
Returns:
A float.
"""
sum = 0.
outgoings = G.sum(1) #sum over cols
for outgoing in outgoings:
sum += comb(outgoing, 2)
return sum
def two_mixed_stars(G):
"""
Compute the number of two-mixed-stars in G.
Args:
G: A 2d numpy array representing an adjacency matrix.
Returns:
A float.
"""
sum = 0.
incomings = G.sum(0)
outgoings = G.sum(1)
for incoming, outgoing in zip(incomings, outgoings):
sum += incoming * outgoing
return sum
def cyclic_triads(G):
"""
Compute the number of cyclic triads in G.
Args:
G: A 2d numpy array representing an adjacency matrix.
Returns:
A float.
"""
return ((G.dot(G)).dot(G)).sum() / 3.
def transitive_triads(G):
n_nodes = G.shape[0]
clust_coef = np.zeros(n_nodes)
for i in xrange(n_nodes):
neighbors = np.where(G[:,i]==1)[0]
n_neighbors = neighbors.shape[0]
if n_neighbors == 0:
clust_coef[i] = 0
else:
transitive_neighbors = 0.
for (edge_from, edge_to) in itertools.combinations(neighbors,2):
if G[edge_from, edge_to] == 1 or G[edge_to,edge_from] == 1:
transitive_neighbors += 1
clust_coef[i] = transitive_neighbors
return clust_coef.sum()
def geo_out(G):
out_degrees = G.sum(1) #sum over cols
max_out = max(out_degrees)
out_dist = np.zeros(int(max_out) + 1)
for d in out_degrees:
out_dist[int(d)] += 1.
geo_out_dist = np.sum([np.exp(-i) * d for i,d in enumerate(out_dist)])
return geo_out_dist
def geo_in(G):
in_degrees = G.sum(0) #sum over rows
max_in = max(in_degrees)
in_dist = np.zeros(int(max_in) + 1)
for d in in_degrees:
in_dist[int(d)] += 1.
geo_in_dist = np.sum([np.exp(-i) * d for i,d in enumerate(in_dist)])
return geo_in_dist
def transitive_triads(G):
sum = 0.
n_nodes = G.shape[0]
for i in xrange(n_nodes):
for j in xrange(n_nodes):
for k in xrange(n_nodes):
sum += G[i,j] * G[j,k] * G[i,k]
return sum
def two_paths(G):
sum = 0.
n_nodes = G.shape[0]
for i in xrange(n_nodes):
for j in xrange(n_nodes):
for k in xrange(n_nodes):
if i != k and j != k:
sum += G[i,k] * G[k,j]
return sum
def alternating_k_triangles(G):
sum = 0.
n_nodes = G.shape[0]
for i in xrange(n_nodes):
for j in xrange(n_nodes):
two_paths = 0
for k in xrange(n_nodes):
if i != k and j != k:
two_paths += G[i,k] * G[k,j]
sum += 2 * G[i,j] * (1 - (0.5 ** two_paths))
return sum
def alternating_k_paths(G):
sum = 0.
n_nodes = G.shape[0]
for i in xrange(n_nodes):
for j in xrange(n_nodes):
two_paths = 0
for k in xrange(n_nodes):
if i != k and j != k:
two_paths += G[i,k] * G[k,j]
sum += 2 * (1 - (0.5 ** two_paths))
return sum