fix: clamp negative eigenvalues before sqrt in LRD predict#21
Open
jamgochiana wants to merge 2 commits into
Open
fix: clamp negative eigenvalues before sqrt in LRD predict#21jamgochiana wants to merge 2 commits into
jamgochiana wants to merge 2 commits into
Conversation
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
When conditioning an LRDMvNormal whose rank nearly saturates the dimension (e.g. d=3, rank=2), the eigenvalues of I - I_plus_FF_inv are mathematically nonnegative but can tip slightly negative in floating point (e.g. -2.4e-14). The unclamped sqrt then throws DomainError. Clamp eigenvalues at zero with sqrt.(max.(λ, 0)) before taking the matrix square root. This does not change results for well-conditioned inputs: a value like -2.4e-14 becomes 0, which is the correct mathematical value. Adds a deterministic regression test (seed 2) that reproduced the DomainError prior to this fix. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Closes #19
Root cause
predicton anLRDMvNormalcomputes the conditional low-rank factor via aneigendecomposition of
I - I_plus_FF_invfollowed by a matrix square root(
src/predict.jl:110-111). When the rank nearly saturates the dimension(e.g.
d=3,rank=2), the eigenvalues ofI - I_plus_FF_invaremathematically nonnegative but can tip slightly negative in floating point
(e.g.
-2.4e-14). The unclampedsqrtthen throwsDomainError: sqrt was called with a negative real argument.Fix
Clamp the eigenvalues at zero before the square root:
sqrt.(max.(λ, 0)). This is a numerical-safety change only — it does notalter results for well-conditioned inputs, since a value like
-2.4e-14becomes
0, which is the correct mathematical value.Regression test
Added a deterministic regression test in
test/test_prediction.jl(EdgeCases testset) that constructs a
d=3,rank=2LRDMvNormalwith seed 2and conditions the 3D joint on coordinate 1 (
input_indices=[1],output_indices=[2,3]). This reproduced theDomainErroron the unmodifiedcode and asserts the returned conditional has length 2 after the fix. Full
suite passes (Prediction: 25/25, LRDMvNormal: 53/53, Fitting: 84/84).
🤖 Generated with Claude Code