Skip to content

Keep from == to edges in rectangular models with allow_self_loops = FALSE - #67

Draft
alexpghayes wants to merge 1 commit into
mainfrom
fix/rectangular-self-loops
Draft

alexpghayes wants to merge 1 commit into
mainfrom
fix/rectangular-self-loops

Conversation

@alexpghayes

Copy link
Copy Markdown
Collaborator

Bug

m <- directed_factor_model(matrix(1, 3, 1), matrix(2), matrix(1, 5, 1), allow_self_loops = FALSE)
# averages ~23.8 edges per sample vs expected_edges(m) = 30

In rectangular directed models (n != d), rows and columns are different sets of nodes, so an edge from row i to column i is not a self-loop. sample_edgelist.matrix() (R/sample_edgelist.R) dropped from == to edges anyway. This affects sample_sparse(), sample_igraph() and sample_tidygraph() too.

Fix

Only drop from == to edges when n == d. The allow_self_loops docs for directed_factor_model() (and the directed models that inherit it) and for sample_edgelist.matrix() now say that it is ignored for rectangular graphs. Undirected models get their own copy of the old wording, so their docs don't mention rectangular graphs.

Tests

  • New test in tests/testthat/test-allow_self_loops.R: for the 3 × 5 model above, the mean edge count over 500 samples matches expected_edges(m) = 30 and the mean count of from == to edges matches 6. Both checks use Poisson z-scores below 5. Square models still drop self-loops. Fails on main, passes here.
  • The existing "directed graphs allow_self_loops = FALSE" test used a 1000 × 500 model and locked in the bug. It's now square (1000 × 1000), so it still checks that self-loops are removed.

Full suite passes. Docs regenerated with roxygen2 7.3.2.

⚠️ Conflicts with #60

#60's test-sampler-agreement.R asserts expect_false(any(el$from == el$to)) under if (!loops) for every model, including the rectangular ones, so it will fail once this is merged. It needs to be restricted to square models, e.g. if (!loops && !rectangular). The !rectangular block that follows is already fine.

NEWS

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

  • allow_self_loops = FALSE no longer removes edges from row i to column i in rectangular directed models (n != d), where rows and columns are different sets of nodes. Previously these models were undersampled. allow_self_loops is now documented as ignored for rectangular graphs.

🤖 Generated with Claude Code

…ALSE

In rectangular directed models (n != d) rows and columns are different
sets of nodes, so an edge from row i to column i is not a self-loop.
sample_edgelist() dropped those edges anyway, undersampling the graph.
Only drop self-loops when n == d, and document that allow_self_loops is
ignored for rectangular graphs.

The existing directed allow_self_loops test used a rectangular model
and locked in the bug; make it square.

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