perf: use covariance path in svd_whiten when n_samples >= n_features - #211
Open
jameschapman19 wants to merge 2 commits into
Open
perf: use covariance path in svd_whiten when n_samples >= n_features#211jameschapman19 wants to merge 2 commits into
jameschapman19 wants to merge 2 commits into
Conversation
Ports PR #211 forward onto main (it was opened against `rewrite/v3`, which was merged into main back in March and never cleaned up as a branch — the PR had been sitting stale against a dead-end base ever since) and fixes a real correctness bug found while verifying it: the covariance path's `lam > 0` filter isn't numerically safe. Forming X.T @ X squares the condition number, so on rank-deficient tall data (n >= p but effective rank < p) near-zero eigenvalues can carry noise of either sign and slip through a strict zero threshold, producing a whitening matrix with spuriously huge columns. Existing rank-deficient test caught this immediately once verified against the real suite. Fixed with a relative tolerance matching numpy.linalg.matrix_rank's convention (`lam.max() * p * eps`) instead of a bare `> 0`. Verified independently before merging: - Full fast suite passes (483 tests) with the corrected version - Whitening postcondition (X_white.T @ X_white / (n-1) ~= I) holds across tall/wide/square, regularised/unregularised, full and rank-deficient inputs - Benchmarked the actual speedup: ~14x at the PR's motivating scale (n=54,000, p=392), confirming the optimization is real and worth having, not just plausible-sounding - Added direct unit coverage for the previously-untested wide (n < p) branch and the n == p boundary Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CL3GT9jTbPvuCwbvmztghe
jameschapman19
force-pushed
the
fix/svd-whiten-covariance-path
branch
from
August 3, 2026 18:29
32d6aa5 to
4fc6869
Compare
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CL3GT9jTbPvuCwbvmztghe
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.
Summary
svd_whitennow forms the p×p sample covarianceC = Xᵀ X / (n-1)and diagonalises it witheigh, rather than callingnp.linalg.svd(X)which allocates an n×p intermediateUmatrix.Context
This PR was originally opened against
rewrite/v3, which was merged intomainback in March and never cleaned up as a branch — so the PR had been sitting stale against a dead-end base for months with an unchecked test plan. Retargeted tomain(the diff is unchanged:rewrite/v3's tip is an ancestor ofmain, so nothing else has touched this function since).Found and fixed a real correctness bug while verifying it before merging: the covariance path's
lam > 0filter isn't numerically safe. FormingX.T @ Xsquares the condition number, so on rank-deficient tall data (n >= p but effective rank < p) near-zero eigenvalues can carry noise of either sign and slip through a strict zero threshold — producing a whitening matrix with spuriously huge columns for directions that don't actually exist in the data. The existingtest_rank_deficient_inputregression test caught this immediately once run against the real suite (previously the PR's own test-plan checkboxes were never actually checked off). Fixed with a relative tolerance matchingnumpy.linalg.matrix_rank's convention (lam.max() * p * eps) instead of a bare> 0.Verification performed
X_white.T @ X_white / (n-1) ≈ I) verified to hold across tall/wide/square shapes, with and without regularisation, and on both full-rank and rank-deficient input.n < p) branch and then == pboundary.Test plan
pytest -m "not slow"— 483 passed)CCA,rCCA,PLS,CCA_EYgive equivalent results before/after (rotation-invariant Gram-matrix check + full test suite for the two real consumers,rCCA/CCA_EY)Generated by Claude Code