Skip to content

Store rescaled edge probabilities in Erdős–Rényi and planted partition models - #66

Draft
alexpghayes wants to merge 1 commit into
mainfrom
fix/stale-scaled-params
Draft

alexpghayes wants to merge 1 commit into
mainfrom
fix/stale-scaled-params

Conversation

@alexpghayes

Copy link
Copy Markdown
Collaborator

Bug

Stored parameters went stale after expected_degree / expected_density scaling:

pp <- planted_partition(n = 100, k = 2, within_block = .5, between_block = .1, expected_degree = 5)
c(pp$within_block, pp$S[1, 1])                # 0.5 vs 0.0833; print shows 0.5
erdos_renyi(n = 100, expected_degree = 5)$p   # 0.5 placeholder; the actual p is 0.05

Same for directed_planted_partition() and directed_erdos_renyi().

Fix

When a scaling argument (expected_*) is given, recompute the stored fields from the final S, converting Bernoulli models back from the Poisson scale with 1 - exp(-S):

  • Erdős–Rényi: p <- S[1, 1];
  • planted partitions: within_block / between_block from S's diagonal and off-diagonal (for k = 1, between_block is scaled by the same factor).

Without scaling, the given values are stored exactly as before. Two small helpers, edge_scale_S() and is_rescaled(), are in R/utils.R.

Both planted partitions now behave the same: directed_planted_partition() documented the old pre-scaling behavior; its @return docs now describe the rescaled values, like the undirected one.

Validation: after rescaling, Poisson rates can exceed 1 (e.g. erdos_renyi(n = 10, expected_degree = 50) has p = 5). So the validators now only require stored values to be non-negative (positive for p), and at most 1 for Bernoulli graphs. The user's inputs are still checked in the constructors: within_block / between_block in [0, 1] (moved from the undirected validator to planted_partition(), now with the directed version's "(or a / n)" wording; one existing test's expected message updated), and p strictly between 0 and 1.

Includes bug 16 from the handoff: directed_erdos_renyi() accepted any p >= 0, including p > 1, with a "strictly non-negative" message. It now checks p exactly like erdos_renyi(): strictly between 0 and 1. This is a small breaking change for directed Poisson ER with p > 1; use expected_*_degree for rates above 1. Folded in here because it touches the same validators.

Not changed: overlapping_sbm()'s $B. Its docs (#42) explicitly say B is the user's matrix, neither symmetrized nor rescaled, and that the matrix used for sampling is S; its validator also checks B in [0, 1] as input probabilities. I left it as documented. Say if you'd rather $B be the rescaled matrix too.

Tests

New tests/testthat/test-rescaled-params.R, for Poisson and Bernoulli models. Fails on main (23 failures, 1 error), passes here. Full suite passes. Docs regenerated with roxygen2 7.3.2.

NEWS

Left out to avoid conflicting with #59. Suggested bullets:

Bug fixes

  • erdos_renyi(), directed_erdos_renyi(), planted_partition() and directed_planted_partition() now store and print the edge probability (p, or within_block and between_block) of the final model after rescaling by expected_degree, expected_density etc. Previously they kept the given (or placeholder) values. For Poisson graphs these are edge rates and may exceed 1.

Breaking changes

  • directed_erdos_renyi() now requires p strictly between 0 and 1, like erdos_renyi().

🤖 Generated with Claude Code

After rescaling S with `expected_degree`, `expected_density` and so on,
erdos_renyi() and directed_erdos_renyi() kept the placeholder `p = 0.5`
(or the given `p`), and planted_partition() and
directed_planted_partition() kept the given `within_block` and
`between_block`, so print() and the stored fields described a different
model from the one sampled. Recompute them from the final S, converting
back to probabilities for Bernoulli graphs.

Since stored values are now Poisson rates after rescaling, the
validators only require them to be non-negative (positive for `p`), and
at most one for Bernoulli graphs. The user's inputs are checked in the
constructors, as before. directed_erdos_renyi() now checks `p` like
erdos_renyi(): strictly between zero and one.

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