Skip to content

Keep mcmc()'s one_by_one advice when a numerical error comes before the first draw (#577) - #863

Merged
njtierney merged 6 commits into
mainfrom
i577-numerical-advice
Oct 26, 2026
Merged

njtierney merged 6 commits into
mainfrom
i577-numerical-advice

Conversation

@njtierney

Copy link
Copy Markdown
Collaborator

Closes #577.

  • TF2 error - Failure (test_inference.R:351): numerical issues are handled in mcmc #577's test was commented out because its Cholesky model stopped raising a numerical error under TensorFlow 2, so mcmc() had nothing to report. That model still samples without raising one.
  • Rebuilding the test around a model that does raise, a solve() of a matrix that is singular whenever round(r) is 1 or -1, found a bug on main: when a numerical error stops sampling before mcmc() keeps its first draw, the clean-up on exit traces an empty trace, and its "subscript out of bounds" replaces greta's error suggesting one_by_one = TRUE. Since Do more inside of TF and TFP + fix thinning issues #855, sampling with verbose = FALSE is one call to TensorFlow, so any numerical error during sampling comes before the first draw.

What changes

  • The sampler's trace_values() returns early when the trace has no draws, so the error that stopped sampling is the one users see.
  • That error reads "Error:" rather than naming the internal method self$abort_numerical_error().
  • stashed_samples(), which the docs point to after an aborted run, returns no draws when several chains stopped before their first, rather than erroring with "length of 'dimnames' [2] not equal to array extent". The sampler sized its empty trace with length() of one traced row per chain, which made it the number of chains times too wide for its names; it now uses ncol().

What users see now, from the new snapshot:

Error:
! TensorFlow hit a numerical problem that caused it to error
greta can handle these as bad proposals if you rerun `mcmc()` with the argument `one_by_one = TRUE`.
This will slow down the sampler slightly.
The error encountered can be recovered and viewed with:
`greta_notes_tf_num_error()`

Tests

  • tests/testthat/test_inference.R: the singular solve() model stops mcmc() with the advice (snapshot); with two chains it gives the advice and stashed_samples() then has no draws; the one_by_one test also checks it keeps all 40 draws. Against main's code, both new tests fail with "subscript out of bounds".
  • tests/testthat/helpers.R: singular_at_one_model(), shared by the three numerical-error tests.
  • tests/testthat/test_inference_numerical_issues.R, all commented out, is removed.
  • The whole suite passes with NOT_CRAN=true and the branch installed.

🤖 Generated with Claude Code

Loading
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.

TF2 error - Failure (test_inference.R:351): numerical issues are handled in mcmc

1 participant