diff --git a/R/expected-spectra.R b/R/expected-spectra.R index f5af2fe..3f1972e 100644 --- a/R/expected-spectra.R +++ b/R/expected-spectra.R @@ -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( @@ -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( @@ -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( diff --git a/R/plotting.R b/R/plotting.R index af44f5c..1f6a1b3 100644 --- a/R/plotting.R +++ b/R/plotting.R @@ -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") } diff --git a/man/eigs_sym.undirected_factor_model.Rd b/man/eigs_sym.undirected_factor_model.Rd index 65b0bad..7ef7a9f 100644 --- a/man/eigs_sym.undirected_factor_model.Rd +++ b/man/eigs_sym.undirected_factor_model.Rd @@ -72,3 +72,9 @@ following parameters: if \code{initvec} is close to an eigenvector of \eqn{A}.} } } +\note{ +\code{A} is on the Poisson scale (see \code{\link[=expectation]{expectation()}}). For Bernoulli +graphs (\code{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)}. +} diff --git a/man/expectation.Rd b/man/expectation.Rd index efbf78f..55c4d9a 100644 --- a/man/expectation.Rd +++ b/man/expectation.Rd @@ -19,9 +19,63 @@ object.} \item{...}{Unused.} } \value{ -The expected value of the adjacency matrix, conditional on the -latent factors \code{X} and \code{Y} (if the model is directed). +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 +\code{\link[Matrix:Matrix]{Matrix::Matrix()}}. This is on the \strong{Poisson scale}: it is the +expected adjacency matrix, conditional on the latent factors, when +edges are Poisson (\code{poisson_edges = TRUE}, the default), and not when +edges are Bernoulli. See "Bernoulli graphs" below. } \description{ Calculate the expected adjacency matrix } +\section{Bernoulli graphs}{ + + +When \code{poisson_edges = FALSE}, the model stores \code{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 \code{i} and \code{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 \code{expectation()}. So for +Bernoulli graphs, compute the expected adjacency matrix as +\code{1 - exp(-as.matrix(expectation(model)))}. Since +\eqn{1 - e^{-x} \le x}{1 - exp(-x) <= x}, \code{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 +\code{allow_self_loops = FALSE}, the diagonal of the expected adjacency matrix +is zero, whatever the edge distribution. + +The same Poisson scale applies to \code{\link[=expected_edges]{expected_edges()}}, +\code{\link[=expected_degree]{expected_degree()}}, \code{\link[=expected_density]{expected_density()}} and the population spectra +computed by \code{\link[=eigs_sym]{eigs_sym()}} and \code{\link[=svds]{svds()}}. +} + +\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))) + +} diff --git a/man/svds.directed_factor_model.Rd b/man/svds.directed_factor_model.Rd index c00b58f..1fa0c11 100644 --- a/man/svds.directed_factor_model.Rd +++ b/man/svds.directed_factor_model.Rd @@ -7,7 +7,7 @@ \method{svds}{directed_factor_model}(A, k = min(A$k1, A$k2), nu = k, nv = k, opts = list(), ...) } \arguments{ -\item{A}{An \code{\link[=undirected_factor_model]{undirected_factor_model()}}.} +\item{A}{A \code{\link[=directed_factor_model]{directed_factor_model()}}.} \item{k}{Desired rank of decomposition.} @@ -53,3 +53,9 @@ following parameters: Default is \code{FALSE}.} } } +\note{ +\code{A} is on the Poisson scale (see \code{\link[=expectation]{expectation()}}). For Bernoulli +graphs (\code{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)}. +} diff --git a/man/svds.undirected_factor_model.Rd b/man/svds.undirected_factor_model.Rd index fe144b7..84c62ce 100644 --- a/man/svds.undirected_factor_model.Rd +++ b/man/svds.undirected_factor_model.Rd @@ -53,3 +53,9 @@ following parameters: Default is \code{FALSE}.} } } +\note{ +\code{A} is on the Poisson scale (see \code{\link[=expectation]{expectation()}}). For Bernoulli +graphs (\code{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)}. +}