# Simulations¶

graspy.simulations.er_np(n, p, directed=False, loops=False, wt=1, wtargs=None, dc=None, dc_kws={})[source]

Samples a Erdos Renyi (n, p) graph with specified edge probability.

Erdos Renyi (n, p) graph is a simple graph with n vertices and a probability p of edges being connected.

Parameters: n: int Number of vertices p: float Probability of an edge existing between two vertices, between 0 and 1. directed: boolean, optional (default=False) If False, output adjacency matrix will be symmetric. Otherwise, output adjacency matrix will be asymmetric. loops: boolean, optional (default=False) If False, no edges will be sampled in the diagonal. Otherwise, edges are sampled in the diagonal. wt: object, optional (default=1) Weight function for each of the edges, taking only a size argument. This weight function will be randomly assigned for selected edges. If 1, graph produced is binary. wtargs: dictionary, optional (default=None) Optional arguments for parameters that can be passed to weight function wt. dc: function or array-like, shape (n_vertices) dc is used to generate a degree-corrected Erdos Renyi Model in which each node in the graph has a parameter to specify its expected degree relative to other nodes. function: should generate a non-negative number to be used as a degree correction to create a heterogenous degree distribution. A weight will be generated for each vertex, normalized so that the sum of weights is 1. array-like of scalars, shape (n_vertices): The weights should sum to 1; otherwise, they will be normalized and a warning will be thrown. The scalar associated with each vertex is the node's relative expected degree. dc_kws: dictionary Ignored if dc is none or array of scalar. If dc is a function, dc_kws corresponds to its named arguments. If not specified, in either case all functions will assume their default parameters. A : ndarray, shape (n, n) Sampled adjacency matrix

Examples

>>> np.random.seed(1)
>>> n = 4
>>> p = 0.25


To sample a binary Erdos Renyi (n, p) graph:

>>> er_np(n, p)
array([[0., 0., 1., 0.],
[0., 0., 1., 0.],
[1., 1., 0., 0.],
[0., 0., 0., 0.]])


To sample a weighted Erdos Renyi (n, p) graph with Uniform(0, 1) distribution:

>>> wt = np.random.uniform
>>> wtargs = dict(low=0, high=1)
>>> er_np(n, p, wt=wt, wtargs=wtargs)
array([[0.        , 0.        , 0.95788953, 0.53316528],
[0.        , 0.        , 0.        , 0.        ],
[0.95788953, 0.        , 0.        , 0.31551563],
[0.53316528, 0.        , 0.31551563, 0.        ]])

graspy.simulations.er_nm(n, m, directed=False, loops=False, wt=1, wtargs=None)[source]

Samples an Erdos Renyi (n, m) graph with specified number of edges.

Erdos Renyi (n, m) graph is a simple graph with n vertices and exactly m number of total edges.

Parameters: n: int Number of vertices m: int Number of edges, a value between 1 and $$n^2$$. directed: boolean, optional (default=False) If False, output adjacency matrix will be symmetric. Otherwise, output adjacency matrix will be asymmetric. loops: boolean, optional (default=False) If False, no edges will be sampled in the diagonal. Otherwise, edges are sampled in the diagonal. wt: object, optional (default=1) Weight function for each of the edges, taking only a size argument. This weight function will be randomly assigned for selected edges. If 1, graph produced is binary. wtargs: dictionary, optional (default=None) Optional arguments for parameters that can be passed to weight function wt. A: ndarray, shape (n, n) Sampled adjacency matrix

Examples

>>> np.random.seed(1)
>>> n = 4
>>> m = 4


To sample a binary Erdos Renyi (n, m) graph:

>>> er_nm(n, m)
array([[0., 1., 1., 1.],
[1., 0., 0., 1.],
[1., 0., 0., 0.],
[1., 1., 0., 0.]])


To sample a weighted Erdos Renyi (n, m) graph with Uniform(0, 1) distribution:

>>> wt = np.random.uniform
>>> wtargs = dict(low=0, high=1)
>>> er_nm(n, m, wt=wt, wtargs=wtargs)
array([[0.        , 0.66974604, 0.        , 0.38791074],
[0.66974604, 0.        , 0.        , 0.39658073],
[0.        , 0.        , 0.        , 0.93553907],
[0.38791074, 0.39658073, 0.93553907, 0.        ]])

graspy.simulations.sbm(n, p, directed=False, loops=False, wt=1, wtargs=None, dc=None, dc_kws={})[source]

Samples a graph from the stochastic block model (SBM).

SBM produces a graph with specified communities, in which each community can have different sizes and edge probabilities.

Parameters: n: list of int, shape (n_communities) Number of vertices in each community. Communities are assigned n, n, ... p: array-like, shape (n_communities, n_communities) Probability of an edge between each of the communities, where p[i, j] indicates the probability of a connection between edges in communities [i, j]. 0 < p[i, j] < 1 for all i, j. directed: boolean, optional (default=False) If False, output adjacency matrix will be symmetric. Otherwise, output adjacency matrix will be asymmetric. loops: boolean, optional (default=False) If False, no edges will be sampled in the diagonal. Otherwise, edges are sampled in the diagonal. wt: object or array-like, shape (n_communities, n_communities) if Wt is an object, a weight function to use globally over the sbm for assigning weights. 1 indicates to produce a binary graph. If Wt is an array-like, a weight function for each of the edge communities. Wt[i, j] corresponds to the weight function between communities i and j. If the entry is a function, should accept an argument for size. An entry of Wt[i, j] = 1 will produce a binary subgraph over the i, j community. wtargs: dictionary or array-like, shape (n_communities, n_communities) if Wt is an object, Wtargs corresponds to the trailing arguments to pass to the weight function. If Wt is an array-like, Wtargs[i, j] corresponds to trailing arguments to pass to Wt[i, j]. dc: function or array-like, shape (n_vertices) or (n_communities), optional dc is used to generate a degree-corrected stochastic block model  in which each node in the graph has a parameter to specify its expected degree relative to other nodes within its community. function: should generate a non-negative number to be used as a degree correction to create a heterogenous degree distribution. A weight will be generated for each vertex, normalized so that the sum of weights in each block is 1. array-like of functions, shape (n_communities): Each function will generate the degree distribution for its respective community. array-like of scalars, shape (n_vertices): The weights in each block should sum to 1; otherwise, they will be normalized and a warning will be thrown. The scalar associated with each vertex is the node's relative expected degree within its community. dc_kws: dictionary or array-like, shape (n_communities), optional Ignored if dc is none or array of scalar. If dc is a function, dc_kws corresponds to its named arguments. If dc is an array-like of functions, dc_kws should be an array-like, shape (n_communities), of dictionary. Each dictionary is the named arguments for the corresponding function for that community. If not specified, in either case all functions will assume their default parameters. A: ndarray, shape (sum(n), sum(n)) Sampled adjacency matrix

References

  Tai Qin and Karl Rohe. "Regularized spectral clustering under the Degree-Corrected Stochastic Blockmodel," Advances in Neural Information Processing Systems 26, 2013

Examples

>>> np.random.seed(1)
>>> n = [3, 3]
>>> p = [[0.5, 0.1], [0.1, 0.5]]


To sample a binary 2-block SBM graph:

>>> sbm(n, p)
array([[0., 0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0., 1.],
[1., 1., 0., 0., 0., 0.],
[0., 0., 0., 0., 1., 0.],
[0., 0., 0., 1., 0., 0.],
[0., 1., 0., 0., 0., 0.]])


To sample a weighted 2-block SBM graph with Poisson(2) distribution:

>>> wt = np.random.poisson
>>> wtargs = dict(lam=2)
>>> sbm(n, p, wt=wt, wtargs=wtargs)
array([[0., 4., 0., 1., 0., 0.],
[4., 0., 0., 0., 0., 2.],
[0., 0., 0., 0., 0., 0.],
[1., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 2., 0., 0., 0., 0.]])

graspy.simulations.rdpg(X, Y=None, rescale=True, directed=False, loops=True, wt=1, wtargs=None)[source]

Samples a random graph based on the latent positions in X (and optionally in Y)

If only X $$\in\mathbb{R}^{n\times d}$$ is given, the P matrix is calculated as $$P = XX^T$$. If X, Y $$\in\mathbb{R}^{n\times d}$$ is given, then $$P = XY^T$$. These operations correspond to the dot products between a set of latent positions, so each row in X or Y represents the latent positions in $$\mathbb{R}^{d}$$ for a single vertex in the random graph Note that this function may also rescale or clip the resulting P matrix to get probabilities between 0 and 1, or remove loops. A binary random graph is then sampled from the P matrix described by X (and possibly Y).

Parameters: X: np.ndarray, shape (n_vertices, n_dimensions) latent position from which to generate a P matrix if Y is given, interpreted as the left latent position Y: np.ndarray, shape (n_vertices, n_dimensions) or None, optional right latent position from which to generate a P matrix rescale: boolean, optional (default=True) when rescale is True, will subtract the minimum value in P (if it is below 0) and divide by the maximum (if it is above 1) to ensure that P has entries between 0 and 1. If False, elements of P outside of [0, 1] will be clipped directed: boolean, optional (default=False) If False, output adjacency matrix will be symmetric. Otherwise, output adjacency matrix will be asymmetric. loops: boolean, optional (default=True) If False, no edges will be sampled in the diagonal. Diagonal elements in P matrix are removed prior to rescaling (see above) which may affect behavior. Otherwise, edges are sampled in the diagonal. wt: object, optional (default=1) Weight function for each of the edges, taking only a size argument. This weight function will be randomly assigned for selected edges. If 1, graph produced is binary. wtargs: dictionary, optional (default=None) Optional arguments for parameters that can be passed to weight function wt. A: ndarray (n_vertices, n_vertices) A matrix representing the probabilities of connections between vertices in a random graph based on their latent positions

References

  Sussman, D.L., Tang, M., Fishkind, D.E., Priebe, C.E. "A Consistent Adjacency Spectral Embedding for Stochastic Blockmodel Graphs," Journal of the American Statistical Association, Vol. 107(499), 2012

Examples

>>> np.random.seed(1)


Generate random latent positions using 2-dimensional Dirichlet distribution.

>>> X = np.random.dirichlet([1, 1], size=5)


Sample a binary RDPG using sampled latent positions.

>>> rdpg(X, loops=False)
array([[0., 1., 0., 0., 1.],
[1., 0., 0., 1., 1.],
[0., 0., 0., 1., 1.],
[0., 1., 1., 0., 0.],
[1., 1., 1., 0., 0.]])


Sample a weighted RDPG with Poisson(2) weight distribution

>>> wt = np.random.poisson
>>> wtargs = dict(lam=2)
>>> rdpg(X, loops=False, wt=wt, wtargs=wtargs)
array([[0., 4., 0., 2., 0.],
[1., 0., 0., 0., 0.],
[0., 0., 0., 0., 2.],
[1., 0., 0., 0., 1.],
[0., 2., 2., 0., 0.]])