From 8659ef87860e60a893dbd1f8a791b36c0fd05ec2 Mon Sep 17 00:00:00 2001 From: Alex Hayes Date: Tue, 17 Nov 2026 18:46:17 -0800 Subject: [PATCH] Support zero theta in dcsbm() and directed_dcsbm() With sort_nodes = TRUE, nodes were sorted by sorting the rows of the membership matrix, which sends all-zero rows (theta = 0) to the bottom, while z had been sorted separately and theta was read back from X@x in block order. So zero theta misaligned z, X and theta. The validators said theta must be "strictly positive" but only rejected negatives. Allow theta = 0 (isolated nodes): sort within blocks by permuting z and theta together with order(z, -theta), then build X from them. For positive theta this gives exactly the same models as before. Blocks whose theta are all zero stay zero under force_identifiability instead of becoming NaN. Validator messages and docs now say non-negative. Co-Authored-By: Claude Opus 5.5 --- R/directed_dcsbm.R | 50 +++++++++---------- R/undirected_chung_lu.R | 3 +- R/undirected_dcsbm.R | 33 ++++++------- R/undirected_mmsbm.R | 2 +- R/utils.R | 6 ++- man/chung_lu.Rd | 3 +- man/dcsbm.Rd | 3 +- man/directed_dcsbm.Rd | 4 +- man/mmsbm.Rd | 3 +- tests/testthat/test-zero-theta.R | 83 ++++++++++++++++++++++++++++++++ 10 files changed, 137 insertions(+), 53 deletions(-) create mode 100644 tests/testthat/test-zero-theta.R diff --git a/R/directed_dcsbm.R b/R/directed_dcsbm.R index eb3a185..ecc53bf 100644 --- a/R/directed_dcsbm.R +++ b/R/directed_dcsbm.R @@ -70,11 +70,11 @@ validate_directed_dcsbm <- function(x) { } if (any(values$theta_out < 0)) { - stop("Elements of `theta_out` must be strictly positive.", call. = FALSE) + stop("Elements of `theta_out` must be non-negative.", call. = FALSE) } if (any(values$theta_in < 0)) { - stop("Elements of `theta_in` must be strictly positive.", call. = FALSE) + stop("Elements of `theta_in` must be non-negative.", call. = FALSE) } if (length(values$theta_out) != nrow(values$X)) { @@ -225,7 +225,7 @@ sample_block_memberships <- function(n, k, pi, block_sizes = NULL) { #' explicitly specifying the degree heterogeneity #' parameters. This implicitly determines the number of nodes #' in the resulting graph, i.e. it will have `length(theta_out)` nodes. -#' Must be positive. Setting to a vector of ones recovers +#' Must be non-negative. Setting to a vector of ones recovers #' a stochastic blockmodel without degree correction. #' Defaults to `NULL`. You must specify either `n` #' or `theta_out` and `theta_in` together, but not both. `theta_out` @@ -236,7 +236,7 @@ sample_block_memberships <- function(n, k, pi, block_sizes = NULL) { #' explicitly specifying the degree heterogeneity #' parameters. This implicitly determines the number of nodes #' in the resulting graph, i.e. it will have `length(theta)` nodes. -#' Must be positive. Setting to a vector of ones recovers +#' Must be non-negative. Setting to a vector of ones recovers #' a stochastic blockmodel without degree correction. #' Defaults to `NULL`. You must specify either `n` #' or `theta_out` and `theta_in` together, but not both. `theta_in` @@ -946,6 +946,25 @@ directed_dcsbm <- function( z_in <- sort(z_in) } + if (force_identifiability) { + theta_out <- l1_normalize_within(theta_out, z_out) + theta_in <- l1_normalize_within(theta_in, z_in) + } + + if (sort_nodes) { + # within each block, sort by decreasing theta. permute each block + # membership vector together with its theta rather than sorting the + # rows of X and Y, which would send rows with theta = 0 to the bottom, + # out of line with z + row_order <- order(z_out, -theta_out) + z_out <- z_out[row_order] + theta_out <- theta_out[row_order] + + col_order <- order(z_in, -theta_in) + z_in <- z_in[col_order] + theta_in <- theta_in[col_order] + } + if (k_out > 1) { X <- sparse.model.matrix(~ z_out + 0) } else { @@ -958,32 +977,9 @@ directed_dcsbm <- function( Y <- Matrix(1, nrow = n, ncol = 1) } - if (force_identifiability) { - theta_out <- l1_normalize_within(theta_out, z_out) - theta_in <- l1_normalize_within(theta_in, z_in) - } - X <- set_membership_weights(X, theta_out) Y <- set_membership_weights(Y, theta_in) - if (sort_nodes) { - # X, Y indexing must match z indexing, be careful about this - X <- sort_by_all_columns(X) - Y <- sort_by_all_columns(Y) - - if (k_out > 1) { - theta_out <- X@x - } else { - theta_out <- sort(theta_out, decreasing = TRUE) - } - - if (k_in > 1) { - theta_in <- Y@x - } else { - theta_in <- sort(theta_in, decreasing = TRUE) - } - } - dcsbm <- new_directed_dcsbm( X = X, S = B, diff --git a/R/undirected_chung_lu.R b/R/undirected_chung_lu.R index 56f5fbe..b038b9f 100644 --- a/R/undirected_chung_lu.R +++ b/R/undirected_chung_lu.R @@ -21,7 +21,8 @@ #' explicitly specifying the degree heterogeneity #' parameters. This implicitly determines the number of nodes #' in the resulting graph, i.e. it will have `length(theta)` nodes. -#' Must be positive. Setting to a vector of ones recovers +#' Must be non-negative. Nodes with `theta` equal to zero have no +#' edges. Setting to a vector of ones recovers #' an erdos renyi graph. #' Defaults to `NULL`. You must specify either `n` or `theta`, #' but not both. diff --git a/R/undirected_dcsbm.R b/R/undirected_dcsbm.R index 508bcf7..1b637f6 100644 --- a/R/undirected_dcsbm.R +++ b/R/undirected_dcsbm.R @@ -40,7 +40,7 @@ validate_undirected_dcsbm <- function(x) { } if (any(values$theta < 0)) { - stop("Elements of `theta` must be strictly positive.", call. = FALSE) + stop("Elements of `theta` must be non-negative.", call. = FALSE) } if (length(levels(values$z)) != values$k) { @@ -91,7 +91,8 @@ validate_undirected_dcsbm <- function(x) { #' explicitly specifying the degree heterogeneity #' parameters. This implicitly determines the number of nodes #' in the resulting graph, i.e. it will have `length(theta)` nodes. -#' Must be positive. Setting to a vector of ones recovers +#' Must be non-negative. Nodes with `theta` equal to zero have no +#' edges. Setting to a vector of ones recovers #' a stochastic blockmodel without degree correction. #' Defaults to `NULL`. You must specify either `n` or `theta`, #' but not both. @@ -394,29 +395,27 @@ dcsbm <- function( z <- sort(z) } - if (k > 1) { - X <- sparse.model.matrix(~ z + 0) - } else { - X <- Matrix(1, nrow = n, ncol = 1) - } - if (force_identifiability) { theta <- l1_normalize_within(theta, z) } - X <- set_membership_weights(X, theta) - if (sort_nodes) { - # note that X and z indexing must match - X <- sort_by_all_columns(X) + # within each block, sort by decreasing theta. permute z and theta + # together rather than sorting the rows of X, which would send nodes + # with theta = 0 (all-zero rows) to the bottom, out of line with z + node_order <- order(z, -theta) + z <- z[node_order] + theta <- theta[node_order] + } - if (k > 1) { - theta <- X@x - } else { - theta <- sort(theta, decreasing = TRUE) - } + if (k > 1) { + X <- sparse.model.matrix(~ z + 0) + } else { + X <- Matrix(1, nrow = n, ncol = 1) } + X <- set_membership_weights(X, theta) + dcsbm <- new_undirected_dcsbm( X = X, S = B, diff --git a/R/undirected_mmsbm.R b/R/undirected_mmsbm.R index 7a8f81f..e89f489 100644 --- a/R/undirected_mmsbm.R +++ b/R/undirected_mmsbm.R @@ -13,7 +13,7 @@ validate_undirected_mmsbm <- function(x) { } if (any(values$theta < 0)) { - stop("Elements of `theta` must be strictly positive.", call. = FALSE) + stop("Elements of `theta` must be non-negative.", call. = FALSE) } if (min(values$S) < 0) { diff --git a/R/utils.R b/R/utils.R index 37ed072..6b64bc3 100644 --- a/R/utils.R +++ b/R/utils.R @@ -22,10 +22,12 @@ l1_normalize <- function(x) x / sum(x) # `x` and `block` should be vectors of the same length. `block` should be # discrete and `x` numeric. normalizes `x` to sum to one within each distinct -# block. ideally only call on positive x to avoid divide by zero errors. +# block. blocks whose elements are all zero are left as zeros. # the result is in the same order as `x` (i.e. node order, not block order) l1_normalize_within <- function(x, block) { - as.numeric(x / stats::ave(x, block, FUN = sum)) + totals <- stats::ave(x, block, FUN = sum) + # blocks whose x are all zero stay zero rather than becoming NaN + as.numeric(ifelse(totals == 0, 0, x / totals)) } # put node-level weights `theta` into the non-zero entries of a block diff --git a/man/chung_lu.Rd b/man/chung_lu.Rd index 2916ea0..4e2d71b 100644 --- a/man/chung_lu.Rd +++ b/man/chung_lu.Rd @@ -27,7 +27,8 @@ or \code{theta}, but not both.} explicitly specifying the degree heterogeneity parameters. This implicitly determines the number of nodes in the resulting graph, i.e. it will have \code{length(theta)} nodes. -Must be positive. Setting to a vector of ones recovers +Must be non-negative. Nodes with \code{theta} equal to zero have no +edges. Setting to a vector of ones recovers an erdos renyi graph. Defaults to \code{NULL}. You must specify either \code{n} or \code{theta}, but not both.} diff --git a/man/dcsbm.Rd b/man/dcsbm.Rd index 141bcd3..5fa5e31 100644 --- a/man/dcsbm.Rd +++ b/man/dcsbm.Rd @@ -31,7 +31,8 @@ or \code{theta}, but not both.} explicitly specifying the degree heterogeneity parameters. This implicitly determines the number of nodes in the resulting graph, i.e. it will have \code{length(theta)} nodes. -Must be positive. Setting to a vector of ones recovers +Must be non-negative. Nodes with \code{theta} equal to zero have no +edges. Setting to a vector of ones recovers a stochastic blockmodel without degree correction. Defaults to \code{NULL}. You must specify either \code{n} or \code{theta}, but not both.} diff --git a/man/directed_dcsbm.Rd b/man/directed_dcsbm.Rd index de005df..d2603e8 100644 --- a/man/directed_dcsbm.Rd +++ b/man/directed_dcsbm.Rd @@ -40,7 +40,7 @@ for both \code{theta_out} and \code{theta_in}.} explicitly specifying the degree heterogeneity parameters. This implicitly determines the number of nodes in the resulting graph, i.e. it will have \code{length(theta_out)} nodes. -Must be positive. Setting to a vector of ones recovers +Must be non-negative. Setting to a vector of ones recovers a stochastic blockmodel without degree correction. Defaults to \code{NULL}. You must specify either \code{n} or \code{theta_out} and \code{theta_in} together, but not both. \code{theta_out} @@ -51,7 +51,7 @@ row sums of the adjacency matrix.} explicitly specifying the degree heterogeneity parameters. This implicitly determines the number of nodes in the resulting graph, i.e. it will have \code{length(theta)} nodes. -Must be positive. Setting to a vector of ones recovers +Must be non-negative. Setting to a vector of ones recovers a stochastic blockmodel without degree correction. Defaults to \code{NULL}. You must specify either \code{n} or \code{theta_out} and \code{theta_in} together, but not both. \code{theta_in} diff --git a/man/mmsbm.Rd b/man/mmsbm.Rd index 54be6bd..ecdce8f 100644 --- a/man/mmsbm.Rd +++ b/man/mmsbm.Rd @@ -30,7 +30,8 @@ or \code{theta}, but not both.} explicitly specifying the degree heterogeneity parameters. This implicitly determines the number of nodes in the resulting graph, i.e. it will have \code{length(theta)} nodes. -Must be positive. Setting to a vector of ones recovers +Must be non-negative. Nodes with \code{theta} equal to zero have no +edges. Setting to a vector of ones recovers a stochastic blockmodel without degree correction. Defaults to \code{NULL}. You must specify either \code{n} or \code{theta}, but not both.} diff --git a/tests/testthat/test-zero-theta.R b/tests/testthat/test-zero-theta.R new file mode 100644 index 0000000..b47da68 --- /dev/null +++ b/tests/testthat/test-zero-theta.R @@ -0,0 +1,83 @@ +# theta = 0 is allowed and gives isolated nodes. sorting must keep each +# node's block, theta and row of X together + +check_alignment <- function(X, z, theta, sorted) { + X <- as.matrix(X) + z <- as.integer(z) + + expect_equal(unname(rowSums(X)), unname(theta)) + + # every row has at most one non-zero entry, in the column of its block + nonzero <- theta > 0 + expect_equal( + max.col(X != 0, ties.method = "first")[nonzero], + z[nonzero] + ) + expect_true(all(rowSums(X != 0) <= 1)) + + if (sorted) { + expect_false(is.unsorted(z)) + for (b in unique(z)) { + expect_false(is.unsorted(-theta[z == b])) + } + } +} + +test_that("dcsbm() keeps z, X and theta aligned with zero theta", { + theta <- c(0, 1, 2, 3, 0, 1, 2, 3, 4, 5) + + for (sort_nodes in c(TRUE, FALSE)) { + for (fi in c(TRUE, FALSE)) { + for (k in 1:2) { + set.seed(3) + m <- dcsbm( + theta = theta, B = diag(k), sort_nodes = sort_nodes, + force_identifiability = fi + ) + check_alignment(m$X, m$z, m$theta, sort_nodes) + expect_false(anyNA(m$theta)) + + # the zero-theta nodes are the ones with zero expected degree + expect_equal(sum(m$theta == 0), 2) + expect_equal( + which(m$theta == 0), + unname(which(Matrix::rowSums(m$X %*% m$S %*% t(m$X)) == 0)) + ) + + A <- sample_sparse(m) + expect_equal(sum(A[m$theta == 0, ]), 0) + } + } + } +}) + +test_that("directed_dcsbm() keeps z, X/Y and theta aligned with zero theta", { + theta_out <- c(0, 1, 2, 3, 0, 1, 2, 3, 4, 5) + theta_in <- c(5, 0, 4, 3, 2, 0, 1, 3, 2, 1) + + for (sort_nodes in c(TRUE, FALSE)) { + for (fi in c(TRUE, FALSE)) { + for (shared_blocks in c(TRUE, FALSE)) { + set.seed(3) + m <- directed_dcsbm( + theta_out = theta_out, theta_in = theta_in, B = diag(2), + sort_nodes = sort_nodes, force_identifiability = fi, + shared_blocks = shared_blocks + ) + check_alignment(m$X, m$z_out, m$theta_out, sort_nodes && !shared_blocks) + check_alignment(m$Y, m$z_in, m$theta_in, sort_nodes && !shared_blocks) + + A <- sample_sparse(m) + expect_equal(sum(A[m$theta_out == 0, ]), 0) + expect_equal(sum(A[, m$theta_in == 0]), 0) + } + } + } +}) + +test_that("negative theta is still an error", { + expect_error( + dcsbm(theta = c(-1, 1, 1, 1), B = diag(2)), + "non-negative" + ) +})