Fix elementwise variance accumulation in lsqr (closes #639)#751
Open
gaoflow wants to merge 1 commit into
Open
Conversation
lsqr's optional `var` output estimates the diagonal of (A^H A)^-1, one variance per unknown. The update accumulated `ncp.dot(self.dk, self.dk)`, which is the scalar sum of squares of `dk`, so the same value was added to every entry of `var` and all returned variances came out identical (and wrong). Accumulate the elementwise square `ncp.square(self.dk)` instead, matching Paige & Saunders (1982, p.53) and scipy's lsqr. The solution path is unchanged; only the `var` output is corrected. Add test_lsqr_calc_var asserting the variances are not all identical and match both scipy's lsqr and the dense diag(inv(A^H A)) on full-column-rank systems.
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 0 |
| Duplication | 0 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
Collaborator
|
@IruNikZe I was wondering if you got anywhere with this in scipy (and pylops), and if you will be willing to review this PR? |
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
lsqr's optionalvaroutput estimates the diagonal ofbut
ncp.dot(self.dk, self.dk)is the scalar sum of squares ofdk, so the same number is added to every entry ofvar. The result is that all returned variances are identical (and wrong), as reported in #639.Reproduction (from the issue)
Before this PR
var_pylopsis a constant vector ([1.0598, 1.0598, ...]), whilevar_scipyandvar_denseagree.Fix
Accumulate the elementwise square instead:
This matches the recurrence in Paige & Saunders (1982, p.53) and scipy's
lsqr.ncp.squareis available across the NumPy / CuPy backends used elsewhere in this method, and the solution path is untouched — only thevaroutput changes. Credit to @IruNikZe for the diagnosis in #639.Tests
Added
test_lsqr_calc_var(overdetermined real, full column rank) asserting the variances are not all identical (guards against the regression) and match both scipy'slsqrand the densediag(inv(A^H A)).Verified locally:
pytest pytests/test_solver.py(98 passed),ruff checkclean, flake8 (max-line 88) clean on changed files.