From 07a8fb6304098160daea210551bfff9cc7ea3604 Mon Sep 17 00:00:00 2001 From: Alex Hayes Date: Tue, 17 Nov 2026 18:49:41 -0800 Subject: [PATCH] Keep each node's own theta and block when sorting nodes 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 --- R/directed_dcsbm.R | 12 +--- R/undirected_dcsbm.R | 9 +-- tests/testthat/test-block-and-theta-order.R | 54 ++++++++++++++++ tests/testthat/test-directed-sbms.R | 72 ++++++++++----------- 4 files changed, 95 insertions(+), 52 deletions(-) diff --git a/R/directed_dcsbm.R b/R/directed_dcsbm.R index ecc53bf..af4bbb0 100644 --- a/R/directed_dcsbm.R +++ b/R/directed_dcsbm.R @@ -941,21 +941,15 @@ directed_dcsbm <- function( # co-factor model: outgoing and incoming parameters are unrelated, and # rows and columns are sorted independently - if (sort_nodes) { - z_out <- sort(z_out) - 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 + # sort by block, and by decreasing theta within blocks. permute each + # block membership vector together with its theta, so that row + # (column) i keeps its own block and its own theta row_order <- order(z_out, -theta_out) z_out <- z_out[row_order] theta_out <- theta_out[row_order] diff --git a/R/undirected_dcsbm.R b/R/undirected_dcsbm.R index 1b637f6..98dbed4 100644 --- a/R/undirected_dcsbm.R +++ b/R/undirected_dcsbm.R @@ -391,18 +391,13 @@ dcsbm <- function( pi <- pi / sum(pi) - if (sort_nodes) { - z <- sort(z) - } - if (force_identifiability) { theta <- l1_normalize_within(theta, z) } if (sort_nodes) { - # 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 + # sort by block, and by decreasing theta within blocks. permute z and + # theta together, so that node i keeps its own block and its own theta node_order <- order(z, -theta) z <- z[node_order] theta <- theta[node_order] diff --git a/tests/testthat/test-block-and-theta-order.R b/tests/testthat/test-block-and-theta-order.R index cd80c3f..487a338 100644 --- a/tests/testthat/test-block-and-theta-order.R +++ b/tests/testthat/test-block-and-theta-order.R @@ -126,3 +126,57 @@ test_that("sorted models keep X, z and theta consistent", { expect_equal(block_of(d$X), as.integer(d$z_out)) expect_equal(block_of(d$Y), as.integer(d$z_in)) }) + +test_that("sorting nodes keeps each node's own block and theta together", { + # with equal block sizes no blocks are relabeled, so the sorted model is + # the unsorted model with its nodes permuted + pairs <- function(z, theta) { + p <- data.frame(z = as.integer(z), theta = unname(theta)) + p[order(p$z, p$theta), ] + } + + theta <- 1:100 + 0 + + for (fi in c(TRUE, FALSE)) { + set.seed(1) + sorted <- dcsbm(theta = theta, B = diag(2), force_identifiability = fi) + set.seed(1) + unsorted <- dcsbm( + theta = theta, B = diag(2), force_identifiability = fi, + sort_nodes = FALSE + ) + + expect_equal( + pairs(sorted$z, sorted$theta), + pairs(unsorted$z, unsorted$theta), + ignore_attr = TRUE + ) + + # theta is not simply split by position between the blocks + if (!fi) { + expect_false(all(sorted$theta[sorted$z == "block1"] <= 50)) + } + + set.seed(1) + sorted <- directed_dcsbm( + theta_out = theta, theta_in = rev(theta), B = diag(2), + force_identifiability = fi + ) + set.seed(1) + unsorted <- directed_dcsbm( + theta_out = theta, theta_in = rev(theta), B = diag(2), + force_identifiability = fi, sort_nodes = FALSE + ) + + expect_equal( + pairs(sorted$z_out, sorted$theta_out), + pairs(unsorted$z_out, unsorted$theta_out), + ignore_attr = TRUE + ) + expect_equal( + pairs(sorted$z_in, sorted$theta_in), + pairs(unsorted$z_in, unsorted$theta_in), + ignore_attr = TRUE + ) + } +}) diff --git a/tests/testthat/test-directed-sbms.R b/tests/testthat/test-directed-sbms.R index 921f63c..7179337 100644 --- a/tests/testthat/test-directed-sbms.R +++ b/tests/testthat/test-directed-sbms.R @@ -28,8 +28,12 @@ expect_reference_case <- function(case, z_out, z_in, theta_out, theta_in, S, i, # of the defaults. they also reflect the theta-order fix (theta in node # order). # - the explicit `pi` pin was generated with fastRG 0.4.0.9000 (commit -# 30679ae), before `block_sizes_out` / `block_sizes_in` were added, and is -# unchanged +# 30679ae), before `block_sizes_out` / `block_sizes_in` were added. +# - the sorted pins (the sorted default and explicit `pi`) were regenerated +# when sorting stopped pairing theta with blocks by position: each node +# now keeps its own theta and block when nodes are sorted, which +# intentionally changes seeded output of sorted models. the unsorted pin +# is unchanged test_that("directed_dcsbm() default sampling is pinned (sorted)", { case <- sample_reference_case( @@ -44,38 +48,35 @@ test_that("directed_dcsbm() default sampling is pinned (sorted)", { z_out = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), z_in = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), theta_out = c( - 0.617259, 0.2880123, 0.04267424, 0.03071212, 0.02134236, 0.5541946, - 0.208573, 0.08806888, 0.08315635, 0.06600721, 0.619145, 0.1508151, - 0.1333359, 0.06108368, 0.03562034 + 0.5310611, 0.1979492, 0.1750073, 0.05662958, 0.03935283, 0.562851, + 0.3201125, 0.06703823, 0.03158169, 0.01841655, 0.5182694, 0.2448739, + 0.1033968, 0.09762926, 0.03583059 ), theta_in = c( - 0.3888567, 0.3887706, 0.2223727, 0.4261955, 0.3017147, 0.1742412, - 0.09784863, 0.4608853, 0.3938223, 0.1242239, 0.02106845, 0.5364644, - 0.2054845, 0.2038565, 0.05419462 + 0.6954421, 0.2797657, 0.02479222, 0.6694407, 0.19997, 0.06762814, + 0.06296111, 0.4200212, 0.4165979, 0.1039326, 0.05944831, 0.3871937, + 0.3704002, 0.1255702, 0.1168359 ), S = structure( c( - 4.604851, 6.235598, 1.309489, 3.287976, 3.37821, - 1.262552, 4.444437, 7.243754, 6.082634, 0.4312557, 4.136733, - 2.58251 + 4.604851, 6.235598, 1.309489, 3.287976, 3.37821, 1.262552, 4.444437, + 7.243754, 6.082634, 0.4312557, 4.136733, 2.58251 ), dim = 3:4 ), i = c( - 1L, 2L, 6L, 7L, 8L, 13L, 1L, 3L, 6L, 9L, 10L, 1L, 2L, 5L, 6L, - 12L, 1L, 6L, 1L, 2L, 6L, 7L, 8L, 11L, 6L, 9L, 2L, 1L, 6L, 9L, - 10L, 11L, 12L, 13L, 6L, 7L, 10L, 13L, 6L, 7L, 11L, 7L, 11L, 6L, - 11L + 1L, 2L, 3L, 4L, 6L, 7L, 8L, 9L, 14L, 1L, 3L, 5L, 6L, 12L, 1L, 6L, 7L, + 11L, 2L, 6L, 7L, 2L, 3L, 1L, 6L, 8L, 9L, 11L, 12L, 13L, 6L, 7L, 10L, + 11L, 13L, 7L, 11L, 6L, 11L, 7L, 11L, 6L, 11L ), j = c( - 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, - 3L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 7L, 8L, 8L, 8L, 8L, - 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 12L, 12L, 13L, 14L + 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, + 5L, 5L, 5L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 10L, + 10L, 11L, 11L, 12L, 12L, 13L, 14L ), x = c( - 1, 2, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, - 3, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, 1, 1, 1, 3, - 1, 1, 1, 1 + 2, 2, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 2, 1, 3, 4, 2, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1 ) ) }) @@ -154,7 +155,7 @@ test_that("directed_dcsbm() defaults to balanced block sizes", { expect_equal(as.vector(table(sorted$z_in)), c(4, 4, 5, 5, 5)) }) -test_that("directed_dcsbm() sampling with explicit pi is unchanged", { +test_that("directed_dcsbm() sampling with explicit pi is pinned", { case <- sample_reference_case( theta_out = rep(1:3, 5), theta_in = rep(2, 15), @@ -169,31 +170,30 @@ test_that("directed_dcsbm() sampling with explicit pi is unchanged", { z_out = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), z_in = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), theta_out = c( - 0.4285714, 0.2857143, 0.1428571, 0.1428571, 0.1304348, 0.1304348, - 0.1304348, 0.1304348, 0.08695652, 0.08695652, 0.08695652, 0.08695652, - 0.04347826, 0.04347826, 0.04347826 + 0.3, 0.3, 0.3, 0.1, 0.15, 0.15, 0.1, 0.1, 0.1, 0.1, 0.1, 0.05, 0.05, + 0.05, 0.05 ), theta_in = c( - 0.3333333, 0.3333333, 0.3333333, 0.25, 0.25, 0.25, 0.25, 0.125, - 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125 + 0.3333333, 0.3333333, 0.3333333, 0.25, 0.25, 0.25, 0.25, 0.125, 0.125, + 0.125, 0.125, 0.125, 0.125, 0.125, 0.125 ), S = structure( - c(4.285714, 2.142857, 8.571429, 6.428571, 12.85714, 10.71429), + c( + 4.285714, 2.142857, 8.571429, 6.428571, 12.85714, 10.71429 + ), dim = 2:3 ), i = c( - 1L, 2L, 1L, 3L, 7L, 8L, 12L, 1L, 7L, 2L, 4L, 5L, 15L, 1L, 7L, - 12L, 1L, 2L, 5L, 10L, 6L, 8L, 2L, 4L, 2L, 9L, 2L, 11L, 3L, 1L, - 3L + 1L, 2L, 2L, 3L, 5L, 10L, 11L, 3L, 10L, 1L, 2L, 6L, 15L, 3L, 10L, 11L, + 2L, 3L, 6L, 7L, 5L, 9L, 1L, 2L, 2L, 14L, 2L, 8L, 4L, 2L, 3L ), j = c( - 1L, 1L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 6L, 7L, 7L, 7L, 8L, 8L, - 8L, 9L, 9L, 9L, 9L, 10L, 10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, - 15L, 15L + 1L, 1L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, + 9L, 9L, 10L, 10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 15L, 15L ), x = c( - 1, 1, 1, 1, 1, 1, 1, 3, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, - 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1 + 1, 1, 1, 1, 1, 1, 1, 3, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, + 1, 2, 1, 1, 1, 1, 2 ) ) })