refactor(s5): share GGM/Mixed precision kernels as cholesky_helpers free functions#140
Merged
Merged
Conversation
…ree functions
The rank-2/rank-1 determinant-lemma log-ratio and the Roverato proposal-constants
extraction were near-identical between GGMModel and MixedMRFModel, differing only
in where the current precision entries come from (GGM stores K directly; Mixed
stores Kyy = -2 * pairwise_effects_continuous_). This was the cross-model
divergence the class-design review flagged (det-lemma block appearing 4x).
Promote the shared math to stateless free functions in cholesky_helpers.h:
- log_det_ratio_edge_kernel / log_det_ratio_diag_kernel
- precision_proposal_constants (returns the 6 reparameterization constants)
Each takes the covariance (and Cholesky), indices, and the resolved precision
scalars; GGM passes precision_matrix_ entries, Mixed passes -2*pairwise. Per the
review-critique decision, these are FREE FUNCTIONS, not a composed
GaussianPrecisionBlock (the 'owns Sigma' framing leaks; only these leaf kernels
are genuinely identical).
cholesky_helpers.h now includes explog_macros.h so the kernels use the same
MY_LOG/MY_EXP as the originals -- required for bitwise fidelity (these may be the
OpenLibM __ieee754_* variants, not std::).
GGMModel::{log_det_ratio_edge,log_det_ratio_diag,get_constants} and
MixedMRFModel::{log_det_ratio_yy_edge,log_det_ratio_yy_diag,get_precision_constants}
are now thin adapters.
Verified bitwise across all paths that hit these kernels: adaptive-Metropolis GGM
(no-edge delta=0/1 + edge-sel), NUTS GGM (+ mass adaptation), and adaptive-
Metropolis mixed-MRF (edge-sel + no-edge, delta=0/1) raw draws are digest-
identical pre/post. identical(main, branch) == TRUE for all three harnesses.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #140 +/- ##
==========================================
- Coverage 87.74% 87.69% -0.06%
==========================================
Files 87 87
Lines 12860 12855 -5
==========================================
- Hits 11284 11273 -11
- Misses 1576 1582 +6 ☔ 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.
S5 from the class-design review, in the free-function form the review-critique settled on (not a composed
GaussianPrecisionBlock).What
The rank-2/rank-1 determinant-lemma log-ratio and the Roverato proposal-constants extraction were near-identical between
GGMModelandMixedMRFModel, differing only in where the current precision entries come from: GGM storesKdirectly; Mixed storesKyy = -2 * pairwise_effects_continuous_. This is the cross-model divergence the review flagged (det-lemma block appearing 4×, and the drift-guard that was added to one model and not the other).Promotes the shared math to stateless free functions in
cholesky_helpers.h:log_det_ratio_edge_kernel/log_det_ratio_diag_kernelprecision_proposal_constants(returns the 6 reparameterization constants)Each takes the covariance (and Cholesky), the indices, and the resolved precision scalars — GGM passes
precision_matrix_entries, Mixed passes-2 * pairwise. The six member functions (GGM::{log_det_ratio_edge,log_det_ratio_diag,get_constants},Mixed::{log_det_ratio_yy_edge,log_det_ratio_yy_diag,get_precision_constants}) become thin adapters.Per the critique, these are free functions, not a composed block: only these leaf kernels are genuinely identical; the "owns Σ" framing leaks (Mixed fuses the Kyy update with the OMRF marginal recomputation).
The
-2/-½boundary accessor (the other half of the simpler-S5 plan) is not in scope here; this PR is the cross-model kernel dedup.Why this is safe
The two models' originals were already identical up to int-vs-double literals (
1vs1.0,2vs2.0) that don't change thedoubleresult, and the precision-source difference now passed in as scalars (-2.0 * pairwise(i,j)is the samedoubleas Mixed computed inline).cholesky_helpers.hnow includesexplog_macros.hso the kernels use the sameMY_LOG/MY_EXPas the originals — required for bitwise fidelity (these can be the OpenLibM__ieee754_*variants, notstd::).Verified bitwise across every path that hits these kernels: adaptive-Metropolis GGM (no-edge δ=0/1 + edge-sel), NUTS GGM (+ mass adaptation), and adaptive-Metropolis mixed-MRF (edge-sel + no-edge, δ=0/1).
identical(main, branch) == TRUEfor all three. Harnesses:dev/bitwise_ggm_mh_primitive.R,dev/bitwise_ggm_nuts.R,dev/bitwise_mixed_logdet_ratio.R.