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
47 changes: 25 additions & 22 deletions R/directed_dcsbm.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down Expand Up @@ -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
)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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,
Expand All @@ -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
Expand Down
28 changes: 16 additions & 12 deletions R/directed_factor_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
39 changes: 21 additions & 18 deletions man/directed_dcsbm.Rd

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

14 changes: 8 additions & 6 deletions man/directed_erdos_renyi.Rd

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

20 changes: 12 additions & 8 deletions man/directed_factor_model.Rd

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

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