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
50 changes: 23 additions & 27 deletions R/directed_dcsbm.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ validate_directed_dcsbm <- function(x) {
}

if (any(values$theta_out < 0)) {
stop("Elements of `theta_out` must be strictly positive.", call. = FALSE)
stop("Elements of `theta_out` must be non-negative.", call. = FALSE)
}

if (any(values$theta_in < 0)) {
stop("Elements of `theta_in` must be strictly positive.", call. = FALSE)
stop("Elements of `theta_in` must be non-negative.", call. = FALSE)
}

if (length(values$theta_out) != nrow(values$X)) {
Expand Down Expand Up @@ -225,7 +225,7 @@ sample_block_memberships <- function(n, k, pi, block_sizes = NULL) {
#' explicitly specifying the degree heterogeneity
#' parameters. This implicitly determines the number of nodes
#' in the resulting graph, i.e. it will have `length(theta_out)` nodes.
#' Must be positive. Setting to a vector of ones recovers
#' Must be non-negative. Setting to a vector of ones recovers
#' a stochastic blockmodel without degree correction.
#' Defaults to `NULL`. You must specify either `n`
#' or `theta_out` and `theta_in` together, but not both. `theta_out`
Expand All @@ -236,7 +236,7 @@ sample_block_memberships <- function(n, k, pi, block_sizes = NULL) {
#' explicitly specifying the degree heterogeneity
#' parameters. This implicitly determines the number of nodes
#' in the resulting graph, i.e. it will have `length(theta)` nodes.
#' Must be positive. Setting to a vector of ones recovers
#' Must be non-negative. Setting to a vector of ones recovers
#' a stochastic blockmodel without degree correction.
#' Defaults to `NULL`. You must specify either `n`
#' or `theta_out` and `theta_in` together, but not both. `theta_in`
Expand Down Expand Up @@ -946,6 +946,25 @@ directed_dcsbm <- function(
z_in <- sort(z_in)
}

if (force_identifiability) {
theta_out <- l1_normalize_within(theta_out, z_out)
theta_in <- l1_normalize_within(theta_in, z_in)
}

if (sort_nodes) {
# within each block, sort by decreasing theta. permute each block
# membership vector together with its theta rather than sorting the
# rows of X and Y, which would send rows with theta = 0 to the bottom,
# out of line with z
row_order <- order(z_out, -theta_out)
z_out <- z_out[row_order]
theta_out <- theta_out[row_order]

col_order <- order(z_in, -theta_in)
z_in <- z_in[col_order]
theta_in <- theta_in[col_order]
}

if (k_out > 1) {
X <- sparse.model.matrix(~ z_out + 0)
} else {
Expand All @@ -958,32 +977,9 @@ directed_dcsbm <- function(
Y <- Matrix(1, nrow = n, ncol = 1)
}

if (force_identifiability) {
theta_out <- l1_normalize_within(theta_out, z_out)
theta_in <- l1_normalize_within(theta_in, z_in)
}

X <- set_membership_weights(X, theta_out)
Y <- set_membership_weights(Y, theta_in)

if (sort_nodes) {
# X, Y indexing must match z indexing, be careful about this
X <- sort_by_all_columns(X)
Y <- sort_by_all_columns(Y)

if (k_out > 1) {
theta_out <- X@x
} else {
theta_out <- sort(theta_out, decreasing = TRUE)
}

if (k_in > 1) {
theta_in <- Y@x
} else {
theta_in <- sort(theta_in, decreasing = TRUE)
}
}

dcsbm <- new_directed_dcsbm(
X = X,
S = B,
Expand Down
3 changes: 2 additions & 1 deletion R/undirected_chung_lu.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#' explicitly specifying the degree heterogeneity
#' parameters. This implicitly determines the number of nodes
#' in the resulting graph, i.e. it will have `length(theta)` nodes.
#' Must be positive. Setting to a vector of ones recovers
#' Must be non-negative. Nodes with `theta` equal to zero have no
#' edges. Setting to a vector of ones recovers
#' an erdos renyi graph.
#' Defaults to `NULL`. You must specify either `n` or `theta`,
#' but not both.
Expand Down
33 changes: 16 additions & 17 deletions R/undirected_dcsbm.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ validate_undirected_dcsbm <- function(x) {
}

if (any(values$theta < 0)) {
stop("Elements of `theta` must be strictly positive.", call. = FALSE)
stop("Elements of `theta` must be non-negative.", call. = FALSE)
}

if (length(levels(values$z)) != values$k) {
Expand Down Expand Up @@ -91,7 +91,8 @@ validate_undirected_dcsbm <- function(x) {
#' explicitly specifying the degree heterogeneity
#' parameters. This implicitly determines the number of nodes
#' in the resulting graph, i.e. it will have `length(theta)` nodes.
#' Must be positive. Setting to a vector of ones recovers
#' Must be non-negative. Nodes with `theta` equal to zero have no
#' edges. Setting to a vector of ones recovers
#' a stochastic blockmodel without degree correction.
#' Defaults to `NULL`. You must specify either `n` or `theta`,
#' but not both.
Expand Down Expand Up @@ -394,29 +395,27 @@ dcsbm <- function(
z <- sort(z)
}

if (k > 1) {
X <- sparse.model.matrix(~ z + 0)
} else {
X <- Matrix(1, nrow = n, ncol = 1)
}

if (force_identifiability) {
theta <- l1_normalize_within(theta, z)
}

X <- set_membership_weights(X, theta)

if (sort_nodes) {
# note that X and z indexing must match
X <- sort_by_all_columns(X)
# within each block, sort by decreasing theta. permute z and theta
# together rather than sorting the rows of X, which would send nodes
# with theta = 0 (all-zero rows) to the bottom, out of line with z
node_order <- order(z, -theta)
z <- z[node_order]
theta <- theta[node_order]
}

if (k > 1) {
theta <- X@x
} else {
theta <- sort(theta, decreasing = TRUE)
}
if (k > 1) {
X <- sparse.model.matrix(~ z + 0)
} else {
X <- Matrix(1, nrow = n, ncol = 1)
}

X <- set_membership_weights(X, theta)

dcsbm <- new_undirected_dcsbm(
X = X,
S = B,
Expand Down
2 changes: 1 addition & 1 deletion R/undirected_mmsbm.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ validate_undirected_mmsbm <- function(x) {
}

if (any(values$theta < 0)) {
stop("Elements of `theta` must be strictly positive.", call. = FALSE)
stop("Elements of `theta` must be non-negative.", call. = FALSE)
}

if (min(values$S) < 0) {
Expand Down
6 changes: 4 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ l1_normalize <- function(x) x / sum(x)

# `x` and `block` should be vectors of the same length. `block` should be
# discrete and `x` numeric. normalizes `x` to sum to one within each distinct
# block. ideally only call on positive x to avoid divide by zero errors.
# block. blocks whose elements are all zero are left as zeros.
# the result is in the same order as `x` (i.e. node order, not block order)
l1_normalize_within <- function(x, block) {
as.numeric(x / stats::ave(x, block, FUN = sum))
totals <- stats::ave(x, block, FUN = sum)
# blocks whose x are all zero stay zero rather than becoming NaN
as.numeric(ifelse(totals == 0, 0, x / totals))
}

# put node-level weights `theta` into the non-zero entries of a block
Expand Down
3 changes: 2 additions & 1 deletion man/chung_lu.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/dcsbm.Rd

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

4 changes: 2 additions & 2 deletions man/directed_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/mmsbm.Rd

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

83 changes: 83 additions & 0 deletions tests/testthat/test-zero-theta.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# theta = 0 is allowed and gives isolated nodes. sorting must keep each
# node's block, theta and row of X together

check_alignment <- function(X, z, theta, sorted) {
X <- as.matrix(X)
z <- as.integer(z)

expect_equal(unname(rowSums(X)), unname(theta))

# every row has at most one non-zero entry, in the column of its block
nonzero <- theta > 0
expect_equal(
max.col(X != 0, ties.method = "first")[nonzero],
z[nonzero]
)
expect_true(all(rowSums(X != 0) <= 1))

if (sorted) {
expect_false(is.unsorted(z))
for (b in unique(z)) {
expect_false(is.unsorted(-theta[z == b]))
}
}
}

test_that("dcsbm() keeps z, X and theta aligned with zero theta", {
theta <- c(0, 1, 2, 3, 0, 1, 2, 3, 4, 5)

for (sort_nodes in c(TRUE, FALSE)) {
for (fi in c(TRUE, FALSE)) {
for (k in 1:2) {
set.seed(3)
m <- dcsbm(
theta = theta, B = diag(k), sort_nodes = sort_nodes,
force_identifiability = fi
)
check_alignment(m$X, m$z, m$theta, sort_nodes)
expect_false(anyNA(m$theta))

# the zero-theta nodes are the ones with zero expected degree
expect_equal(sum(m$theta == 0), 2)
expect_equal(
which(m$theta == 0),
unname(which(Matrix::rowSums(m$X %*% m$S %*% t(m$X)) == 0))
)

A <- sample_sparse(m)
expect_equal(sum(A[m$theta == 0, ]), 0)
}
}
}
})

test_that("directed_dcsbm() keeps z, X/Y and theta aligned with zero theta", {
theta_out <- c(0, 1, 2, 3, 0, 1, 2, 3, 4, 5)
theta_in <- c(5, 0, 4, 3, 2, 0, 1, 3, 2, 1)

for (sort_nodes in c(TRUE, FALSE)) {
for (fi in c(TRUE, FALSE)) {
for (shared_blocks in c(TRUE, FALSE)) {
set.seed(3)
m <- directed_dcsbm(
theta_out = theta_out, theta_in = theta_in, B = diag(2),
sort_nodes = sort_nodes, force_identifiability = fi,
shared_blocks = shared_blocks
)
check_alignment(m$X, m$z_out, m$theta_out, sort_nodes && !shared_blocks)
check_alignment(m$Y, m$z_in, m$theta_in, sort_nodes && !shared_blocks)

A <- sample_sparse(m)
expect_equal(sum(A[m$theta_out == 0, ]), 0)
expect_equal(sum(A[, m$theta_in == 0]), 0)
}
}
}
})

test_that("negative theta is still an error", {
expect_error(
dcsbm(theta = c(-1, 1, 1, 1), B = diag(2)),
"non-negative"
)
})
Loading