diff --git a/R/undirected_dcsbm.R b/R/undirected_dcsbm.R index 508bcf7..6ede032 100644 --- a/R/undirected_dcsbm.R +++ b/R/undirected_dcsbm.R @@ -109,22 +109,22 @@ validate_undirected_dcsbm <- function(x) { #' to a node in community `j` is `Poisson(B[i, j])`. Must be #' a square matrix. `matrix` and `Matrix` objects are both #' acceptable. If `B` is not symmetric, it will be -#' symmetrized via the update `B := B + t(B) / 2`. Defaults to `NULL`. +#' symmetrized via the update `B := (B + t(B)) / 2`. Defaults to `NULL`. #' You must specify either `k` or `B`, but not both. #' #' @param block_sizes (block sizes) Number of nodes in each block, -#' as a vector of integers. Must match the dimensions of `B`, or `k` and -#' must sum to `n`. Defaults to `NULL`, in which case blocks are made +#' as a vector of non-negative integers. Must match the dimensions of +#' `B`, or `k`, and must sum to `n`. Defaults to `NULL`, in which case blocks are made #' to be as balanced as possible. You can specify either `pi` or #' `block_sizes`, but not both. #' #' @param pi (block sizes) Relative block #' probabilities. Must be positive, but do not need to sum #' to one, as they will be normalized internally. -#' Must match the dimensions of `B` or `k`. Defaults to `NULL`, in which -#' case the `block_sizes` argument will take precedence. Note that -#' you can specify either `pi` or `block_sizes`, but should not -#' specify both. +#' Must match the dimensions of `B` or `k`. When `pi` is given, +#' block memberships are sampled independently from `pi`, so block +#' sizes are random. Defaults to `NULL`. You can specify either `pi` +#' or `block_sizes`, but not both. #' #' @param sort_nodes Logical indicating whether or not to sort the nodes #' so that they are grouped by block and by `theta`. Useful for plotting. @@ -179,11 +179,14 @@ validate_undirected_dcsbm <- function(x) { #' ## Block memberships #' #' Let \eqn{z_i} represent the block membership of node \eqn{i}. -#' To generate \eqn{z_i} we sample from a categorical -#' distribution (note that this is a special case of a -#' multinomial) with parameter \eqn{\pi}, such that -#' \eqn{\pi_i} represents the probability of ending up in -#' the ith block. Block memberships for each node are independent. +#' By default, and when `block_sizes` is given, block sizes are +#' fixed: exactly `block_sizes[j]` nodes are put in block \eqn{j} +#' (by default, blocks are as balanced as possible), and which nodes +#' land in which block is uniformly random. When `pi` is given +#' instead, each \eqn{z_i} is sampled independently from a categorical +#' distribution (a special case of a multinomial) with parameter +#' \eqn{\pi}, such that \eqn{\pi_j} is the probability of ending up in +#' the jth block, and block sizes are random. #' #' ## Degree heterogeneity #' @@ -331,38 +334,13 @@ dcsbm <- function( ### block membership - if (is.null(block_sizes) && is.null(pi)) { - base_value <- floor(n / k) - remainder <- n %% k - num_base_values <- k - remainder - - upper_values <- rep(base_value + 1, remainder) - base_values <- rep(base_value, num_base_values) - - block_sizes <- c(upper_values, base_values) - - z <- sample(rep(1:k, times = block_sizes)) - - # for sorting by block size later - pi <- block_sizes / n - } else if (!is.null(block_sizes)) { - if (sum(block_sizes) != n) { - stop( - "Sum of `block_sizes` must equal `n` or `length(theta)`.", - call. = FALSE - ) - } - - z <- sample(rep(1:k, times = block_sizes)) - - # for sorting by block size later - pi <- block_sizes / n + if (!is.null(block_sizes) && !is.null(pi)) { + stop( + "Must specify only one of `block_sizes` and `pi`, not both.", + call. = FALSE + ) } else if (!is.null(pi)) { - if (!is.null(pi) && !is.null(block_sizes)) { - stop("Length of `pi` must match dimensions of `B`.", call. = FALSE) - } - - if (length(pi) != nrow(B) || length(pi) != ncol(B)) { + if (length(pi) != k) { stop("Length of `pi` must match dimensions of `B`.", call. = FALSE) } @@ -372,7 +350,24 @@ dcsbm <- function( z <- sample(k, n, replace = TRUE, prob = pi) } else { - stop("Must at most one of `block_sizes` and `pi`.", call. = FALSE) + if (is.null(block_sizes)) { + # default: blocks as balanced as possible + block_sizes <- balanced_block_sizes(n, k) + } else { + check_block_sizes( + block_sizes, + arg = "block_sizes", + k = k, + n = n, + k_description = "dimensions of `B`", + n_description = "`n` or `length(theta)`" + ) + } + + z <- sample(rep(1:k, times = block_sizes)) + + # for sorting by block size later + pi <- block_sizes / n } # order mixing matrix by expected group size. `z` was sampled using the diff --git a/man/dcsbm.Rd b/man/dcsbm.Rd index 141bcd3..6aa781b 100644 --- a/man/dcsbm.Rd +++ b/man/dcsbm.Rd @@ -49,7 +49,7 @@ probabilities. The probability that a node in block \code{i} connects to a node in community \code{j} is \code{Poisson(B[i, j])}. Must be a square matrix. \code{matrix} and \code{Matrix} objects are both acceptable. If \code{B} is not symmetric, it will be -symmetrized via the update \code{B := B + t(B) / 2}. Defaults to \code{NULL}. +symmetrized via the update \code{B := (B + t(B)) / 2}. Defaults to \code{NULL}. You must specify either \code{k} or \code{B}, but not both.} \item{...}{ @@ -68,18 +68,18 @@ to achieve this. Defaults to \code{NULL}. Do not specify both }} \item{block_sizes}{(block sizes) Number of nodes in each block, -as a vector of integers. Must match the dimensions of \code{B}, or \code{k} and -must sum to \code{n}. Defaults to \code{NULL}, in which case blocks are made +as a vector of non-negative integers. Must match the dimensions of +\code{B}, or \code{k}, and must sum to \code{n}. Defaults to \code{NULL}, in which case blocks are made to be as balanced as possible. You can specify either \code{pi} or \code{block_sizes}, but not both.} \item{pi}{(block sizes) Relative block probabilities. Must be positive, but do not need to sum to one, as they will be normalized internally. -Must match the dimensions of \code{B} or \code{k}. Defaults to \code{NULL}, in which -case the \code{block_sizes} argument will take precedence. Note that -you can specify either \code{pi} or \code{block_sizes}, but should not -specify both.} +Must match the dimensions of \code{B} or \code{k}. When \code{pi} is given, +block memberships are sampled independently from \code{pi}, so block +sizes are random. Defaults to \code{NULL}. You can specify either \code{pi} +or \code{block_sizes}, but not both.} \item{sort_nodes}{Logical indicating whether or not to sort the nodes so that they are grouped by block and by \code{theta}. Useful for plotting. @@ -148,11 +148,14 @@ graph representation. \subsection{Block memberships}{ Let \eqn{z_i} represent the block membership of node \eqn{i}. -To generate \eqn{z_i} we sample from a categorical -distribution (note that this is a special case of a -multinomial) with parameter \eqn{\pi}, such that -\eqn{\pi_i} represents the probability of ending up in -the ith block. Block memberships for each node are independent. +By default, and when \code{block_sizes} is given, block sizes are +fixed: exactly \code{block_sizes[j]} nodes are put in block \eqn{j} +(by default, blocks are as balanced as possible), and which nodes +land in which block is uniformly random. When \code{pi} is given +instead, each \eqn{z_i} is sampled independently from a categorical +distribution (a special case of a multinomial) with parameter +\eqn{\pi}, such that \eqn{\pi_j} is the probability of ending up in +the jth block, and block sizes are random. } \subsection{Degree heterogeneity}{ diff --git a/man/mmsbm.Rd b/man/mmsbm.Rd index 54be6bd..f2c4ef4 100644 --- a/man/mmsbm.Rd +++ b/man/mmsbm.Rd @@ -48,7 +48,7 @@ probabilities. The probability that a node in block \code{i} connects to a node in community \code{j} is \code{Poisson(B[i, j])}. Must be a square matrix. \code{matrix} and \code{Matrix} objects are both acceptable. If \code{B} is not symmetric, it will be -symmetrized via the update \code{B := B + t(B) / 2}. Defaults to \code{NULL}. +symmetrized via the update \code{B := (B + t(B)) / 2}. Defaults to \code{NULL}. You must specify either \code{k} or \code{B}, but not both.} \item{...}{ diff --git a/man/planted_partition.Rd b/man/planted_partition.Rd index ea63756..33a8d43 100644 --- a/man/planted_partition.Rd +++ b/man/planted_partition.Rd @@ -62,18 +62,18 @@ between blocks. Useful for sparse graphs. Must specify either edge probabilities.} \item{block_sizes}{(block sizes) Number of nodes in each block, -as a vector of integers. Must match the dimensions of \code{B}, or \code{k} and -must sum to \code{n}. Defaults to \code{NULL}, in which case blocks are made +as a vector of non-negative integers. Must match the dimensions of +\code{B}, or \code{k}, and must sum to \code{n}. Defaults to \code{NULL}, in which case blocks are made to be as balanced as possible. You can specify either \code{pi} or \code{block_sizes}, but not both.} \item{pi}{(block sizes) Relative block probabilities. Must be positive, but do not need to sum to one, as they will be normalized internally. -Must match the dimensions of \code{B} or \code{k}. Defaults to \code{NULL}, in which -case the \code{block_sizes} argument will take precedence. Note that -you can specify either \code{pi} or \code{block_sizes}, but should not -specify both.} +Must match the dimensions of \code{B} or \code{k}. When \code{pi} is given, +block memberships are sampled independently from \code{pi}, so block +sizes are random. Defaults to \code{NULL}. You can specify either \code{pi} +or \code{block_sizes}, but not both.} \item{sort_nodes}{Logical indicating whether or not to sort the nodes so that they are grouped by block and by \code{theta}. Useful for plotting. diff --git a/man/sbm.Rd b/man/sbm.Rd index 92e7761..b9b3692 100644 --- a/man/sbm.Rd +++ b/man/sbm.Rd @@ -33,7 +33,7 @@ probabilities. The probability that a node in block \code{i} connects to a node in community \code{j} is \code{Poisson(B[i, j])}. Must be a square matrix. \code{matrix} and \code{Matrix} objects are both acceptable. If \code{B} is not symmetric, it will be -symmetrized via the update \code{B := B + t(B) / 2}. Defaults to \code{NULL}. +symmetrized via the update \code{B := (B + t(B)) / 2}. Defaults to \code{NULL}. You must specify either \code{k} or \code{B}, but not both.} \item{...}{ @@ -52,18 +52,18 @@ to achieve this. Defaults to \code{NULL}. Do not specify both }} \item{block_sizes}{(block sizes) Number of nodes in each block, -as a vector of integers. Must match the dimensions of \code{B}, or \code{k} and -must sum to \code{n}. Defaults to \code{NULL}, in which case blocks are made +as a vector of non-negative integers. Must match the dimensions of +\code{B}, or \code{k}, and must sum to \code{n}. Defaults to \code{NULL}, in which case blocks are made to be as balanced as possible. You can specify either \code{pi} or \code{block_sizes}, but not both.} \item{pi}{(block sizes) Relative block probabilities. Must be positive, but do not need to sum to one, as they will be normalized internally. -Must match the dimensions of \code{B} or \code{k}. Defaults to \code{NULL}, in which -case the \code{block_sizes} argument will take precedence. Note that -you can specify either \code{pi} or \code{block_sizes}, but should not -specify both.} +Must match the dimensions of \code{B} or \code{k}. When \code{pi} is given, +block memberships are sampled independently from \code{pi}, so block +sizes are random. Defaults to \code{NULL}. You can specify either \code{pi} +or \code{block_sizes}, but not both.} \item{sort_nodes}{Logical indicating whether or not to sort the nodes so that they are grouped by block and by \code{theta}. Useful for plotting. diff --git a/tests/testthat/test-dcsbm-block-sizes.R b/tests/testthat/test-dcsbm-block-sizes.R new file mode 100644 index 0000000..7104d66 --- /dev/null +++ b/tests/testthat/test-dcsbm-block-sizes.R @@ -0,0 +1,44 @@ +test_that("dcsbm() errors when both `pi` and `block_sizes` are given", { + expect_error( + dcsbm(theta = rep(1, 10), B = diag(2), block_sizes = c(5, 5), pi = c(.9, .1)), + "only one of `block_sizes` and `pi`" + ) + expect_error( + sbm(n = 10, B = diag(2), block_sizes = c(5, 5), pi = c(.9, .1)), + "only one of `block_sizes` and `pi`" + ) +}) + +test_that("dcsbm() validates `block_sizes`", { + # non-integer sizes used to silently drop nodes + expect_error( + dcsbm(theta = 1:5 + 0, B = diag(2), block_sizes = c(2.5, 2.5)), + "non-negative integers" + ) + expect_error( + dcsbm(theta = rep(1, 10), B = diag(2), block_sizes = c(3, 3, 4)), + "Length of `block_sizes` must match" + ) + expect_error( + dcsbm(theta = rep(1, 10), B = diag(2), block_sizes = c(-1, 11)), + "non-negative integers" + ) + expect_error( + dcsbm(theta = rep(1, 10), B = diag(2), block_sizes = c(4, 4)), + "Sum of `block_sizes` must equal" + ) +}) + +test_that("dcsbm() uses `pi` or `block_sizes` when given alone", { + set.seed(1) + m <- dcsbm(theta = rep(1, 1000), B = diag(2), pi = c(.9, .1), sort_nodes = FALSE) + # exact binomial bound on the size of the first block + expect_lt(abs(sum(m$z == "block1") - 900) / sqrt(1000 * .9 * .1), 5) + + m <- dcsbm(theta = rep(1, 10), B = diag(2), block_sizes = c(3, 7), sort_nodes = FALSE) + expect_equal(as.vector(table(m$z)), c(3, 7)) + expect_equal(m$pi, c(.3, .7)) + + m <- dcsbm(theta = rep(1, 11), B = diag(3)) + expect_equal(sort(as.vector(table(m$z))), c(3, 4, 4)) +})