Skip to content

Support zero theta in dcsbm() and directed_dcsbm() - #70

Draft
alexpghayes wants to merge 1 commit into
mainfrom
fix/zero-theta
Draft

alexpghayes wants to merge 1 commit into
mainfrom
fix/zero-theta

Conversation

@alexpghayes

Copy link
Copy Markdown
Collaborator

Bug

set.seed(3)
m <- dcsbm(theta = c(0, 1, 2, 3, 0, 1, 2, 3, 4, 5), B = diag(2))
cbind(as.matrix(m$X), z = as.integer(m$z), theta = m$theta)
# 2 rows where z disagrees with the block in X; 7 rows where theta != rowSums(X)

With sort_nodes = TRUE, sort_by_all_columns() sent the all-zero rows of X to the bottom, but z had been sorted separately, and theta <- X@x assumed block order. The co-factor branch of directed_dcsbm() had the same pattern. The validators said theta must be "strictly positive" but only rejected negatives.

Fix

As decided by the maintainer, zero theta is supported and gives isolated nodes:

  • dcsbm() and the co-factor branch of directed_dcsbm() now sort within blocks by permuting z and theta together with order(z, -theta), and then build X (and Y) from them, like the shared-blocks branch.
  • For positive theta this gives exactly the same models and seeded samples as before: all pinned reference tests pass unchanged.
  • l1_normalize_within() leaves a block whose theta are all zero at zero instead of NaN, so force_identifiability = TRUE works too.
  • Validator messages (dcsbm, directed_dcsbm, mmsbm) now say "must be non-negative", and the theta docs for dcsbm(), directed_dcsbm() and chung_lu() say zero theta gives a node with no edges.

Tests

New tests/testthat/test-zero-theta.R: after construction, each row of X has its weight in the column of its block z, rowSums(X) == theta, sorted models are sorted by block and then by decreasing theta, and zero-theta nodes get no sampled edges. This is checked for dcsbm() (k = 1, 2) and directed_dcsbm() (co-factor and shared blocks), with every combination of sort_nodes and force_identifiability. Fails on main (20 failures), passes here. Full suite passes. Docs regenerated with roxygen2 7.3.2.

Related bug, in a stacked PR

While fixing this I found that sorted models don't pair theta[i] with node i's own block. z is sorted first and theta is then laid on in input order, so block 1 always gets the first block_sizes[1] elements of theta:

set.seed(1)
m <- dcsbm(theta = 1:100 + 0, B = diag(2))
tapply(m$theta, m$z, range)   # block1: 1..50, block2: 51..100, every time

Unsorted models assign theta to blocks at random, so the two settings give different models whenever theta isn't exchangeable. Fixing that changes seeded output and the pinned reference values in test-directed-sbms.R, so it is a separate PR stacked on this one, for you to take or leave.

NEWS

Left out to avoid conflicting with #59. Suggested bullet under Bug fixes:

  • dcsbm() and directed_dcsbm() now support theta = 0 (isolated nodes). Previously, with sort_nodes = TRUE, zero theta misaligned the block memberships z, the rows of X and the stored theta.

🤖 Generated with Claude Code

With sort_nodes = TRUE, nodes were sorted by sorting the rows of the
membership matrix, which sends all-zero rows (theta = 0) to the bottom,
while z had been sorted separately and theta was read back from X@x in
block order. So zero theta misaligned z, X and theta. The validators
said theta must be "strictly positive" but only rejected negatives.

Allow theta = 0 (isolated nodes): sort within blocks by permuting z and
theta together with order(z, -theta), then build X from them. For
positive theta this gives exactly the same models as before. Blocks
whose theta are all zero stay zero under force_identifiability instead
of becoming NaN. Validator messages and docs now say non-negative.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant