Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 39 additions & 44 deletions R/undirected_dcsbm.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
#'
Expand Down Expand Up @@ -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)
}

Expand All @@ -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
Expand Down
27 changes: 15 additions & 12 deletions man/dcsbm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/mmsbm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions man/planted_partition.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions man/sbm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions tests/testthat/test-dcsbm-block-sizes.R
Original file line number Diff line number Diff line change
@@ -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))
})
Loading