refactor(mixed): RAII guard for proposed-state OMRF marginal evaluation#137
Merged
Merged
Conversation
The three continuous Kyy moves (update_pairwise_effects_continuous_offdiag,
_diag, and update_edge_indicator_continuous) each temporarily set
pairwise_effects_continuous_, covariance_continuous_, and marginal_interactions_
to proposed values, sum the OMRF marginals, then restore the accepted state --
open-coded as three near-identical save / mutate / recompute / restore blocks
(offdiag and edge-indicator byte-identical; diag saved one element).
Replace the manual save/restore with a file-local ProposedContinuousState RAII
guard that snapshots the three fields on construction and restores them on
scope exit, so the proposed-state evaluation lives in a { } block that cannot
leak the mutation regardless of control flow.
Behavior-preserving: same fields mutated, same recompute, same values restored
(restore order among the independent fields is irrelevant; the diag case now
snapshots the whole pairwise matrix but only (i,i) is changed, so the restored
state is identical).
Verified bitwise: adaptive-Metropolis mixed-MRF raw draws (edge-sel delta=0,
no-edge delta=0, no-edge delta=1) digest-identical pre/post.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #137 +/- ##
==========================================
+ Coverage 87.61% 87.72% +0.10%
==========================================
Files 87 87
Lines 12891 12883 -8
==========================================
+ Hits 11295 11301 +6
+ Misses 1596 1582 -14 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The three continuous Kyy moves —
update_pairwise_effects_continuous_offdiag,update_pairwise_effects_continuous_diag, andupdate_edge_indicator_continuous— each evaluate the OMRF marginal log-likelihood under the proposed(Kyy', Σ')by temporarily settingpairwise_effects_continuous_,covariance_continuous_, andmarginal_interactions_, summinglog_marginal_omrf, then restoring the accepted state. This was open-coded as three near-identical save / mutate / recompute / restore blocks (offdiag and edge-indicator byte-identical; diag saved a single element).This replaces the manual save/restore with a file-local
ProposedContinuousStateRAII guard that snapshots the three fields on construction and restores them on scope exit. Each call site becomes a{ }block: construct guard → set proposed values →recompute_marginal_interactions()→ sum marginals → (scope exit restores).Removes the manual-restore footgun — any future field touched inside the block can no longer leak past it by a forgotten restore.
Why this is safe
Same fields mutated, same
recompute_marginal_interactions(), same values restored. Restore order among the three independent fields is irrelevant. The diag case now snapshots the wholepairwise_effects_continuous_matrix rather than the single(i,i)element, but only(i,i)is ever changed in the block, so the restored matrix is identical.Verified bitwise: adaptive-Metropolis mixed-MRF raw draws (main + pairwise + indicator) are digest-identical pre/post across edge-sel
delta=0, no-edgedelta=0, and no-edgedelta=1.identical(main, branch) == TRUE. Harness:dev/bitwise_mixed_logdet_ratio.R.