diff --git a/R/directed_erdos_renyi.R b/R/directed_erdos_renyi.R index 009df77..92f8d72 100644 --- a/R/directed_erdos_renyi.R +++ b/R/directed_erdos_renyi.R @@ -32,8 +32,15 @@ validate_directed_erdos_renyi <- function(x) { stop("`Y` must have a single column.", call. = FALSE) } - if (values$p < 0) { - stop("`p` must be strictly non-negative.", call. = FALSE) + if (!is.numeric(values$p) || length(values$p) != 1 || values$p <= 0) { + stop("`p` must be a positive scalar.", call. = FALSE) + } + + if (!values$poisson_edges && values$p >= 1) { + stop( + "`p` must be strictly between zero and one for Bernoulli graphs.", + call. = FALSE + ) } x @@ -43,9 +50,12 @@ validate_directed_erdos_renyi <- function(x) { #' #' @param n Number of nodes in graph. #' -#' @param p Probability of an edge between any two nodes. You must specify -#' either `p`, `expected_in_degree`, `expected_out_degree` or -#' `expected_density`. +#' @param p Probability of an edge between any two nodes, strictly +#' between zero and one. You must specify either `p`, +#' `expected_in_degree`, `expected_out_degree` or `expected_density`. +#' When one of those is given, the stored `p` is the edge probability +#' (the edge rate, for Poisson graphs) of the rescaled model, not the `p` +#' given here. #' #' @inheritDotParams directed_factor_model expected_in_degree expected_out_degree #' @inherit directed_factor_model params return @@ -92,6 +102,8 @@ directed_erdos_renyi <- function( if (is.null(p)) { p <- 0.5 # doesn't matter, will get rescaled anyway + } else if (!is.numeric(p) || length(p) != 1 || p <= 0 || 1 <= p) { + stop("`p` must be strictly between zero and one.", call. = FALSE) } S <- matrix(p, nrow = 1, ncol = 1) @@ -106,5 +118,10 @@ directed_erdos_renyi <- function( ... ) + # store the edge probability of the final, rescaled model + if (is_rescaled(...)) { + er$p <- edge_scale_S(er)[1, 1] + } + validate_directed_erdos_renyi(er) } diff --git a/R/directed_planted_partition.R b/R/directed_planted_partition.R index 16fc09c..a086b06 100644 --- a/R/directed_planted_partition.R +++ b/R/directed_planted_partition.R @@ -166,14 +166,14 @@ check_positive_integer <- function(x, arg) { #' of the [directed_dcsbm()] object (with `theta_out` and `theta_in` #' identically one), with additional fields: #' -#' - `within_block`: The probability of edge formation within a block, -#' as specified (or as computed from `a / n`), before any rescaling -#' by `expected_out_degree`, `expected_in_degree` or `expected_density`. +#' - `within_block`: The probability of edge formation within a block. +#' If `expected_out_degree`, `expected_in_degree` or `expected_density` +#' is given, this is the probability after rescaling (the edge rate, +#' for Poisson graphs), not the value given as `within_block` (or +#' `a / n`). #' #' - `between_block`: The probability of edge formation between two -#' distinct blocks, as specified (or as computed from `b / n`), before -#' any rescaling by `expected_out_degree`, `expected_in_degree` or -#' `expected_density`. +#' distinct blocks, after any rescaling, as for `within_block`. #' #' @export #' @family stochastic block models @@ -442,6 +442,17 @@ directed_planted_partition <- function( subclass = "directed_planted_partition" ) + # store the edge probabilities of the final, rescaled model + if (is_rescaled(...)) { + S <- edge_scale_S(pp) + if (k > 1) { + between_block <- S[1, 2] + } else { + between_block <- between_block * S[1, 1] / within_block + } + within_block <- S[1, 1] + } + pp$within_block <- within_block pp$between_block <- between_block diff --git a/R/undirected_erdos_renyi.R b/R/undirected_erdos_renyi.R index 6b70536..ff0958c 100644 --- a/R/undirected_erdos_renyi.R +++ b/R/undirected_erdos_renyi.R @@ -16,8 +16,15 @@ validate_undirected_erdos_renyi <- function(x) { stop("`X` must have a single column.", call. = FALSE) } - if (values$p <= 0 || 1 <= values$p) { - stop("`p` must be strictly between zero and one.", call. = FALSE) + if (!is.numeric(values$p) || length(values$p) != 1 || values$p <= 0) { + stop("`p` must be a positive scalar.", call. = FALSE) + } + + if (!values$poisson_edges && values$p >= 1) { + stop( + "`p` must be strictly between zero and one for Bernoulli graphs.", + call. = FALSE + ) } x @@ -27,8 +34,11 @@ validate_undirected_erdos_renyi <- function(x) { #' #' @param n Number of nodes in graph. #' -#' @param p Probability of an edge between any two nodes. You must specify -#' either `p`, `expected_degree` or `expected_density`. +#' @param p Probability of an edge between any two nodes, strictly +#' between zero and one. You must specify either `p`, `expected_degree` +#' or `expected_density`. When `expected_degree` or `expected_density` is +#' given, the stored `p` is the edge probability (the edge rate, for +#' Poisson graphs) of the rescaled model, not the `p` given here. #' #' @inheritDotParams undirected_factor_model expected_degree #' @inherit undirected_factor_model params return @@ -77,6 +87,8 @@ erdos_renyi <- function( if (is.null(p)) { p <- 0.5 # doesn't matter, will get rescaled anyway + } else if (!is.numeric(p) || length(p) != 1 || p <= 0 || 1 <= p) { + stop("`p` must be strictly between zero and one.", call. = FALSE) } B <- matrix(p) @@ -89,5 +101,11 @@ erdos_renyi <- function( poisson_edges = poisson_edges, allow_self_loops = allow_self_loops ) + + # store the edge probability of the final, rescaled model + if (is_rescaled(...)) { + er$p <- edge_scale_S(er)[1, 1] + } + validate_undirected_erdos_renyi(er) } diff --git a/R/undirected_planted_partition.R b/R/undirected_planted_partition.R index 0790eab..7304602 100644 --- a/R/undirected_planted_partition.R +++ b/R/undirected_planted_partition.R @@ -20,12 +20,21 @@ validate_undirected_planted_partition <- function(x) { stop("`between_block` must be a scalar.", call. = FALSE) } - if (!is.null(x$within_block) && (x$within_block < 0 || x$within_block > 1)) { + # after rescaling by `expected_degree` or `expected_density`, the stored + # values are edge rates for Poisson graphs, so they may exceed one. the + # values the user gives are checked in planted_partition() + max_value <- if (x$poisson_edges) Inf else 1 + + if ( + !is.null(x$within_block) && + (x$within_block < 0 || x$within_block > max_value) + ) { stop("`within_block` must be between zero and one.", call. = FALSE) } if ( - !is.null(x$between_block) && (x$between_block < 0 || x$between_block > 1) + !is.null(x$between_block) && + (x$between_block < 0 || x$between_block > max_value) ) { stop("`between_block` must be between zero and one.", call. = FALSE) } @@ -87,9 +96,12 @@ validate_undirected_planted_partition <- function(x) { #' of the [sbm()] object, with additional fields: #' #' - `within_block`: The probability of edge formation within a block. +#' If `expected_degree` or `expected_density` is given, this is the +#' probability after rescaling (the edge rate, for Poisson graphs), +#' not the value given as `within_block` (or `a / n`). #' #' - `between_block`: The probability of edge formation between two distinct -#' blocks. +#' blocks, after any rescaling, as for `within_block`. #' #' @export #' @family stochastic block models @@ -140,6 +152,20 @@ planted_partition <- function( between_block <- b / n } + if (within_block < 0 || within_block > 1) { + stop( + "`within_block` (or `a / n`) must be between zero and one.", + call. = FALSE + ) + } + + if (between_block < 0 || between_block > 1) { + stop( + "`between_block` (or `b / n`) must be between zero and one.", + call. = FALSE + ) + } + B <- matrix(between_block, nrow = k, ncol = k) diag(B) <- rep(within_block, k) @@ -161,6 +187,17 @@ planted_partition <- function( pp <- validate_undirected_sbm(pp) + # store the edge probabilities of the final, rescaled model + if (is_rescaled(...)) { + S <- edge_scale_S(pp) + if (k > 1) { + between_block <- S[1, 2] + } else { + between_block <- between_block * S[1, 1] / within_block + } + within_block <- S[1, 1] + } + pp$within_block <- within_block pp$between_block <- between_block diff --git a/R/utils.R b/R/utils.R index 37ed072..58aa461 100644 --- a/R/utils.R +++ b/R/utils.R @@ -92,6 +92,23 @@ balanced_block_sizes <- function(n, k) { c(rep(base_value + 1, remainder), rep(base_value, k - remainder)) } +# the edge probabilities (Bernoulli graphs) or edge rates (Poisson graphs) +# implied by the final S of a model, i.e. after any rescaling to achieve an +# expected degree or density, on the scale users specify them. this undoes +# the `-log(1 - S)` transform the factor model constructors apply to +# Bernoulli graphs +edge_scale_S <- function(model) { + S <- as.matrix(model$S) + if (model$poisson_edges) S else -expm1(-S) +} + +# TRUE when `...` contains an argument such as `expected_degree`, +# `expected_in_degree` or `expected_density` that rescales S +is_rescaled <- function(...) { + dots <- names(list(...)) + !is.null(dots) && any(startsWith(dots, "expected_")) +} + utils::globalVariables( c("i", "j", "value", "x") ) diff --git a/man/directed_erdos_renyi.Rd b/man/directed_erdos_renyi.Rd index 9cf9941..9752c40 100644 --- a/man/directed_erdos_renyi.Rd +++ b/man/directed_erdos_renyi.Rd @@ -28,9 +28,12 @@ to achieve this. Defaults to \code{NULL}. Specify only one of \code{expected_in_degree}, \code{expected_out_degree}, and \code{expected_density}.} }} -\item{p}{Probability of an edge between any two nodes. You must specify -either \code{p}, \code{expected_in_degree}, \code{expected_out_degree} or -\code{expected_density}.} +\item{p}{Probability of an edge between any two nodes, strictly +between zero and one. You must specify either \code{p}, +\code{expected_in_degree}, \code{expected_out_degree} or \code{expected_density}. +When one of those is given, the stored \code{p} is the edge probability +(the edge rate, for Poisson graphs) of the rescaled model, not the \code{p} +given here.} \item{poisson_edges}{Logical indicating whether or not multiple edges are allowed to form between a pair of diff --git a/man/directed_planted_partition.Rd b/man/directed_planted_partition.Rd index 762c2cc..be0032e 100644 --- a/man/directed_planted_partition.Rd +++ b/man/directed_planted_partition.Rd @@ -126,13 +126,13 @@ A \code{directed_planted_partition} S3 object, which is a subclass of the \code{\link[=directed_dcsbm]{directed_dcsbm()}} object (with \code{theta_out} and \code{theta_in} identically one), with additional fields: \itemize{ -\item \code{within_block}: The probability of edge formation within a block, -as specified (or as computed from \code{a / n}), before any rescaling -by \code{expected_out_degree}, \code{expected_in_degree} or \code{expected_density}. +\item \code{within_block}: The probability of edge formation within a block. +If \code{expected_out_degree}, \code{expected_in_degree} or \code{expected_density} +is given, this is the probability after rescaling (the edge rate, +for Poisson graphs), not the value given as \code{within_block} (or +\code{a / n}). \item \code{between_block}: The probability of edge formation between two -distinct blocks, as specified (or as computed from \code{b / n}), before -any rescaling by \code{expected_out_degree}, \code{expected_in_degree} or -\code{expected_density}. +distinct blocks, after any rescaling, as for \code{within_block}. } } \description{ diff --git a/man/erdos_renyi.Rd b/man/erdos_renyi.Rd index 08d59d3..7be57cd 100644 --- a/man/erdos_renyi.Rd +++ b/man/erdos_renyi.Rd @@ -18,8 +18,11 @@ to achieve this. Defaults to \code{NULL}. Do not specify both \code{expected_degree} and \code{expected_density} at the same time.} }} -\item{p}{Probability of an edge between any two nodes. You must specify -either \code{p}, \code{expected_degree} or \code{expected_density}.} +\item{p}{Probability of an edge between any two nodes, strictly +between zero and one. You must specify either \code{p}, \code{expected_degree} +or \code{expected_density}. When \code{expected_degree} or \code{expected_density} is +given, the stored \code{p} is the edge probability (the edge rate, for +Poisson graphs) of the rescaled model, not the \code{p} given here.} \item{poisson_edges}{Logical indicating whether or not multiple edges are allowed to form between a pair of diff --git a/man/planted_partition.Rd b/man/planted_partition.Rd index ea63756..861e3b0 100644 --- a/man/planted_partition.Rd +++ b/man/planted_partition.Rd @@ -102,8 +102,11 @@ An \code{undirected_planted_partition} S3 object, which is a subclass of the \code{\link[=sbm]{sbm()}} object, with additional fields: \itemize{ \item \code{within_block}: The probability of edge formation within a block. +If \code{expected_degree} or \code{expected_density} is given, this is the +probability after rescaling (the edge rate, for Poisson graphs), +not the value given as \code{within_block} (or \code{a / n}). \item \code{between_block}: The probability of edge formation between two distinct -blocks. +blocks, after any rescaling, as for \code{within_block}. } } \description{ diff --git a/tests/testthat/test-rescaled-params.R b/tests/testthat/test-rescaled-params.R new file mode 100644 index 0000000..14ed91e --- /dev/null +++ b/tests/testthat/test-rescaled-params.R @@ -0,0 +1,84 @@ +# stored edge probabilities must describe the final model, after rescaling +# by `expected_degree`, `expected_density` and friends + +prob_S <- function(model) { + S <- as.matrix(model$S) + if (model$poisson_edges) S else 1 - exp(-S) +} + +test_that("erdos_renyi() stores the rescaled p", { + for (poisson_edges in c(TRUE, FALSE)) { + er <- erdos_renyi(n = 100, expected_degree = 5, poisson_edges = poisson_edges) + expect_equal(er$p, prob_S(er)[1, 1]) + expect_equal(er$p, 0.05) + + er <- erdos_renyi(n = 100, p = 0.5, expected_density = 0.1, poisson_edges = poisson_edges) + expect_equal(er$p, prob_S(er)[1, 1]) + expect_equal(er$p, 0.1) + + er <- erdos_renyi(n = 100, p = 0.2, poisson_edges = poisson_edges) + expect_identical(er$p, 0.2) + } + + # Poisson rates above one are fine after rescaling + er <- erdos_renyi(n = 10, expected_degree = 50) + expect_equal(er$p, 5) +}) + +test_that("directed_erdos_renyi() stores the rescaled p", { + for (poisson_edges in c(TRUE, FALSE)) { + er <- directed_erdos_renyi(n = 100, expected_out_degree = 5, poisson_edges = poisson_edges) + expect_equal(er$p, prob_S(er)[1, 1]) + expect_equal(er$p, 0.05) + + er <- directed_erdos_renyi(n = 100, p = 0.2, poisson_edges = poisson_edges) + expect_identical(er$p, 0.2) + } +}) + +test_that("erdos_renyi() and directed_erdos_renyi() check p the same way", { + for (f in list(erdos_renyi, directed_erdos_renyi)) { + expect_error(f(n = 10, p = 1.5), "strictly between zero and one") + expect_error(f(n = 10, p = 0), "strictly between zero and one") + expect_error(f(n = 10, p = -0.1), "strictly between zero and one") + expect_error(f(n = 10, p = c(0.1, 0.2)), "strictly between zero and one") + } +}) + +test_that("planted_partition() stores the rescaled edge probabilities", { + for (poisson_edges in c(TRUE, FALSE)) { + pp <- planted_partition( + n = 100, k = 2, within_block = .5, between_block = .1, + expected_degree = 5, poisson_edges = poisson_edges + ) + S <- prob_S(pp) + expect_equal(pp$within_block, S[1, 1]) + expect_equal(pp$between_block, S[1, 2]) + # the ratio is preserved + expect_equal(pp$within_block / pp$between_block, 5) + } + + # without rescaling the given values are kept exactly + pp <- planted_partition(n = 100, k = 2, within_block = .5, between_block = .1) + expect_identical(c(pp$within_block, pp$between_block), c(.5, .1)) + + # Poisson rates above one are fine after rescaling + pp <- planted_partition( + n = 10, k = 2, within_block = .5, between_block = .1, expected_degree = 30 + ) + expect_gt(pp$within_block, 1) + expect_equal(pp$within_block, as.matrix(pp$S)[1, 1]) +}) + +test_that("directed_planted_partition() stores the rescaled edge probabilities", { + for (poisson_edges in c(TRUE, FALSE)) { + pp <- directed_planted_partition( + n = 100, k = 2, within_block = .5, between_block = .1, + expected_out_degree = 5, poisson_edges = poisson_edges + ) + S <- prob_S(pp) + expect_equal(pp$within_block, S[1, 1]) + expect_equal(pp$between_block, S[1, 2]) + expect_equal(pp$within_block / pp$between_block, 5) + } +}) diff --git a/tests/testthat/test-undirected-planted-partition.R b/tests/testthat/test-undirected-planted-partition.R index a45831b..5e601bf 100644 --- a/tests/testthat/test-undirected-planted-partition.R +++ b/tests/testthat/test-undirected-planted-partition.R @@ -117,7 +117,7 @@ test_that("planted partition validators check B", { expect_error( planted_partition(n = 30, k = 3, within_block = 1.5, between_block = 0.1), - "`within_block` must be between zero and one.", + "`within_block` (or `a / n`) must be between zero and one.", fixed = TRUE ) })