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

the number of vertices

p: float

the probability of an edge existing between two vertices, between 0 and 1.

directed: boolean optional, default = False

Whether to create a directed graph or not.

loops: boolean optional, default = False

Whether to include self-loops or not.

wt: object

a 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 arguments for parameters that can be passed to weight function wt.

Returns:
A : array-like, shape (n, n)

Sampled adjacency matrix

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

Samples a weighted 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

Whether to create a directed graph or not.

loops: boolean optional, default = False

Whether to include self-loops or not.

wt: object

a 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 arguments for parameters that can be passed to weight function wt.

Returns:
A: array-like, 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)[source]
n: list of int, shape (n_communities)
the number of vertices in each community. Communities are assigned n[0], n[1], ...
p: array-like, shape (n_communities, n_communities)
the 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
whether or not the graph will be directed.
loops: boolean
whether to allow self-loops for vertices.
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].
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 is given, the P matrix is calculated as \(P = XX^T\) If X and Y 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 \(\R^{num_columns}\) 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 (2 dimensions, same shape as Y if given)

latent position from which to generate a P matrix if Y is given, interpreted as the left latent position

Y: np.ndarray (2 dimensions, same shape as X)

right latent position from which to generate a P matrix

rescale: boolean (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 (default False)

Whether to force symmetry upon the resulting graph by only sampling from the upper triangle of P and then reflecting the sampled values accross the diagonal

loops: boolean (default True)

whether to allow elements on the diagonal (corresponding to self connections in a graph) in the returned P matrix. If loops is False, these elements are removed prior to rescaling (see above) which may affect behavior

wt: object

a 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 arguments for parameters that can be passed to weight function wt.

Returns:
P: np.ndarray (X.shape[0], X.shape[0])

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