diff --git a/R/directed_dcsbm.R b/R/directed_dcsbm.R index eb3a185..fcec43e 100644 --- a/R/directed_dcsbm.R +++ b/R/directed_dcsbm.R @@ -41,14 +41,14 @@ validate_directed_dcsbm <- function(x) { if (length(levels(values$z_out)) != values$k1) { stop( - "Number of levels of `z1` must match the incoming rank of the model.", + "Number of levels of `z_out` must match the outgoing rank of the model.", call. = FALSE ) } if (length(levels(values$z_in)) != values$k2) { stop( - "Number of levels of `z_in` must match the outgoing rank of the model.", + "Number of levels of `z_in` must match the incoming rank of the model.", call. = FALSE ) } @@ -107,14 +107,14 @@ validate_directed_dcsbm <- function(x) { if (length(values$pi_out) != values$k1) { stop( - "Dimension of `pi_out` must match the incoming rank of the model.", + "Dimension of `pi_out` must match the outgoing rank of the model.", call. = FALSE ) } if (length(values$pi_in) != values$k2) { stop( - "Dimension of `pi_in` must match the outgoing rank of the model.", + "Dimension of `pi_in` must match the incoming rank of the model.", call. = FALSE ) } @@ -353,8 +353,8 @@ sample_block_memberships <- function(n, k, pi, block_sizes = NULL) { #' decreasing `theta_out` and `theta_in` within each block. #' #' @param force_identifiability Logical indicating whether or not to -#' normalize `theta_out` such that it sums to one within each incoming -#' block and `theta_in` such that it sums to one within each outgoing +#' normalize `theta_out` such that it sums to one within each outgoing +#' block and `theta_in` such that it sums to one within each incoming #' block. Defaults to `TRUE`. #' #' @inheritDotParams directed_factor_model expected_out_degree expected_density expected_in_degree @@ -409,8 +409,8 @@ sample_block_memberships <- function(n, k, pi, block_sizes = NULL) { #' # Generative Model #' #' There are two levels of randomness in a directed degree-corrected -#' stochastic blockmodel. First, we randomly chose a incoming -#' block membership and an outgoing block membership +#' stochastic blockmodel. First, we randomly choose an outgoing +#' block membership and an incoming block membership #' for each node in the blockmodel. This is #' handled by `directed_dcsbm()`. Then, given these block memberships, #' we randomly sample edges between nodes. This second @@ -421,13 +421,13 @@ sample_block_memberships <- function(n, k, pi, block_sizes = NULL) { #' #' ## Block memberships #' -#' Let \eqn{x} represent the incoming block membership of a node -#' and \eqn{y} represent the outgoing block membership of a node. +#' Let \eqn{x} represent the outgoing block membership of a node +#' and \eqn{y} represent the incoming block membership of a node. #' To generate \eqn{x} we sample from a categorical #' distribution with parameter \eqn{\pi_{out}}. #' To generate \eqn{y} we sample from a categorical #' distribution with parameter \eqn{\pi_{in}}. -#' Block memberships are independent across nodes. Incoming and outgoing +#' Block memberships are independent across nodes. Outgoing and incoming #' block memberships of the same node are also independent. #' #' These categorical draws are used when `pi_out` (`pi_in`) is specified. @@ -442,12 +442,12 @@ sample_block_memberships <- function(n, k, pi, block_sizes = NULL) { #' #' ## Degree heterogeneity #' -#' In addition to block membership, the DCSBM also -#' nodes to have different propensities for incoming and -#' outgoing edge formation. -#' We represent the propensity to form incoming edges for a -#' given node by a positive number \eqn{\theta_{out}}. +#' In addition to block membership, the DCSBM also allows +#' nodes to have different propensities for outgoing and +#' incoming edge formation. #' We represent the propensity to form outgoing edges for a +#' given node by a positive number \eqn{\theta_{out}}. +#' We represent the propensity to form incoming edges for a #' given node by a positive number \eqn{\theta_{in}}. #' Typically the \eqn{\theta_{out}} (and \eqn{\theta_{in}}) across all nodes are #' constrained to sum to one for identifiability purposes, @@ -456,15 +456,18 @@ sample_block_memberships <- function(n, k, pi, block_sizes = NULL) { #' ## Edge formulation #' #' Once we know the block memberships \eqn{x} and \eqn{y} -#' and the degree heterogeneity parameters \eqn{\theta_{in}} and -#' \eqn{\theta_{out}}, we need one more +#' and the degree heterogeneity parameters \eqn{\theta_{out}} and +#' \eqn{\theta_{in}}, we need one more #' ingredient, which is the baseline intensity of connections -#' between nodes in block `i` and block `j`. Then each edge forms -#' independently according to a Poisson distribution with -#' parameters +#' from nodes in outgoing block `a` to nodes in incoming block `b`, +#' `B[a, b]`. Then each edge \eqn{A_{ij}} from node \eqn{i} to node +#' \eqn{j} forms independently according to a Poisson distribution with +#' parameter #' #' \deqn{ -#' \lambda = \theta_{in} \cdot B_{x, y} \cdot \theta_{out}. +#' \lambda_{ij} = \theta_{out, i} \cdot B_{x_i, y_j} \cdot \theta_{in, j}. +#' }{ +#' lambda[i, j] = theta_out[i] * B[x[i], y[j]] * theta_in[j]. #' } #' #' # Co-factor models versus shared blocks and degree heterogeneity diff --git a/R/directed_factor_model.R b/R/directed_factor_model.R index 0e084cd..01e99c3 100644 --- a/R/directed_factor_model.R +++ b/R/directed_factor_model.R @@ -73,14 +73,16 @@ validate_directed_factor_model <- function(x) { #' #' @param X A [matrix()] or [Matrix::Matrix()] representing real-valued #' latent node positions encoding community structure of -#' incoming edges. Entries must be positive. +#' outgoing edges, one row per row of `A` (sending node). Entries must +#' be positive. #' #' @param S A [matrix()] or [Matrix::Matrix()] mixing matrix. Entries #' must be positive. #' #' @param Y A [matrix()] or [Matrix::Matrix()] representing real-valued #' latent node positions encoding community structure of -#' outgoing edges. Entries must be positive. +#' incoming edges, one row per column of `A` (receiving node). Entries +#' must be positive. #' #' @param ... Ignored. For internal developer use only. #' @@ -117,23 +119,25 @@ validate_directed_factor_model <- function(x) { #' @return A `directed_factor_model` S3 class based on a list #' with the following elements: #' -#' - `X`: The incoming latent positions as a [Matrix::Matrix()] object. +#' - `X`: The outgoing latent positions as a [Matrix::Matrix()] object. #' #' - `S`: The mixing matrix as a [Matrix::Matrix()] object. #' -#' - `Y`: The outgoing latent positions as a [Matrix::Matrix()] object. +#' - `Y`: The incoming latent positions as a [Matrix::Matrix()] object. #' -#' - `n`: The number of nodes with incoming edges in the network. +#' - `n`: The number of nodes with outgoing edges in the network +#' (rows of `A`). #' #' - `k1`: The dimension of the latent node position vectors -#' encoding incoming latent communities (i.e. in `X`). +#' encoding outgoing latent communities (i.e. in `X`). #' -#' - `d`: The number of nodes with outgoing edges in the network. +#' - `d`: The number of nodes with incoming edges in the network +#' (columns of `A`). #' Does not need to match `n` -- rectangular adjacency matrices #' are supported. #' #' - `k2`: The dimension of the latent node position vectors -#' encoding outgoing latent communities (i.e. in `Y`). +#' encoding incoming latent communities (i.e. in `Y`). #' #' - `poisson_edges`: Whether or not the graph is taken to be have #' Poisson or Bernoulli edges, as indicated by a logical vector @@ -269,10 +273,10 @@ print.directed_factor_model <- function(x, ...) { cat(glue("Directed Factor Model\n", .trim = FALSE)) cat(glue("---------------------\n\n", .trim = FALSE)) - cat(glue("Incoming Nodes (n): {x$n}\n", .trim = FALSE)) - cat(glue("Incoming Rank (k1): {x$k1}\n", .trim = FALSE)) - cat(glue("Outgoing Rank (k2): {x$k2}\n", .trim = FALSE)) - cat(glue("Outgoing Nodes (d): {x$d}\n\n", .trim = FALSE)) + cat(glue("Outgoing Nodes (n): {x$n}\n", .trim = FALSE)) + cat(glue("Outgoing Rank (k1): {x$k1}\n", .trim = FALSE)) + cat(glue("Incoming Rank (k2): {x$k2}\n", .trim = FALSE)) + cat(glue("Incoming Nodes (d): {x$d}\n\n", .trim = FALSE)) cat("X:", dim_and_class(x$X), "\n") cat("S:", dim_and_class(x$S), "\n") diff --git a/man/directed_dcsbm.Rd b/man/directed_dcsbm.Rd index de005df..a641a5e 100644 --- a/man/directed_dcsbm.Rd +++ b/man/directed_dcsbm.Rd @@ -185,8 +185,8 @@ matrix refer to the same node: nodes are sorted by block, and then by decreasing \code{theta_out} and \code{theta_in} within each block.} \item{force_identifiability}{Logical indicating whether or not to -normalize \code{theta_out} such that it sums to one within each incoming -block and \code{theta_in} such that it sums to one within each outgoing +normalize \code{theta_out} such that it sums to one within each outgoing +block and \code{theta_in} such that it sums to one within each incoming block. Defaults to \code{TRUE}.} \item{poisson_edges}{Logical indicating whether or not @@ -258,8 +258,8 @@ sampling large, dense graphs. } \section{Generative Model}{ There are two levels of randomness in a directed degree-corrected -stochastic blockmodel. First, we randomly chose a incoming -block membership and an outgoing block membership +stochastic blockmodel. First, we randomly choose an outgoing +block membership and an incoming block membership for each node in the blockmodel. This is handled by \code{directed_dcsbm()}. Then, given these block memberships, we randomly sample edges between nodes. This second @@ -269,13 +269,13 @@ operation is handled by \code{\link[=sample_edgelist]{sample_edgelist()}}, graph representation. \subsection{Block memberships}{ -Let \eqn{x} represent the incoming block membership of a node -and \eqn{y} represent the outgoing block membership of a node. +Let \eqn{x} represent the outgoing block membership of a node +and \eqn{y} represent the incoming block membership of a node. To generate \eqn{x} we sample from a categorical distribution with parameter \eqn{\pi_{out}}. To generate \eqn{y} we sample from a categorical distribution with parameter \eqn{\pi_{in}}. -Block memberships are independent across nodes. Incoming and outgoing +Block memberships are independent across nodes. Outgoing and incoming block memberships of the same node are also independent. These categorical draws are used when \code{pi_out} (\code{pi_in}) is specified. @@ -291,12 +291,12 @@ values. \subsection{Degree heterogeneity}{ -In addition to block membership, the DCSBM also -nodes to have different propensities for incoming and -outgoing edge formation. -We represent the propensity to form incoming edges for a -given node by a positive number \eqn{\theta_{out}}. +In addition to block membership, the DCSBM also allows +nodes to have different propensities for outgoing and +incoming edge formation. We represent the propensity to form outgoing edges for a +given node by a positive number \eqn{\theta_{out}}. +We represent the propensity to form incoming edges for a given node by a positive number \eqn{\theta_{in}}. Typically the \eqn{\theta_{out}} (and \eqn{\theta_{in}}) across all nodes are constrained to sum to one for identifiability purposes, @@ -306,15 +306,18 @@ but this doesn't really matter during sampling. \subsection{Edge formulation}{ Once we know the block memberships \eqn{x} and \eqn{y} -and the degree heterogeneity parameters \eqn{\theta_{in}} and -\eqn{\theta_{out}}, we need one more +and the degree heterogeneity parameters \eqn{\theta_{out}} and +\eqn{\theta_{in}}, we need one more ingredient, which is the baseline intensity of connections -between nodes in block \code{i} and block \code{j}. Then each edge forms -independently according to a Poisson distribution with -parameters +from nodes in outgoing block \code{a} to nodes in incoming block \code{b}, +\code{B[a, b]}. Then each edge \eqn{A_{ij}} from node \eqn{i} to node +\eqn{j} forms independently according to a Poisson distribution with +parameter \deqn{ - \lambda = \theta_{in} \cdot B_{x, y} \cdot \theta_{out}. + \lambda_{ij} = \theta_{out, i} \cdot B_{x_i, y_j} \cdot \theta_{in, j}. +}{ + lambda[i, j] = theta_out[i] * B[x[i], y[j]] * theta_in[j]. } } } diff --git a/man/directed_erdos_renyi.Rd b/man/directed_erdos_renyi.Rd index 9cf9941..7346d60 100644 --- a/man/directed_erdos_renyi.Rd +++ b/man/directed_erdos_renyi.Rd @@ -51,17 +51,19 @@ self-loops, and these are then removed after the fact.} A \code{directed_factor_model} S3 class based on a list with the following elements: \itemize{ -\item \code{X}: The incoming latent positions as a \code{\link[Matrix:Matrix]{Matrix::Matrix()}} object. +\item \code{X}: The outgoing latent positions as a \code{\link[Matrix:Matrix]{Matrix::Matrix()}} object. \item \code{S}: The mixing matrix as a \code{\link[Matrix:Matrix]{Matrix::Matrix()}} object. -\item \code{Y}: The outgoing latent positions as a \code{\link[Matrix:Matrix]{Matrix::Matrix()}} object. -\item \code{n}: The number of nodes with incoming edges in the network. +\item \code{Y}: The incoming latent positions as a \code{\link[Matrix:Matrix]{Matrix::Matrix()}} object. +\item \code{n}: The number of nodes with outgoing edges in the network +(rows of \code{A}). \item \code{k1}: The dimension of the latent node position vectors -encoding incoming latent communities (i.e. in \code{X}). -\item \code{d}: The number of nodes with outgoing edges in the network. +encoding outgoing latent communities (i.e. in \code{X}). +\item \code{d}: The number of nodes with incoming edges in the network +(columns of \code{A}). Does not need to match \code{n} -- rectangular adjacency matrices are supported. \item \code{k2}: The dimension of the latent node position vectors -encoding outgoing latent communities (i.e. in \code{Y}). +encoding incoming latent communities (i.e. in \code{Y}). \item \code{poisson_edges}: Whether or not the graph is taken to be have Poisson or Bernoulli edges, as indicated by a logical vector of length 1. diff --git a/man/directed_factor_model.Rd b/man/directed_factor_model.Rd index d4ef91e..c530187 100644 --- a/man/directed_factor_model.Rd +++ b/man/directed_factor_model.Rd @@ -19,14 +19,16 @@ directed_factor_model( \arguments{ \item{X}{A \code{\link[=matrix]{matrix()}} or \code{\link[Matrix:Matrix]{Matrix::Matrix()}} representing real-valued latent node positions encoding community structure of -incoming edges. Entries must be positive.} +outgoing edges, one row per row of \code{A} (sending node). Entries must +be positive.} \item{S}{A \code{\link[=matrix]{matrix()}} or \code{\link[Matrix:Matrix]{Matrix::Matrix()}} mixing matrix. Entries must be positive.} \item{Y}{A \code{\link[=matrix]{matrix()}} or \code{\link[Matrix:Matrix]{Matrix::Matrix()}} representing real-valued latent node positions encoding community structure of -outgoing edges. Entries must be positive.} +incoming edges, one row per column of \code{A} (receiving node). Entries +must be positive.} \item{...}{Ignored. For internal developer use only.} @@ -64,17 +66,19 @@ self-loops, and these are then removed after the fact.} A \code{directed_factor_model} S3 class based on a list with the following elements: \itemize{ -\item \code{X}: The incoming latent positions as a \code{\link[Matrix:Matrix]{Matrix::Matrix()}} object. +\item \code{X}: The outgoing latent positions as a \code{\link[Matrix:Matrix]{Matrix::Matrix()}} object. \item \code{S}: The mixing matrix as a \code{\link[Matrix:Matrix]{Matrix::Matrix()}} object. -\item \code{Y}: The outgoing latent positions as a \code{\link[Matrix:Matrix]{Matrix::Matrix()}} object. -\item \code{n}: The number of nodes with incoming edges in the network. +\item \code{Y}: The incoming latent positions as a \code{\link[Matrix:Matrix]{Matrix::Matrix()}} object. +\item \code{n}: The number of nodes with outgoing edges in the network +(rows of \code{A}). \item \code{k1}: The dimension of the latent node position vectors -encoding incoming latent communities (i.e. in \code{X}). -\item \code{d}: The number of nodes with outgoing edges in the network. +encoding outgoing latent communities (i.e. in \code{X}). +\item \code{d}: The number of nodes with incoming edges in the network +(columns of \code{A}). Does not need to match \code{n} -- rectangular adjacency matrices are supported. \item \code{k2}: The dimension of the latent node position vectors -encoding outgoing latent communities (i.e. in \code{Y}). +encoding incoming latent communities (i.e. in \code{Y}). \item \code{poisson_edges}: Whether or not the graph is taken to be have Poisson or Bernoulli edges, as indicated by a logical vector of length 1. diff --git a/tests/testthat/test-directedness.R b/tests/testthat/test-directedness.R index c487ac5..f7bd2ee 100644 --- a/tests/testthat/test-directedness.R +++ b/tests/testthat/test-directedness.R @@ -60,3 +60,40 @@ test_that("directed graphs are directed", { tbl_graph <- sample_tidygraph(fm) expect_false(is_directed(tbl_graph)) }) + +test_that("print.directed_factor_model() labels rows as outgoing nodes", { + # rows of A send edges: n = 3 outgoing nodes, d = 5 incoming nodes + m <- directed_factor_model(matrix(1, 3, 2), matrix(1, 2, 4), matrix(1, 5, 4)) + out <- paste(capture.output(print(m)), collapse = "\n") + + expect_match(out, "Outgoing Nodes (n): 3", fixed = TRUE) + expect_match(out, "Outgoing Rank (k1): 2", fixed = TRUE) + expect_match(out, "Incoming Rank (k2): 4", fixed = TRUE) + expect_match(out, "Incoming Nodes (d): 5", fixed = TRUE) + + # out degree is edges per row, in degree is edges per column + expect_equal(expected_out_degree(m), expected_edges(m) / 3) + expect_equal(expected_in_degree(m), expected_edges(m) / 5) +}) + +test_that("directed_dcsbm() validator messages name the right side", { + m <- directed_dcsbm( + theta_out = rep(1, 10), theta_in = rep(1, 10), B = matrix(1, 2, 3) + ) + + bad <- m + bad$pi_out <- c(0.5, 0.25, 0.25) + expect_error( + validate_directed_dcsbm(bad), + "`pi_out` must match the outgoing rank", + fixed = TRUE + ) + + bad <- m + bad$pi_in <- c(0.5, 0.5) + expect_error( + validate_directed_dcsbm(bad), + "`pi_in` must match the incoming rank", + fixed = TRUE + ) +})