Repository navigation
Make factor model spectra reliable for small models - #62
Draft
alexpghayes wants to merge 1 commit into
Draft
alexpghayes wants to merge 1 commit into
alexpghayes wants to merge 1 commit into
Conversation
fastRG's eigs_sym() and svds() methods for factor models call RSpectra on an exactly rank-k implicit operator. RSpectra's Lanczos solvers are unreliable on such operators when they are small (yixuan/RSpectra#20): with the default ncv = min(dim, max(2k + 1, 20)), they errored or silently returned wrong values for most n = 20 models. - default to ncv = min(dim - 1, max(2k + 1, 10)) unless opts$ncv is given, which fixes every n = 20 case tested (40/40 seeds, vs 7-25/40) - warn, with a link to the RSpectra issue, when min(n, d) <= 25 - forward `which`, `opts` and `...` to RSpectra; `which` was ignored - error informatively for `sigma`, which RSpectra can't support for an implicit operator Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Fixes bugs #1 and #5 from the bug hunt: wrong spectra for small models, and
eigs_sym()ignoringwhich.Problem
fastRG's
eigs_sym()/svds()methods for models call RSpectra on an exactly rank-k implicit operator. RSpectra's Lanczos solvers are unreliable on small, exactly low-rank matrices: see yixuan/RSpectra#20, which is still open. RSpectra's defaultncv = min(dim, max(2k + 1, 20))makes the Krylov subspace nearly the whole space for small models. Onmainat n = 20, over 40 seeds:mainsvds()eigs_sym()svds()TridiagEigen: eigen decomposition failed)The same
ncvdependence reproduces both examples in RSpectra#20: each fails at the defaultncvand works with a smaller one. RSpectra on the dense matrix fails too, so this is RSpectra's weakness rather than fastRG's operator. But fastRG's methods are what feed RSpectra these operators.Per the maintainer's direction, this PR only touches methods for models. RSpectra's own behavior on data matrices, including the missing
dsCMatrixmethod foreigs_sym(), is left to RSpectra.Changes (
R/expected-spectra.R)ncv:min(dim − 1, max(2k + 1, 10)), unless the user passesopts$ncv, and only whenk < ncv ≤ dimis possible.min(n, d) ≤ 25. It has classfastRG_warning_small_model_spectra, links to RSpectra#20, and suggestseigen()/svd()ofas.matrix(expectation(model)).which,optsand...now reach RSpectra.eigs_sym(m, k = 1, which = "SA")used to return the largest-magnitude eigenvalue, 6, instead of the smallest, −4.sigma: a clear error. Shift-and-invert can't work on an implicit operator; RSpectra's own error was the cryptic "unsupported matrix type".svds.directed_factor_model()no longer documentsAas an undirected model.How the default and threshold were chosen
I measured failures for 40 seeds × 3 methods, for k = 2 and 4 and n from 6 to 100.
ncv: RSpectra's default fails 79 of 120 times at n = 20 (k = 2). The new rule fails at most once at n ≥ 12.Threshold:
svds()runs are off by at most 2e-3, relative.Warning at
min(n, d) ≤ 25covers every badly wrong case.Tests (
tests/testthat/test-spectra-small-models.R)min(n, d) ≥ 26don't warn.which = "SA","LA"and"LM"matcheigen().optsare forwarded; thencvdefault helper is unit-tested, including respecting a user-suppliedncv.sigmagives an informative error.Five of the six tests fail on
main; "larger models don't warn" is meant to pass on both. Full suite: 2,312 + the new tests pass, with no new warnings elsewhere. Docs were regenerated with roxygen2 7.3.2.Notes:
@noteto the same roxygen blocks.min(n, d) ≤ 25. They currently use n ≥ 30, so they shouldn't warn.🤖 Generated with Claude Code