diff --git a/R/directed_factor_model.R b/R/directed_factor_model.R index 0e084cd..732f164 100644 --- a/R/directed_factor_model.R +++ b/R/directed_factor_model.R @@ -107,7 +107,9 @@ validate_directed_factor_model <- function(x) { #' connection probability, and back-transform this `S` to the #' appropriate Poisson intensity parameter to approximate Bernoulli #' factor connection probabilities. See Section 2.3 of Rohe et al. (2017) -#' for some additional details. +#' for some additional details. In this case every element of `S` +#' (after any rescaling) must be less than one, since a probability of +#' exactly one corresponds to an infinite Poisson intensity. #' #' @param allow_self_loops Logical indicating whether or not #' nodes should be allowed to form edges with themselves. @@ -173,6 +175,10 @@ directed_factor_model <- function( poisson_edges = TRUE, allow_self_loops = TRUE ) { + # check these first: they are used below, before the validator runs + check_flag(poisson_edges, "poisson_edges") + check_flag(allow_self_loops, "allow_self_loops") + X <- Matrix(X) S <- Matrix(S) Y <- Matrix(Y) @@ -241,10 +247,12 @@ directed_factor_model <- function( # back-transform it to a Poisson parameterization of S. see section 2.3 # of the paper and issue #20 for details. - if (max(fm$S) > 1) { + # a probability of exactly 1 would be an infinite Poisson rate + if (max(fm$S) >= 1) { stop( - "Elements of `S` (after symmetrizing and scaling to achieve expected ", - "degree) must not exceed 1 for Bernoulli graphs.", + "Elements of `S` (after scaling to achieve expected degree or ", + "density) must be less than 1 for Bernoulli graphs ", + "(`poisson_edges = FALSE`).", call. = FALSE ) } diff --git a/R/undirected_factor_model.R b/R/undirected_factor_model.R index dea099e..4f0b19f 100644 --- a/R/undirected_factor_model.R +++ b/R/undirected_factor_model.R @@ -155,6 +155,10 @@ undirected_factor_model <- function( poisson_edges = TRUE, allow_self_loops = TRUE ) { + # check these first: they are used below, before the validator runs + check_flag(poisson_edges, "poisson_edges") + check_flag(allow_self_loops, "allow_self_loops") + X <- Matrix(X) S <- Matrix(S) @@ -206,10 +210,12 @@ undirected_factor_model <- function( # back-transform it to a Poisson parameterization of S. see section 2.3 # of the paper and issue #20 for details. - if (max(ufm$S) > 1) { + # a probability of exactly 1 would be an infinite Poisson rate + if (max(ufm$S) >= 1) { stop( "Elements of `S` (after symmetrizing and scaling to achieve expected ", - "degree) must not exceed 1 for Bernoulli graphs.", + "degree or density) must be less than 1 for Bernoulli graphs ", + "(`poisson_edges = FALSE`).", call. = FALSE ) } diff --git a/man/chung_lu.Rd b/man/chung_lu.Rd index 2916ea0..cb911fc 100644 --- a/man/chung_lu.Rd +++ b/man/chung_lu.Rd @@ -59,7 +59,9 @@ when \code{FALSE}, we assume that \code{S} specifies a desired between-factor connection probability, and back-transform this \code{S} to the appropriate Poisson intensity parameter to approximate Bernoulli factor connection probabilities. See Section 2.3 of Rohe et al. (2017) -for some additional details.} +for some additional details. In this case every element of \code{S} +(after any rescaling) must be less than one, since a probability of +exactly one corresponds to an infinite Poisson intensity.} \item{allow_self_loops}{Logical indicating whether or not nodes should be allowed to form edges with themselves. diff --git a/man/dcsbm.Rd b/man/dcsbm.Rd index 141bcd3..f93d72b 100644 --- a/man/dcsbm.Rd +++ b/man/dcsbm.Rd @@ -101,7 +101,9 @@ when \code{FALSE}, we assume that \code{S} specifies a desired between-factor connection probability, and back-transform this \code{S} to the appropriate Poisson intensity parameter to approximate Bernoulli factor connection probabilities. See Section 2.3 of Rohe et al. (2017) -for some additional details.} +for some additional details. In this case every element of \code{S} +(after any rescaling) must be less than one, since a probability of +exactly one corresponds to an infinite Poisson intensity.} \item{allow_self_loops}{Logical indicating whether or not nodes should be allowed to form edges with themselves. diff --git a/man/directed_dcsbm.Rd b/man/directed_dcsbm.Rd index de005df..8c8c5e4 100644 --- a/man/directed_dcsbm.Rd +++ b/man/directed_dcsbm.Rd @@ -197,7 +197,9 @@ when \code{FALSE}, we assume that \code{S} specifies a desired between-factor connection probability, and back-transform this \code{S} to the appropriate Poisson intensity parameter to approximate Bernoulli factor connection probabilities. See Section 2.3 of Rohe et al. (2017) -for some additional details.} +for some additional details. In this case every element of \code{S} +(after any rescaling) must be less than one, since a probability of +exactly one corresponds to an infinite Poisson intensity.} \item{allow_self_loops}{Logical indicating whether or not nodes should be allowed to form edges with themselves. diff --git a/man/directed_erdos_renyi.Rd b/man/directed_erdos_renyi.Rd index 9cf9941..49cf2a0 100644 --- a/man/directed_erdos_renyi.Rd +++ b/man/directed_erdos_renyi.Rd @@ -40,7 +40,9 @@ when \code{FALSE}, we assume that \code{S} specifies a desired between-factor connection probability, and back-transform this \code{S} to the appropriate Poisson intensity parameter to approximate Bernoulli factor connection probabilities. See Section 2.3 of Rohe et al. (2017) -for some additional details.} +for some additional details. In this case every element of \code{S} +(after any rescaling) must be less than one, since a probability of +exactly one corresponds to an infinite Poisson intensity.} \item{allow_self_loops}{Logical indicating whether or not nodes should be allowed to form edges with themselves. diff --git a/man/directed_factor_model.Rd b/man/directed_factor_model.Rd index d4ef91e..9f50506 100644 --- a/man/directed_factor_model.Rd +++ b/man/directed_factor_model.Rd @@ -53,7 +53,9 @@ when \code{FALSE}, we assume that \code{S} specifies a desired between-factor connection probability, and back-transform this \code{S} to the appropriate Poisson intensity parameter to approximate Bernoulli factor connection probabilities. See Section 2.3 of Rohe et al. (2017) -for some additional details.} +for some additional details. In this case every element of \code{S} +(after any rescaling) must be less than one, since a probability of +exactly one corresponds to an infinite Poisson intensity.} \item{allow_self_loops}{Logical indicating whether or not nodes should be allowed to form edges with themselves. diff --git a/man/directed_planted_partition.Rd b/man/directed_planted_partition.Rd index 762c2cc..0dbf628 100644 --- a/man/directed_planted_partition.Rd +++ b/man/directed_planted_partition.Rd @@ -114,7 +114,9 @@ when \code{FALSE}, we assume that \code{S} specifies a desired between-factor connection probability, and back-transform this \code{S} to the appropriate Poisson intensity parameter to approximate Bernoulli factor connection probabilities. See Section 2.3 of Rohe et al. (2017) -for some additional details.} +for some additional details. In this case every element of \code{S} +(after any rescaling) must be less than one, since a probability of +exactly one corresponds to an infinite Poisson intensity.} \item{allow_self_loops}{Logical indicating whether or not nodes should be allowed to form edges with themselves. diff --git a/man/erdos_renyi.Rd b/man/erdos_renyi.Rd index 08d59d3..75079c7 100644 --- a/man/erdos_renyi.Rd +++ b/man/erdos_renyi.Rd @@ -29,7 +29,9 @@ when \code{FALSE}, we assume that \code{S} specifies a desired between-factor connection probability, and back-transform this \code{S} to the appropriate Poisson intensity parameter to approximate Bernoulli factor connection probabilities. See Section 2.3 of Rohe et al. (2017) -for some additional details.} +for some additional details. In this case every element of \code{S} +(after any rescaling) must be less than one, since a probability of +exactly one corresponds to an infinite Poisson intensity.} \item{allow_self_loops}{Logical indicating whether or not nodes should be allowed to form edges with themselves. diff --git a/man/mmsbm.Rd b/man/mmsbm.Rd index 54be6bd..7b4774d 100644 --- a/man/mmsbm.Rd +++ b/man/mmsbm.Rd @@ -95,7 +95,9 @@ when \code{FALSE}, we assume that \code{S} specifies a desired between-factor connection probability, and back-transform this \code{S} to the appropriate Poisson intensity parameter to approximate Bernoulli factor connection probabilities. See Section 2.3 of Rohe et al. (2017) -for some additional details.} +for some additional details. In this case every element of \code{S} +(after any rescaling) must be less than one, since a probability of +exactly one corresponds to an infinite Poisson intensity.} \item{allow_self_loops}{Logical indicating whether or not nodes should be allowed to form edges with themselves. diff --git a/man/planted_partition.Rd b/man/planted_partition.Rd index ea63756..00d58b1 100644 --- a/man/planted_partition.Rd +++ b/man/planted_partition.Rd @@ -90,7 +90,9 @@ when \code{FALSE}, we assume that \code{S} specifies a desired between-factor connection probability, and back-transform this \code{S} to the appropriate Poisson intensity parameter to approximate Bernoulli factor connection probabilities. See Section 2.3 of Rohe et al. (2017) -for some additional details.} +for some additional details. In this case every element of \code{S} +(after any rescaling) must be less than one, since a probability of +exactly one corresponds to an infinite Poisson intensity.} \item{allow_self_loops}{Logical indicating whether or not nodes should be allowed to form edges with themselves. diff --git a/man/sbm.Rd b/man/sbm.Rd index 92e7761..5849a98 100644 --- a/man/sbm.Rd +++ b/man/sbm.Rd @@ -80,7 +80,9 @@ when \code{FALSE}, we assume that \code{S} specifies a desired between-factor connection probability, and back-transform this \code{S} to the appropriate Poisson intensity parameter to approximate Bernoulli factor connection probabilities. See Section 2.3 of Rohe et al. (2017) -for some additional details.} +for some additional details. In this case every element of \code{S} +(after any rescaling) must be less than one, since a probability of +exactly one corresponds to an infinite Poisson intensity.} \item{allow_self_loops}{Logical indicating whether or not nodes should be allowed to form edges with themselves. diff --git a/man/undirected_factor_model.Rd b/man/undirected_factor_model.Rd index 1c89ee0..491646b 100644 --- a/man/undirected_factor_model.Rd +++ b/man/undirected_factor_model.Rd @@ -44,7 +44,9 @@ when \code{FALSE}, we assume that \code{S} specifies a desired between-factor connection probability, and back-transform this \code{S} to the appropriate Poisson intensity parameter to approximate Bernoulli factor connection probabilities. See Section 2.3 of Rohe et al. (2017) -for some additional details.} +for some additional details. In this case every element of \code{S} +(after any rescaling) must be less than one, since a probability of +exactly one corresponds to an infinite Poisson intensity.} \item{allow_self_loops}{Logical indicating whether or not nodes should be allowed to form edges with themselves. diff --git a/tests/testthat/test-bernoulli-probability-one.R b/tests/testthat/test-bernoulli-probability-one.R new file mode 100644 index 0000000..c23cf73 --- /dev/null +++ b/tests/testthat/test-bernoulli-probability-one.R @@ -0,0 +1,65 @@ +test_that("Bernoulli models with an edge probability of 1 error clearly", { + msg <- "must be less than 1 for Bernoulli graphs" + + expect_error( + planted_partition( + n = 10, k = 2, within_block = 1, between_block = 0.1, + poisson_edges = FALSE + ), + msg + ) + expect_error( + sbm(n = 10, B = matrix(c(1, .1, .1, .5), 2), poisson_edges = FALSE), + msg + ) + expect_error( + undirected_factor_model(matrix(1, 5, 1), matrix(1), poisson_edges = FALSE), + msg + ) + expect_error( + directed_factor_model( + matrix(1, 5, 1), matrix(1), matrix(1, 4, 1), + poisson_edges = FALSE + ), + msg + ) + expect_error( + directed_planted_partition( + n = 10, k = 2, within_block = 1, between_block = 0.1, + poisson_edges = FALSE + ), + msg + ) + # rescaling to a probability of 1 + expect_error( + erdos_renyi(n = 10, expected_degree = 10, poisson_edges = FALSE), + msg + ) + + # Poisson rates of 1 are fine + m <- sbm(n = 10, B = matrix(c(1, .1, .1, .5), 2)) + expect_true(all(is.finite(as.matrix(m$S)))) +}) + +test_that("poisson_edges and allow_self_loops must be TRUE or FALSE", { + for (bad in list("yes", NA, c(TRUE, FALSE), 1)) { + expect_error( + undirected_factor_model(matrix(1, 5, 1), matrix(.1), poisson_edges = bad), + "`poisson_edges` must be `TRUE` or `FALSE`.", + fixed = TRUE + ) + expect_error( + directed_factor_model( + matrix(1, 5, 1), matrix(.1), matrix(1, 4, 1), + poisson_edges = bad + ), + "`poisson_edges` must be `TRUE` or `FALSE`.", + fixed = TRUE + ) + expect_error( + sbm(n = 10, B = diag(2) * .5, allow_self_loops = bad), + "`allow_self_loops` must be `TRUE` or `FALSE`.", + fixed = TRUE + ) + } +})