Stochastic Block Model (SBM)

[1]:
import graspologic

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm
/home/runner/work/graspologic/graspologic/graspologic/models/edge_swaps.py:215: NumbaDeprecationWarning: The keyword argument 'nopython=False' was supplied. From Numba 0.59.0 the default is being changed to True and use of 'nopython=False' will raise a warning as the argument will have no effect. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
  _edge_swap_numba = nb.jit(_edge_swap, nopython=False)

Unlike Erdos-Renyi (ER) models, a Stochastic Block Model (SBM) produces graphs containing communities: disjoint subgraphs characterized by differing edge probabilities for vertices within and between communities (1).

SBM is parametrized by the number of vertices in each community \(n\), and a block probability matrix \(P \in \mathbb{R}^{n x n}\) where each element specifies the probability of an edge in a particular block. One can think of SBM as a collection of ER graphs where each block corresponds to an ER graph.

Below, we sample a two-block SBM (undirected, no self-loops) with following parameters:

\begin{align*} n &= [50, 50]\\ P &= \begin{bmatrix} 0.5 & 0.2\\ 0.2 & 0.05 \end{bmatrix} \end{align*}

The diagonals correspond to probability of an edge within blocks and the off-diagonals correspond to probability of an edge between blocks.

[2]:
from graspologic.simulations import sbm

n = [50, 50]
p = [[0.5, 0.2],
     [0.2, 0.05]]

np.random.seed(1)
G = sbm(n=n, p=p)

Visualize the graph using heatmap

[3]:
from graspologic.plot import heatmap

_ = heatmap(G, title ='SBM Simulation')
../../_images/tutorials_simulations_sbm_5_0.png

Weighted SBM Graphs

Similar to ER simulations, sbm() functions provide ways to sample weights for all edges that were sampled via a probability distribution function. In order to sample with weights, you can either:

  1. Provide a single probability distribution function with corresponding keyword arguments for the distribution function. All weights will be sampled using the same function.

  2. Provide a probability distribution function with corresponding keyword arguments for each block.

Below we sample a SBM (undirected, no self-loops) with the following parameters:

\begin{align*} n &= [50, 50]\\ P &= \begin{bmatrix}0.5 & 0.2\\ 0.2 & 0.05 \end{bmatrix} \end{align*}

and the weights are sampled from the following probability functions:

\begin{align*} PDFs &= \begin{bmatrix}Normal & Poisson\\ Poisson & Normal \end{bmatrix}\\ Parameters &= \begin{bmatrix}{\mu=3, \sigma^2=1} & {\lambda=5}\\ {\lambda=5} & {\mu=3, \sigma^2=1} \end{bmatrix} \end{align*}

[4]:
from numpy.random import normal, poisson

n = [50, 50]
p = [[0.5, 0.2],
     [0.2, 0.05]]
wt = [[normal, poisson],
      [poisson, normal]]
wtargs = [[dict(loc=3, scale=1), dict(lam=5)],
          [dict(lam=5), dict(loc=3, scale=1)]]

G = sbm(n=n, p=p, wt=wt, wtargs=wtargs)

Visualize the graph using heatmap

[5]:
_ = heatmap(G, title='Weighted SBM Simulation')
../../_images/tutorials_simulations_sbm_9_0.png