Skip to content

refactor(ggm): single MH-move primitive shared by sampling and tuning#135

Merged
MaartenMarsman merged 1 commit into
mainfrom
refactor/ggm-mh-move-primitive
Jun 4, 2026
Merged

refactor(ggm): single MH-move primitive shared by sampling and tuning#135
MaartenMarsman merged 1 commit into
mainfrom
refactor/ggm-mh-move-primitive

Conversation

@MaartenMarsman

@MaartenMarsman MaartenMarsman commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

What

The within-model edge and diagonal Metropolis moves were copy-pasted between the sampling path (update_edge_parameter / update_diagonal_parameter) and the Robbins-Monro tuning path (tune_proposal_sd) — four RNG/math-identical blocks. This extracts two private primitives:

  • ggm_edge_move(i, j) / ggm_diag_move(i) — propose, compute the full acceptance ratio, apply the move (with the Cholesky update) on accept, and return the raw ln_alpha.

The sampling wrappers return min(1, exp(ln_alpha)) (the accept prob do_one_metropolis_step records); the tuning loops feed the raw ln_alpha to the RM update. The edge-active short-circuit and cache-flag invalidation (constraint_dirty_/theta_valid_) stay at the callers; the edge-off path still returns 0.0/continue before any rnorm.

Net: the proposal RNG and acceptance math live in one place; tune_proposal_sd shrinks from two ~35-line bodies to two ~4-line calls.

Why this is safe

Pure code extraction — identical proposal/accept/update sequence, so the RNG draw order is unchanged.

Verified bitwise: adaptive-Metropolis GGM raw draws (main + pairwise + indicator) are digest-identical pre/post across three scenarios that exercise every touched line:

  • no edge selection, delta = 0 (within-K edge+diag moves + tuning, no tilt)
  • no edge selection, delta = 1 (adds the determinant_tilt_ branch in both primitives)
  • edge selection, delta = 0 (selection moves interleaved with within-K moves)

identical(main, refactor) == TRUE for all three. Harness: dev/bitwise_ggm_mh_primitive.R.

The within-model edge and diagonal Metropolis moves were copy-pasted between
the sampling path (update_edge_parameter/update_diagonal_parameter) and the
Robbins-Monro tuning path (tune_proposal_sd) -- four RNG/math-identical blocks.
Extract ggm_edge_move/ggm_diag_move, each proposing, computing the full
acceptance ratio, applying the move on accept, and returning the RAW ln_alpha.

The sampling wrappers return min(1,exp(ln_alpha)) (the accept prob consumed by
do_one_metropolis_step); the tuning loops feed the raw ln_alpha to the
Robbins-Monro update. Edge-active short-circuit and cache-flag invalidation
stay at the callers. Proposal RNG and acceptance math now live in one place.

Behavior-preserving: identical proposal/accept/update sequence, so the RNG
draw order is unchanged. Verified bitwise -- adaptive-Metropolis GGM raw draws
(no-edge delta=0, no-edge delta=1, edge-selection delta=0) digest-identical
pre/post.
@codecov

codecov Bot commented Jun 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 87.69%. Comparing base (a7130eb) to head (5bbb380).

Files with missing lines Patch % Lines
src/models/ggm/ggm_model.cpp 91.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #135      +/-   ##
==========================================
+ Coverage   87.66%   87.69%   +0.02%     
==========================================
  Files          87       87              
  Lines       12933    12897      -36     
==========================================
- Hits        11338    11310      -28     
+ Misses       1595     1587       -8     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@MaartenMarsman MaartenMarsman merged commit 7b32aa0 into main Jun 4, 2026
10 checks passed
@MaartenMarsman MaartenMarsman deleted the refactor/ggm-mh-move-primitive branch June 4, 2026 12:58
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