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
17 changes: 16 additions & 1 deletion R/expected-spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ RSpectra::eigs_sym
#' @inherit RSpectra::eigs_sym params details
#' @param ... Unused, included only for consistency with generic signature.
#'
#' @note `A` is on the Poisson scale (see [expectation()]). For Bernoulli
#' graphs (`poisson_edges = FALSE`), these are the spectra of the matrix
#' of Poisson rates \eqn{P}{P}, not of the expected adjacency matrix
#' \eqn{1 - \exp(-P)}{1 - exp(-P)}.
#'
#' @method eigs_sym undirected_factor_model
#' @export
eigs_sym.undirected_factor_model <- function(
Expand All @@ -37,6 +42,11 @@ eigs_sym.undirected_factor_model <- function(
#' @inherit RSpectra::svds params details
#' @param ... Unused, included only for consistency with generic signature.
#'
#' @note `A` is on the Poisson scale (see [expectation()]). For Bernoulli
#' graphs (`poisson_edges = FALSE`), these are the spectra of the matrix
#' of Poisson rates \eqn{P}{P}, not of the expected adjacency matrix
#' \eqn{1 - \exp(-P)}{1 - exp(-P)}.
#'
#' @method svds undirected_factor_model
#' @export
svds.undirected_factor_model <- function(
Expand Down Expand Up @@ -72,11 +82,16 @@ svds.undirected_factor_model <- function(

#' Compute the singular value decomposition of the expected adjacency matrix of a directed factor model
#'
#' @param A An [undirected_factor_model()].
#' @param A A [directed_factor_model()].
#' @param k Desired rank of decomposition.
#' @inherit RSpectra::svds params details
#' @param ... Unused, included only for consistency with generic signature.
#'
#' @note `A` is on the Poisson scale (see [expectation()]). For Bernoulli
#' graphs (`poisson_edges = FALSE`), these are the spectra of the matrix
#' of Poisson rates \eqn{P}{P}, not of the expected adjacency matrix
#' \eqn{1 - \exp(-P)}{1 - exp(-P)}.
#'
#' @method svds directed_factor_model
#' @export
svds.directed_factor_model <- function(
Expand Down
56 changes: 54 additions & 2 deletions R/plotting.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,62 @@
#' object.
#' @param ... Unused.
#'
#' @return The expected value of the adjacency matrix, conditional on the
#' latent factors `X` and `Y` (if the model is directed).
#' @return The matrix \eqn{P = X S X^T}{P = X S X'} (undirected models) or
#' \eqn{P = X S Y^T}{P = X S Y'} (directed models), as a
#' [Matrix::Matrix()]. This is on the **Poisson scale**: it is the
#' expected adjacency matrix, conditional on the latent factors, when
#' edges are Poisson (`poisson_edges = TRUE`, the default), and not when
#' edges are Bernoulli. See "Bernoulli graphs" below.
#'
#' @section Bernoulli graphs:
#'
#' When `poisson_edges = FALSE`, the model stores `S` on the Poisson scale:
#' the requested Bernoulli edge probabilities are transformed to Poisson
#' rates via \eqn{-\log(1 - p)}{-log(1 - p)} when the model is created.
#' Sampling then draws a Poisson number of edges for each pair of nodes and
#' keeps a single edge whenever that number is positive, so the probability
#' of an edge between nodes `i` and `j` is
#'
#' \deqn{
#' \mathbb{E}[A_{ij} \mid X, S] = P(A_{ij} = 1 \mid X, S) = 1 - \exp(-P_{ij}),
#' }{
#' E[A[i, j] | X, S] = P(A[i, j] = 1 | X, S) = 1 - exp(-P[i, j]),
#' }
#'
#' where \eqn{P}{P} is the matrix returned by `expectation()`. So for
#' Bernoulli graphs, compute the expected adjacency matrix as
#' `1 - exp(-as.matrix(expectation(model)))`. Since
#' \eqn{1 - e^{-x} \le x}{1 - exp(-x) <= x}, `expectation()` overestimates
#' edge probabilities for Bernoulli graphs, and the difference is small
#' when edge probabilities are small. For blockmodels, where each node
#' belongs to a single block with no degree heterogeneity, the result
#' recovers the requested probabilities exactly. If
#' `allow_self_loops = FALSE`, the diagonal of the expected adjacency matrix
#' is zero, whatever the edge distribution.
#'
#' The same Poisson scale applies to [expected_edges()],
#' [expected_degree()], [expected_density()] and the population spectra
#' computed by [eigs_sym()] and [svds()].
#'
#' @export
#'
#' @examples
#'
#' set.seed(27)
#'
#' B <- matrix(c(0.6, 0.1, 0.1, 0.4), nrow = 2)
#'
#' # Poisson edges: expectation() is E[A | X, S]
#' poisson_sbm <- sbm(n = 6, B = B, block_sizes = c(3, 3))
#' expectation(poisson_sbm)
#'
#' # Bernoulli edges: expectation() is on the Poisson scale
#' bernoulli_sbm <- sbm(n = 6, B = B, block_sizes = c(3, 3), poisson_edges = FALSE)
#' expectation(bernoulli_sbm)
#'
#' # the expected adjacency matrix, which recovers B for a blockmodel
#' 1 - exp(-as.matrix(expectation(bernoulli_sbm)))
#'
expectation <- function(model, ...) {
UseMethod("expectation")
}
Expand Down
6 changes: 6 additions & 0 deletions man/eigs_sym.undirected_factor_model.Rd

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

58 changes: 56 additions & 2 deletions man/expectation.Rd

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

8 changes: 7 additions & 1 deletion man/svds.directed_factor_model.Rd

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

6 changes: 6 additions & 0 deletions man/svds.undirected_factor_model.Rd

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

Loading