Skip to content

PCA: Vectorize Minka's MLE implementation for 40-50x speedup on most problem sizes - #33602

Closed
jberg5 wants to merge 7 commits into
scikit-learn:mainfrom
jberg5:pca-mle-perf
Closed

jberg5 wants to merge 7 commits into
scikit-learn:mainfrom
jberg5:pca-mle-perf

Conversation

@jberg5

@jberg5 jberg5 commented Mar 22, 2026 •

Copy link
Copy Markdown

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 over

  • every possible rank in n_features
  • range(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

pca = PCA(n_components="mle")
# data.shape = (1255, 490), I dropped a few tickers with missing data
pca.fit(data)

Measured fit time 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

image

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

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.

@themavik themavik left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jberg5

jberg5 commented Jul 31, 2026

Copy link
Copy Markdown
Author

Closing because an O(n^2) implementation is possible and much much faster! Will open a new PR.

@jberg5

jberg5 commented Jul 31, 2026

Copy link
Copy Markdown
Author

New PR: #34619

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants