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
25 changes: 23 additions & 2 deletions R/undirected_planted_partition.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,35 @@ planted_partition <- function(
poisson_edges = TRUE,
allow_self_loops = TRUE
) {
if (is.null(within_block)) {
check_scalar(within_block, "within_block")
check_scalar(between_block, "between_block")
check_scalar(a, "a")
check_scalar(b, "b")

if (!is.null(within_block) && !is.null(a)) {
stop("Must specify only one of `within_block` and `a`.", call. = FALSE)
}

if (!is.null(between_block) && !is.null(b)) {
stop("Must specify only one of `between_block` and `b`.", call. = FALSE)
}

if (is.null(within_block) && !is.null(a)) {
within_block <- a / n
}

if (is.null(between_block)) {
if (is.null(between_block) && !is.null(b)) {
between_block <- b / n
}

if (is.null(within_block) || is.null(between_block)) {
stop(
"Must specify either `within_block` and `between_block`, ",
"or `a` and `b`.",
call. = FALSE
)
}

B <- matrix(between_block, nrow = k, ncol = k)
diag(B) <- rep(within_block, k)

Expand Down
27 changes: 27 additions & 0 deletions tests/testthat/test-undirected-planted-partition.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,30 @@ test_that("planted partition validators check B", {
fixed = TRUE
)
})

test_that("planted_partition() requires exactly one way to give each probability", {
expect_error(
planted_partition(n = 10, k = 2, within_block = 0.5, a = 2, between_block = 0.1),
"only one of `within_block` and `a`"
)
expect_error(
planted_partition(n = 10, k = 2, within_block = 0.5, between_block = 0.1, b = 2),
"only one of `between_block` and `b`"
)
expect_error(
planted_partition(n = 10, k = 2),
"Must specify either `within_block` and `between_block`, or `a` and `b`"
)
expect_error(
planted_partition(n = 10, k = 2, within_block = 0.5),
"Must specify either `within_block` and `between_block`, or `a` and `b`"
)
expect_error(
planted_partition(n = 10, k = 2, within_block = c(0.5, 0.2), between_block = 0.1),
"`within_block` must be a scalar"
)

# mixing the two parameterizations is fine
pp <- planted_partition(n = 10, k = 2, within_block = 0.5, b = 2)
expect_equal(pp$between_block, 0.2)
})
Loading