Skip to content

Error on Bernoulli edge probabilities of 1; check poisson_edges first - #72

Draft
alexpghayes wants to merge 1 commit into
mainfrom
fix/bernoulli-probability-one
Draft

alexpghayes wants to merge 1 commit into
mainfrom
fix/bernoulli-probability-one

Conversation

@alexpghayes

Copy link
Copy Markdown
Collaborator

Bugs

Probability 1 (bug 3 of the handoff):

pp <- planted_partition(n = 10, k = 2, within_block = 1, between_block = 0.1, poisson_edges = FALSE)
pp$S              # Inf on the diagonal
sample_sparse(pp) # "missing value where TRUE/FALSE needed"

undirected_factor_model() and directed_factor_model() checked max(S) > 1, so a probability of exactly 1 passed and became -log(1 - 1) = Inf.

Non-logical poisson_edges (bug 15): poisson_edges = "yes" gave "invalid argument type", and NA gave "missing value…", because if (!poisson_edges) ran before the validator.

Fix

  • As decided by the maintainer, error when any Bernoulli probability, after symmetrizing and scaling, is >= 1: "Elements of S (after … scaling to achieve expected degree or density) must be less than 1 for Bernoulli graphs (poisson_edges = FALSE)." This covers every model, since they all go through the two factor model constructors, including scaling that reaches exactly 1 (e.g. erdos_renyi(n = 10, expected_degree = 10, poisson_edges = FALSE)). The poisson_edges docs now say so.
  • Call check_flag() on poisson_edges and allow_self_loops at the top of both constructors: "poisson_edges must be TRUE or FALSE." The directed message no longer mentions symmetrizing.

Tests

New tests/testthat/test-bernoulli-probability-one.R. Fails on main (6 failures, 1 error), passes here. Full suite passes. Docs regenerated with roxygen2 7.3.2.

Interaction with #66

#66 makes erdos_renyi() / directed_erdos_renyi() require p < 1 for Bernoulli graphs. That is consistent with this PR, and the two PRs touch different files.

NEWS

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

  • Bernoulli models (poisson_edges = FALSE) now error clearly when any edge probability, after symmetrizing and rescaling, is 1 or more. Previously a probability of exactly 1 became an infinite Poisson rate and sampling failed with "missing value where TRUE/FALSE needed". Invalid poisson_edges or allow_self_loops values (e.g. "yes" or NA) now give a clear error.

🤖 Generated with Claude Code

The Bernoulli check in undirected_factor_model() and
directed_factor_model() used `max(S) > 1`, so a probability of exactly
1 passed and became an infinite Poisson rate via -log(1 - S), failing
later in sample_sparse() with "missing value where TRUE/FALSE needed".
Error when any probability is >= 1.

Also check `poisson_edges` and `allow_self_loops` with check_flag()
before they are used: `if (!poisson_edges)` ran before validation, so
values like "yes" or NA failed with cryptic base R errors.

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