Skip to content

stochastic_blockmodel_graph

stochastic_blockmodel_graph(
    block_sizes: Union[Sequence[int], array],
    edge_probs: Union[Sequence[Sequence[float]], array],
    directed: bool = False,
) -> array

Returns the edges of a random graph from the stochastic block model: the nodes are split into blocks, and each possible edge (without self-loops) between a node of block a and a node of block b is present independently with probability edge_probs[a][b].

Randomness comes from mx.random, so results are reproducible with mx.random.seed.

Parameters:

Name Type Description Default
block_sizes Union[Sequence[int], array]

Number of nodes of each block. The nodes of block 0 come first, then those of block 1, and so on.

required
edge_probs Union[Sequence[Sequence[float]], array]

Square matrix of the edge probabilities between blocks. It must be symmetric if directed=False.

required
directed bool

If True, each ordered pair (i, j) is sampled independently. If False, each unordered pair is sampled once and returned in both directions. Default: False

False

Returns:

Type Description
array

The sorted int32 edge_index of the graph.

Example:

block_sizes = [2, 3]
edge_probs = [[0.9, 0.1], [0.1, 0.8]]
stochastic_blockmodel_graph(block_sizes, edge_probs)
# e.g. mx.array([[0, 1, 2, 2, 3, 3, 4, 4], [1, 0, 3, 4, 2, 4, 2, 3]])
Source code in mlx_graphs/utils/generators.py
def stochastic_blockmodel_graph(
    block_sizes: Union[Sequence[int], mx.array],
    edge_probs: Union[Sequence[Sequence[float]], mx.array],
    directed: bool = False,
) -> mx.array:
    """
    Returns the edges of a random graph from the stochastic block model: the
    nodes are split into blocks, and each possible edge (without self-loops)
    between a node of block ``a`` and a node of block ``b`` is present
    independently with probability ``edge_probs[a][b]``.

    Randomness comes from ``mx.random``, so results are reproducible with
    ``mx.random.seed``.

    Args:
        block_sizes: Number of nodes of each block. The nodes of block 0 come
            first, then those of block 1, and so on.
        edge_probs: Square matrix of the edge probabilities between blocks.
            It must be symmetric if ``directed=False``.
        directed: If ``True``, each ordered pair ``(i, j)`` is sampled
            independently. If ``False``, each unordered pair is sampled once
            and returned in both directions. Default: ``False``

    Returns:
        The sorted int32 ``edge_index`` of the graph.

    Example:

    .. code-block:: python

        block_sizes = [2, 3]
        edge_probs = [[0.9, 0.1], [0.1, 0.8]]
        stochastic_blockmodel_graph(block_sizes, edge_probs)
        # e.g. mx.array([[0, 1, 2, 2, 3, 3, 4, 4], [1, 0, 3, 4, 2, 4, 2, 3]])
    """
    size = np.asarray(
        _to_numpy(block_sizes) if isinstance(block_sizes, mx.array) else block_sizes,
        dtype=np.int64,
    )
    prob = np.asarray(
        _to_numpy(edge_probs) if isinstance(edge_probs, mx.array) else edge_probs,
        dtype=np.float64,
    )
    if size.ndim != 1:
        raise ValueError("`block_sizes` must be one-dimensional")
    if prob.ndim != 2 or prob.shape[0] != prob.shape[1]:
        raise ValueError("`edge_probs` must be a square matrix")
    if size.size != prob.shape[0]:
        raise ValueError("`block_sizes` and `edge_probs` must have the same size")
    if not directed and not np.allclose(prob, prob.T):
        raise ValueError("`edge_probs` must be symmetric for an undirected graph")

    node_block = np.repeat(np.arange(size.size), size)
    num_nodes = node_block.size
    if directed:
        row, col = _ordered_pairs(num_nodes)
    else:
        row, col = np.triu_indices(num_nodes, k=1)
    uniform = _to_numpy(mx.random.uniform(shape=(row.size,)))
    keep = uniform < prob[node_block[row], node_block[col]]
    edges = np.stack([row[keep], col[keep]])
    if directed:
        return mx.array(edges.astype(np.int32))
    return _undirected(edges, num_nodes)

from mlx_graphs.utils import stochastic_blockmodel_graph · mlx_graphs/utils/generators.py