Repository navigation
fix: Compute PCA variances and OLS TSS with a two-pass scheme - #628
Open
KFilippopolitis wants to merge 1 commit into
Open
KFilippopolitis wants to merge 1 commit into
KFilippopolitis wants to merge 1 commit into
Conversation
PCA (and PCA with transformations) derived sample variances from aggregated sum(x) and sum(x^2), and OLS derived the total sum of squares from sum(y) and sum(y^2). Both cancel catastrophically when the mean is large relative to the spread (e.g. values around 1e8 with unit standard deviation). Aggregate n and sums first to get the global mean, then aggregate sums of squared deviations from that mean. PCA and the PCA-with-transformations wrapper share a new global_mean_std helper. Every worker issues the same aggregation calls in the same order. Add large-offset regression tests and describe the two-round flow in the PCA, PCA with transformations and linear regression docs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Summary
PCA, PCA with transformations, and the OLS total sum of squares computed variance-like quantities from aggregated raw moments:
variances = (sum(x^2) - n * mean^2) / (n - 1)(inFederatedPCA.fitand duplicated in thepca_with_transformationswrapper).tss = sum(y^2) - 2 * mean * sum(y) + n * mean^2.Both cancel catastrophically when the mean is large relative to the spread (e.g. values around 1e8 with unit standard deviation). This PR switches them to the same two-pass scheme already used for Pearson, describe, and the t-tests:
nand sums to get the global mean.Changes
exaflow/algorithms/federated/decomposition/pca.py: newglobal_mean_std(agg_client, X)helper (round 1 sums[n, column sums]in one call, round 2 sums centered squares).FederatedPCA.fituses it. The negative-variance clip is removed because a sum of squares cannot be negative.exaflow/algorithms/exareme3/decomposition/pca_with_transformations.py: reuses the helper instead of its own raw-moment copy. Then <= 1fallback is unchanged.exaflow/algorithms/federated/linear_model/ols.py: drops thesum(y^2)aggregation and aggregatessum((y - mean_y)^2)after the residual sums.PCA.md,PCAWithTransformation.md,LinearRegression.mddescribe the two-round flow.federated/docs/ols.mdno longer listsy'yas aggregated (the commit hook also reformatted that file's Markdown).Tests
New large-offset regression tests (OFFSET = 1e8, 3 simulated workers via
_simulate_federated_execution, rtol 1e-6):test_pca.py:test_global_mean_std_large_offset,test_pca_large_offset,test_pca_with_transformations_large_offset.test_ols.py:test_ols_tss_large_offset(TSS, R², F against statsmodels).Against the old code, the PCA eigenvalue test fails (up to 158% relative error) and the OLS TSS test fails (42% relative error).
128 passed. Other OLS/PCA consumers (two-way ANOVA, cross-validation, pipeline, linear regression wrapper) also pass.
Risk
🤖 Generated with Claude Code