Skip to content

Keep each node's own theta and block when sorting nodes - #71

Draft
alexpghayes wants to merge 1 commit into
fix/zero-thetafrom
fix/sorted-theta-pairing
Draft

alexpghayes wants to merge 1 commit into
fix/zero-thetafrom
fix/sorted-theta-pairing

Conversation

@alexpghayes

Copy link
Copy Markdown
Collaborator

Stacked on #70. The base is fix/zero-theta; retarget it to main once #70 is merged.

Bug

With sort_nodes = TRUE (the default), dcsbm() and the co-factor branch of directed_dcsbm() sorted z first and then laid theta onto the sorted memberships in input order. So block 1 always got the first block_sizes[1] elements of theta, and so on:

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

m <- dcsbm(theta = 1:100 + 0, B = diag(2), sort_nodes = FALSE)
tapply(m$theta, m$z, range)   # random, as intended

Sorted and unsorted models are therefore different models whenever theta isn't exchangeable, e.g. if it's sorted or correlated with anything the user cares about. Constant theta (all SBMs, planted partitions) is unaffected.

Fix

Permute z and theta together from node order with order(z, -theta), without pre-sorting z. This is what the shared-blocks branch of directed_dcsbm() already does, and it is the fix the maintainer picked for zero theta (#70). #70 kept the old positional pairing so that it didn't change any seeded output; this PR changes the pairing.

⚠️ Seeded output changes

Sorted models with non-constant theta now give different graphs for a given seed. The two sorted pins in test-directed-sbms.R are regenerated (the sorted default, and explicit pi, renamed from "is unchanged" to "is pinned"). The comment there explains why. The unsorted pin is unchanged. As a check, the new sorted default pin's theta_out values are a permutation of the unsorted pin's within each block, which is what correct pairing should give.

If you'd rather not change seeded output in 1.0.0, close this; #70 stands on its own.

Tests

New test in tests/testthat/test-block-and-theta-order.R: with equal block sizes (so no relabeling), the sorted model's (z, theta) pairs equal the unsorted model's, for dcsbm() and both sides of directed_dcsbm(), with and without force_identifiability. Fails on #70's branch (8 failures), passes here. Full suite passes. No man/ changes.

NEWS

Left out to avoid conflicting with #59. Suggested bullet under Bug fixes (or Breaking changes, since seeded output changes):

  • Fixed dcsbm() and directed_dcsbm() pairing degree parameters with blocks by position when sort_nodes = TRUE: block 1 always got the first block_sizes[1] elements of theta, so sorted and unsorted models differed whenever theta was not exchangeable. Each node now keeps its own theta and block. Graphs sampled with a given seed from sorted models with non-constant theta will differ.

🤖 Generated with Claude Code

With sort_nodes = TRUE, dcsbm() and the co-factor branch of
directed_dcsbm() sorted z first and then laid theta onto the sorted
memberships in input order, so block 1 always got the first
block_sizes[1] elements of theta. Unsorted models assigned theta to
blocks at random, so sorted and unsorted models differed whenever theta
was not exchangeable (e.g. sorted theta). Permute z and theta together
from node order instead.

This intentionally changes seeded output of sorted models with
non-constant theta; regenerate the two sorted directed reference pins.

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