Make the ISVD test robust against ill-conditioning and sign ambiguity - #28
Merged
Conversation
The `@testset "ISVD"` built its target as `M = A*B` with `A = rand(20, 5)` and `B = rand(5, 30)`. Because `rand` entries are all positive, M had a dominant rank-one component and a lopsided spectrum: the smallest singular value was often a small fraction of the largest. The trailing singular vector is then only weakly determined, and the two incremental SVD code paths compared by the test (`update!` from a `nothing` start vs. `isvd` from a zero-filled start) disagreed on it by more than the default `isapprox` tolerance. This failed intermittently, e.g. on the `min` Julia CI job, depending on the random draw. Build M instead from orthonormal factors and an explicit, well-separated set of singular values (5, 4, 3, 2, 1), so every singular vector is pinned down to full precision. A seeded RNG was avoided deliberately, since the stream is not stable across Julia versions. Even with a well-conditioned target, left singular vectors are determined only up to sign, and the two code paths occasionally choose opposite signs for a column. Compare `U` columnwise allowing a flip -- the same idiom the test already uses for the unequal-block comparisons -- and compare the sign-coupled rows of `Vt` by magnitude. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #28 +/- ##
=======================================
Coverage 98.71% 98.71%
=======================================
Files 1 1
Lines 78 78
=======================================
Hits 77 77
Misses 1 1 ☔ View full report in Codecov by Sentry. 🚀 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.
Problem
The
@testset "ISVD"fails intermittently — it failed on theJulia minCI job of #26, attest/runtests.jl'sU2 ≈ Ucomparison.The test built its target as
M = A*BwithA = rand(20, 5),B = rand(5, 30). Two independent issues made it flaky:No headroom in the target.
randentries are all positive, so M is dominated by a rank-one "mean" component and has a lopsided spectrum — σ₅/σ₁ frequently lands at 0.02–0.06. The trailing singular vector is then only weakly determined, and the two incremental SVD paths the test compares (update!from anothingstart vs.isvdfrom a zero-filled start) disagree on it by more than the defaultisapproxtolerance. Measured rate: ~0.7% of random draws.Sign ambiguity. Left singular vectors are determined only up to sign, and the two paths occasionally pick opposite signs for a column.
@test U2 ≈ Uignores this — even with a well-conditioned target, ~0.03% of draws fail on a clean sign flip. The test already handles this correctly for its unequal-block comparisons (col1 ≈ col2 || col1 ≈ -col2) but not here.Fix
Ucolumnwise allowing a sign flip (matching the idiom already used elsewhere in the file). The sign-coupled rows ofVtare compared by magnitude.Verification
pA ≈ A, sign-awareU2 ≈ U,s2 ≈ s): 0 failures over 80,000 random trials on both Julia 1.10 and 1.12 — vs. ~206/30,000 with the old construction.Pkg.test()passes (37/37).🤖 Generated with Claude Code