Skip to content

Make factor model spectra reliable for small models - #62

Draft
alexpghayes wants to merge 1 commit into
mainfrom
fix/model-spectra-rspectra
Draft

alexpghayes wants to merge 1 commit into
mainfrom
fix/model-spectra-rspectra

Conversation

@alexpghayes

Copy link
Copy Markdown
Collaborator

Fixes bugs #1 and #5 from the bug hunt: wrong spectra for small models, and eigs_sym() ignoring which.

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 default ncv = min(dim, max(2k + 1, 20)) makes the Krylov subspace nearly the whole space for small models. On main at n = 20, over 40 seeds:

Method main This PR
directed svds() 10 / 40 correct, 30 silently wrong 40 / 40
undirected eigs_sym() 25 / 40 correct, 15 silently wrong 40 / 40
undirected svds() 7 / 40 correct, 33 errors (TridiagEigen: eigen decomposition failed) 40 / 40

The same ncv dependence reproduces both examples in RSpectra#20: each fails at the default ncv and 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 dsCMatrix method for eigs_sym(), is left to RSpectra.

Changes (R/expected-spectra.R)

  • Smaller default ncv: min(dim − 1, max(2k + 1, 10)), unless the user passes opts$ncv, and only when k < ncv ≤ dim is possible.
  • Warning when min(n, d) ≤ 25. It has class fastRG_warning_small_model_spectra, links to RSpectra#20, and suggests eigen() / svd() of as.matrix(expectation(model)).
  • Forwarding: which, opts and ... 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".
  • Docs: a new "Accuracy for small models" section is shared by all three methods. svds.directed_factor_model() no longer documents A as 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:

    • Seriously wrong answers (relative error up to 8.5) occur only at n ≤ 20, under every rule.
    • At n = 30 a few undirected svds() runs are off by at most 2e-3, relative.
    • Nothing failed at n = 25, 50 or 100.

    Warning at min(n, d) ≤ 25 covers every badly wrong case.

Tests (tests/testthat/test-spectra-small-models.R)

  • n = 20 models are correct for 15 seeds across all three methods, and warn.
  • The warning links to the issue, and fires based on the smaller dimension of rectangular models. Models with min(n, d) ≥ 26 don't warn.
  • which = "SA", "LA" and "LM" match eigen().
  • opts are forwarded; the ncv default helper is unit-tested, including respecting a user-supplied ncv.
  • sigma gives 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:

🤖 Generated with Claude Code

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant