Skip to content
Merged
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
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# fastRG (development version)

- **Breaking change:** fixed `expected_edges()` and `expected_density()` double counting edges in undirected models. Since 0.4.0 fixed the edge over-sampling in #19, both have been twice the true value, because each undirected edge appears in `A` twice (as `A[i, j]` and `A[j, i]`). As a result, **specifying `expected_density = d` for any undirected model produced graphs with density about `d / 2`**, and print methods reported twice the number of edges actually sampled.
- `expected_edges()` is now the expected number of undirected edges, counting self-loops once: `(sum(P) + trace(P)) / 2` for `P = X S X'`.
- `expected_density()` is now the expected number of edges between distinct nodes divided by `choose(n, 2)`: `(sum(P) - trace(P)) / (n (n - 1))`.
- `expected_degree()`, `expected_degrees()`, sampling and population spectra were already correct and are unchanged, as are all directed models.
- Undirected models built with `expected_density` now sample twice as many edges as before, so graphs sampled with a given seed will differ.

- `erdos_renyi()` and `directed_erdos_renyi()` now error when neither `p` nor a scaling argument such as `expected_degree` or `expected_density` is given. Previously the check never fired, because it looked up the generic functions `expected_degree()` (or `expected_in_degree()` / `expected_out_degree()`) instead of the arguments, and the model silently used `p = 0.5`.

- `sample_edgelist()`, `sample_igraph()` and `sample_tidygraph()` gain a `multigraph` argument. The default, `multigraph = TRUE`, keeps the current behavior: Poisson graphs are returned as multigraphs with repeated edges. With `multigraph = FALSE`, repeated edges are collapsed into a single edge with an integer `weight` attribute (an edgelist column, or an `igraph` edge attribute) that equals the corresponding entry of `sample_sparse()`. In undirected graphs `(i, j)` and `(j, i)` are collapsed together. The `weight` attribute is always present when `multigraph = FALSE`, so Bernoulli graphs (`poisson_edges = FALSE`) get weights of 1 (#30).

- Fixed `sample_sparse()`, `sample_igraph()` and `sample_tidygraph()` erroring on directed rectangular models (`n != d`) when no edges are sampled. Empty graphs now have the same output types as non-empty ones: `sample_edgelist()` returns a tibble with integer `from` and `to` columns instead of a base matrix of doubles, and `sample_sparse()` returns a symmetric `dsCMatrix` for undirected models.
Expand Down
15 changes: 9 additions & 6 deletions R/directed_erdos_renyi.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ 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`, or `expected_out_degree`.
#' either `p`, `expected_in_degree`, `expected_out_degree` or
#' `expected_density`.
#'
#' @inheritDotParams directed_factor_model expected_in_degree expected_out_degree
#' @inherit directed_factor_model params return
Expand Down Expand Up @@ -77,12 +78,14 @@ directed_erdos_renyi <- function(
X <- Matrix(1, nrow = n, ncol = 1)
Y <- Matrix(1, nrow = n, ncol = 1)

if (
is.null(p) && is.null(expected_in_degree) && is.null(expected_out_degree)
) {
# the scaling arguments arrive via `...`, so look for them by name (a
# bare `expected_in_degree` would find the generic function)
scaling <- c("expected_in_degree", "expected_out_degree", "expected_density")

if (is.null(p) && !any(scaling %in% names(list(...)))) {
stop(
"Must specify either `p`, `expected_in_degree` or ",
" `expected_out_degree`.",
"Must specify either `p`, `expected_in_degree`, ",
"`expected_out_degree` or `expected_density`.",
call. = FALSE
)
}
Expand Down
73 changes: 60 additions & 13 deletions R/expected-degrees.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,44 @@
#'
#' @inherit sample_edgelist references params
#'
#' @return Expected edge counts, or graph densities.
#'
#' @details Note that the runtime of the `fastRG` algorithm is proportional to
#' the expected number of edges in the graph. Expected edge count will be
#' an underestimate of expected number of edges for Bernoulli
#' graphs. See the Rohe et al for details.
#' @return Expected edge counts, degrees, or graph densities.
#'
#' @details Write \eqn{P = \mathbb{E}[A \mid X, S] = X S X^T}{P = E[A | X, S] = X S X'}
#' for an undirected factor model, and
#' \eqn{P = X S Y^T}{P = X S Y'} for a directed one.
#'
#' For **undirected** graphs, every edge between distinct nodes `i` and
#' `j` appears twice in the symmetric adjacency matrix `A`, as both
#' `A[i, j]` and `A[j, i]`, while a self-loop appears once, as `A[i, i]`.
#' Accordingly:
#'
#' - `expected_edges()` is the expected number of edges, counting each
#' undirected edge once: \eqn{(\sum_{ij} P_{ij} + \mathrm{tr}(P)) / 2}{(sum(P) + trace(P)) / 2}.
#' This includes expected self-loops, even when
#' `allow_self_loops = FALSE`.
#' - `expected_degree()` is the expected mean degree, i.e. the average
#' row sum of \eqn{P}{P}: \eqn{\sum_{ij} P_{ij} / n}{sum(P) / n}.
#' `expected_degrees()` gives the expected degree of each node.
#' - `expected_density()` is the expected number of edges between
#' distinct nodes, divided by the number of pairs of distinct nodes:
#' \eqn{(\sum_{ij} P_{ij} - \mathrm{tr}(P)) / (n (n - 1))}{(sum(P) - trace(P)) / (n (n - 1))}.
#' This is the average edge probability (or rate) between distinct
#' nodes, and does not depend on self-loops.
#'
#' For **directed** graphs, `expected_edges()` is
#' \eqn{\sum_{ij} P_{ij}}{sum(P)}, `expected_out_degree()` and
#' `expected_in_degree()` divide this by the number of rows and columns
#' of `A`, and `expected_density()` divides it by the number of entries
#' of `A`.
#'
#' These are expectations under Poisson edges. For Bernoulli graphs
#' (`poisson_edges = FALSE`), they are computed from the Poisson
#' parameterization of `S`, and overestimate edge counts slightly when
#' edge probabilities are large.
#'
#' Note that the runtime of the `fastRG` algorithm is proportional to
#' the expected number of edges in the graph. See Rohe et al. (2018) for
#' details.
#'
#' @export
#'
Expand All @@ -32,7 +64,7 @@
#' A <- sample_sparse(b_model)
#'
#' # compare
#' mean(rowSums(triu(A)))
#' mean(rowSums(A))
#'
#' pop * a + pop * b # analytical average degree
#'
Expand Down Expand Up @@ -86,11 +118,22 @@ expected_edges.directed_factor_model <- function(factor_model, ...) {

#' @export
expected_edges.undirected_factor_model <- function(factor_model, ...) {
X <- factor_model$X
S <- factor_model$S
# each edge between distinct nodes is counted twice in sum(P), once as
# P[i, j] and once as P[j, i], but self-loops are only counted once (#19)
(expected_sum(factor_model) + expected_trace(factor_model)) / 2
}

Cx <- Diagonal(n = ncol(X), x = colSums(X))
sum(Cx %*% S %*% Cx)
# sum of all entries of P = E[A | X, S] = X S X', without forming P
expected_sum <- function(factor_model) {
cx <- colSums(factor_model$X)
as.numeric(crossprod(cx, factor_model$S %*% cx))
}

# trace of P = E[A | X, S] = X S X', i.e. the expected number of
# self-loops, without forming P
expected_trace <- function(factor_model) {
X <- factor_model$X
sum((X %*% factor_model$S) * X)
}

#' @rdname expected_edges
Expand Down Expand Up @@ -137,12 +180,16 @@ expected_density <- function(factor_model, ...) {

#' @export
expected_degree.undirected_factor_model <- function(factor_model, ...) {
expected_edges(factor_model) / as.numeric(factor_model$n)
# mean row sum of P, i.e. the mean expected degree
expected_sum(factor_model) / as.numeric(factor_model$n)
}

#' @export
expected_density.undirected_factor_model <- function(factor_model, ...) {
expected_edges(factor_model) / as.numeric(choose(factor_model$n, 2))
# mean of P[i, j] over pairs of distinct nodes i != j
n <- as.numeric(factor_model$n)
off_diagonal <- expected_sum(factor_model) - expected_trace(factor_model)
off_diagonal / (n * (n - 1))
}

#' @export
Expand Down
13 changes: 10 additions & 3 deletions R/undirected_erdos_renyi.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ 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` or `expected_degree`.
#' either `p`, `expected_degree` or `expected_density`.
#'
#' @inheritDotParams undirected_factor_model expected_degree
#' @inherit undirected_factor_model params return
Expand Down Expand Up @@ -64,8 +64,15 @@ erdos_renyi <- function(
) {
X <- Matrix(1, nrow = n, ncol = 1)

if (is.null(p) && is.null(expected_degree)) {
stop("Must specify either `p` or `expected_degree`.", call. = FALSE)
# `expected_degree` and `expected_density` arrive via `...`, so look for
# them by name (a bare `expected_degree` would find the generic function)
scaling <- c("expected_degree", "expected_density")

if (is.null(p) && !any(scaling %in% names(list(...)))) {
stop(
"Must specify either `p`, `expected_degree` or `expected_density`.",
call. = FALSE
)
}

if (is.null(p)) {
Expand Down
4 changes: 3 additions & 1 deletion R/undirected_factor_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ validate_undirected_factor_model <- function(x) {
#' `expected_degree` and `expected_density` at the same time.
#'
#' @param expected_density If specified, the desired expected density
#' of the graph. Specifying `expected_density` simply rescales `S`
#' of the graph, that is, the expected number of edges between distinct
#' nodes divided by `choose(n, 2)`. See [expected_density()].
#' Specifying `expected_density` simply rescales `S`
#' to achieve this. Defaults to `NULL`. Do not specify both
#' `expected_degree` and `expected_density` at the same time.
#'
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.

3 changes: 2 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.

2 changes: 1 addition & 1 deletion man/erdos_renyi.Rd

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

43 changes: 38 additions & 5 deletions man/expected_edges.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/overlapping_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/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.

Loading
Loading