def _build_Gs(adj_mat): """ Build single-edge indicator matrix. Build the sparse single-edge adjacency matrix `Gs` from a graph adjacency matrix. Parameters ---------- adj_mat : matrix The original adjacency matrix. Returns ------- Gs : sparse matrix A single-edge adjacency matrix in sparse form. """ G = _build_G(adj_mat) J = _build_J(adj_mat) if sparse.issparse(adj_mat): Gs = mcut._drop0_killdiag(G - G.multiply(J.T)) else: Gs = mcut._drop0_killdiag(G - G * J.T) return Gs
def _build_Js(adj_mat): """ Build single-edge indicator matrix. Build the sparse single-edge indicator matrix `Js` from a graph adjacency matrix. Parameters ---------- adj_mat : matrix The original adjacency matrix. Returns ------- Js : sparse matrix A single-edge indicator matrix in sparse form. """ Gs = _build_Gs(adj_mat) if sparse.issparse(adj_mat): Js = mcut._drop0_killdiag(Gs > 0) else: Js = mcut._drop0_killdiag(1 * (Gs > 0)) return Js
def _build_Gp(adj_mat): """ Build product matrix. Build the sparse product matrix `Gp` from a graph adjacency matrix. Parameters ---------- adj_mat : matrix The original adjacency matrix. Returns ------- Gp : sparse matrix A product matrix in sparse form. """ G = _build_G(adj_mat) if sparse.issparse(adj_mat): Gp = mcut._drop0_killdiag(G.multiply(G.T)) else: Gp = mcut._drop0_killdiag(G * G.T) return Gp
def _build_J(adj_mat): """ Build directed indicator matrix. Build the sparse directed indicator matrix `J` from a graph adjacency matrix. Parameters ---------- adj_mat : matrix The original adjacency matrix. Returns ------- J : sparse matrix A directed indicator matrix in sparse form. """ G = _build_G(adj_mat) if sparse.issparse(adj_mat): J = mcut._drop0_killdiag(G > 0) else: J = mcut._drop0_killdiag(1 * (G > 0)) return J
def _build_Jn(adj_mat): """ Build vertex-distinct indicator matrix. Build the vertex-distinct indicator matrix `Jn` from a graph adjacency matrix. Parameters ---------- adj_mat : matrix The original adjacency matrix. Returns ------- Jn : sparse matrix A vertex-distinct indicator matrix. """ n = adj_mat.shape[0] if sparse.issparse(adj_mat): Jn = sparse.csr_matrix(mcut._drop0_killdiag(np.ones((n, n)))) else: Jn = mcut._drop0_killdiag(np.ones((n, n))) return Jn
def _build_J0(adj_mat): """ Build missing-edge indicator matrix. Build the missing-edge indicator matrix `J0` from a graph adjacency matrix. Parameters ---------- adj_mat : matrix The original adjacency matrix. Returns ------- J0 : sparse matrix A missing-edge indicator matrix. """ G = _build_G(adj_mat) if sparse.issparse(adj_mat): J0 = sparse.csr_matrix(mcut._drop0_killdiag((G + G.T) == 0)) else: J0 = mcut._drop0_killdiag((G + G.T) == 0) return J0
def _build_Jd(adj_mat): """ Build double-edge indicator matrix. Build the sparse double-edge indicator matrix `Jd` from a graph adjacency matrix. Parameters ---------- adj_mat : matrix The original adjacency matrix. Returns ------- Jd : sparse matrix A double-edge indicator matrix in sparse form. """ Gd = _build_Gd(adj_mat) if sparse.issparse(adj_mat): Jd = mcut._drop0_killdiag(Gd > 0) else: Jd = mcut._drop0_killdiag(1 * (Gd > 0)) return Jd
def _build_Gd(adj_mat): """ Build double-edge adjacency matrix. Build the sparse double-edge adjacency matrix `Gd` from a graph adjacency matrix. Parameters ---------- adj_mat : matrix The original adjacency matrix. Returns ------- Gd : sparse matrix A double-edge adjacency matrix in sparse form. """ J = _build_J(adj_mat) G = _build_G(adj_mat) if sparse.issparse(adj_mat): Gd = mcut._drop0_killdiag((G + G.T).multiply(J).multiply(J.T)) else: Gd = mcut._drop0_killdiag((G + G.T) * J * J.T) return Gd
def test_drop0_kill_diag(): adj_mat_dense = np.array(range(-1, 8)).reshape((3, 3)) adj_mat_sparse = sparse.csr_matrix(adj_mat_dense) ans = np.array([0, 0, 1, 2, 0, 4, 5, 6, 0]).reshape((3, 3)) assert np.allclose(mcut._drop0_killdiag(adj_mat_dense), ans) assert np.allclose(mcut._drop0_killdiag(adj_mat_sparse).toarray(), ans)
def sample_dsbm(block_sizes, connection_matrix, weight_matrix=None, sample_weight_type="unweighted"): """ Sample a directed stochastic block model (DSBM). Sample the (weighted) adjacency matrix of a (weighted) directed stochastic block model (DSBM) with specified parameters. Parameters ---------- block_sizes : list of int A list containing the size of each block of vertices. connection_matrix : matrix A matrix containing the block-to-block connection probabilities. sample_weight_type : str The type of weighting scheme. One of `"unweighted"`, `"constant"` or `"poisson"`. weight_matrix : matrix A matrix containing the block-to-block weight parameters. Unused for `sample_weight_type = "constant"`. Defaults to `None`. Returns ------- adj_mat : sparse matrix A randomly sampled (weighted) adjacency matrix of a DSBM. Examples -------- >>> block_sizes = [10, 10] >>> connection_matrix = np.array([0.8, 0.1, 0.1, 0.8]).reshape((2, 2)) >>> weight_matrix = np.array([10, 3, 3, 10]).reshape((2, 2)) >>> sample_dsbm(block_sizes, connection_matrix, weight_matrix, "poisson") """ # check args assert block_sizes == [int(x) for x in block_sizes] assert all(x > 0 for x in block_sizes) assert len(block_sizes) == connection_matrix.shape[0] assert len(block_sizes) == connection_matrix.shape[1] assert (connection_matrix >= 0).all() assert (connection_matrix <= 1).all() assert sample_weight_type in ["unweighted", "constant", "poisson"] if sample_weight_type != "unweighted": assert weight_matrix is not None assert len(block_sizes) == weight_matrix.shape[0] assert len(block_sizes) == weight_matrix.shape[1] assert (weight_matrix >= 0).all() # initialize variables k = len(block_sizes) block_list = [] for i in range(k): row_list = [] for j in range(k): # block parameters ni = block_sizes[i] nj = block_sizes[j] p = connection_matrix[i, j] # generate block if sample_weight_type == "unweighted": w = 1 else: w = weight_matrix[i, j] block = mcut._random_sparse_matrix(ni, nj, p, sample_weight_type, w) row_list.append(block) block_list.append(row_list) # build from dense blocks if isinstance(block_list[0][0], np.ndarray): adj_mat = np.block(block_list) # build from sparse blocks else: adj_mat = sparse.bmat(block_list) adj_mat = sparse.csr_matrix(adj_mat) adj_mat = mcut._drop0_killdiag(adj_mat) return adj_mat