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
16 changes: 12 additions & 4 deletions R/directed_factor_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
)
}
Expand Down
10 changes: 8 additions & 2 deletions R/undirected_factor_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
)
}
Expand Down
4 changes: 3 additions & 1 deletion man/chung_lu.Rd

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

4 changes: 3 additions & 1 deletion man/dcsbm.Rd

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

4 changes: 3 additions & 1 deletion man/directed_dcsbm.Rd

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

4 changes: 3 additions & 1 deletion man/directed_erdos_renyi.Rd

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

4 changes: 3 additions & 1 deletion man/directed_factor_model.Rd

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

4 changes: 3 additions & 1 deletion man/directed_planted_partition.Rd

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

4 changes: 3 additions & 1 deletion man/erdos_renyi.Rd

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

4 changes: 3 additions & 1 deletion man/mmsbm.Rd

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

4 changes: 3 additions & 1 deletion man/planted_partition.Rd

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

4 changes: 3 additions & 1 deletion man/sbm.Rd

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

4 changes: 3 additions & 1 deletion man/undirected_factor_model.Rd

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

65 changes: 65 additions & 0 deletions tests/testthat/test-bernoulli-probability-one.R
Original file line number Diff line number Diff line change
@@ -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
)
}
})
Loading