Repository navigation
Conversation
themavik
reviewed
Mar 23, 2026
themavik
left a comment
There was a problem hiding this comment.
this could blow up if any leading signal eigenvalue is 0 (division in (si-sj)**2/(si*sj)); same log domain as the old loop, but vectorized NaNs may propagate differently—worth parity checks on corner spectra.
Author
|
Closing because an O(n^2) implementation is possible and much much faster! Will open a new PR. |
Author
|
New PR: #34619 |
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.
Reference Issues/PRs
Not sure if these types of performance-related PRs typically have an associated issue. Happy to create one if appropriate.
What does this implement/fix? Explain your changes.
PCA(n_components="mle")will attempt to determine the number of principal components to be retained. This is roughly O(n_features^3), since it iterates overrange(rank)range(i+1, n_features)Where the latter two loops are needed to implement equation 27 in https://proceedings.neurips.cc/paper/2000/file/7503cfacd12053d309b6bed5c89de212-Paper.pdf.
Doing this in Python loops is slow, but appears necessary from a literal interpretation of the equation. However, if we exploit the piecewise nature of the "spectrum" vector, we can treat the computation as two more easily vectorizable (is that a word?) subproblems. The only downside is that we now materialize a rank**2 intermediate matrix, which will have a memory cost, but I don't think this is significant at realistic problem sizes.
For a "small" but informative case of using 5 years of daily returns of S&P500 stocks, and running
Measured
fittime drops from 16.3s to 0.4s on my macbook, which is a 40x speedup. On a gcloud c3 instance (to get an x86 comparison), I see a 50x speedup on the same problem.https://gist.github.com/jberg5/63aaa9aebfb92be8ad9b47fa62ab28dc
AI usage disclosure
I used AI assistance for:
Claude (Opus 4.6) was instrumental here. I asked it to look for vectorization opportunities, and it spotted this one. Its first implementation was good but not great, so I spent a lot of (human!) time understanding and refining, and the code in this PR is something I stand behind.