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
27 changes: 22 additions & 5 deletions R/directed_erdos_renyi.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
23 changes: 17 additions & 6 deletions R/directed_planted_partition.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
26 changes: 22 additions & 4 deletions R/undirected_erdos_renyi.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
43 changes: 40 additions & 3 deletions R/undirected_planted_partition.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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

Expand Down
17 changes: 17 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
9 changes: 6 additions & 3 deletions man/directed_erdos_renyi.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/directed_planted_partition.Rd

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

7 changes: 5 additions & 2 deletions man/erdos_renyi.Rd

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

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

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

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