forked from jcatw/ergm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
39 lines (29 loc) · 873 Bytes
/
util.py
File metadata and controls
39 lines (29 loc) · 873 Bytes
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
"""
Utility functions for the ergm package.
"""
import numpy as np
def pam(fs, x):
"""
Apply a sequence of functions to x.
Args:
fs: a sequence of functions.
x: a value to which each f in fs can be applied.
Returns:
A list of the result of applying each f to x.
"""
return [f(x) for f in fs]
def permute(G):
"""
Invert an edge in G uniformly at random and return the
result. Does not mutate G.
Args:
G: A 2d numpy array representing an adjacency matrix.
Returns:
A 2d numpy array representing an adjacency matrix of the permuted graph.
"""
permuter = np.zeros(G.shape)
n_nodes = G.shape[0]
node_from = np.random.randint(n_nodes)
node_to = np.random.randint(n_nodes)
permuter[node_from, node_to] = 1
return np.logical_xor(G, permuter).astype(G.dtype)