Simulations

graspy.simulations.er_np(n, p, directed=False, loops=False, wt=1, wtargs=None)[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.

Returns:
A : ndarray, shape (n, n)

Sampled adjacency matrix

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.

Returns:
A: ndarray, shape (n, n)

Sampled adjacency matrix

Examples

>>> n = 100
>>> m = 20
>>> wt = np.random.uniform
>>> wtargs = dict(low=1, high=2)
>>> A = weighted_er_nm(n, m, wt=wt, wtargs=wtargs)
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[0], n[1], ...

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 [1] 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.

Returns:
A: ndarray, shape (sum(n), sum(n))

Sampled adjacency matrix

References

[1]Tai Qin and Karl Rohe. "Regularized spectral clustering under the Degree-Corrected Stochastic Blockmodel," Advances in Neural Information Processing Systems 26, 2013
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.

Returns:
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

[1]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