Skip to content

[WIP] use more robust mean online computation in StandardScaler - #11549

Closed
agramfort wants to merge 2 commits into
scikit-learn:mainfrom
agramfort:fix_scaler_overflow
Closed

agramfort wants to merge 2 commits into
scikit-learn:mainfrom
agramfort:fix_scaler_overflow

Conversation

@agramfort

Copy link
Copy Markdown
Member

Reference Issues/PRs

Closes #5602

What does this implement/fix? Explain your changes.

use parallel algo from https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm

I still new to check if same problem occurs on sparse matrices and for variance... which I suspect

assert_array_equal(n, A.shape[0])
assert_array_almost_equal(A.mean(axis=0), mean)
assert_allclose(A.mean(axis=0), mean, rtol=1e-12)
assert_greater(tol, np.abs(stable_var(A) - var).max())

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this test comes from agramfort@6a5a2f7 by @giorgiop

Comment thread sklearn/utils/extmath.py
delta = new_mean - last_mean
updated_mean = \
last_mean + (delta * new_sample_count) / updated_sample_count # fixes test
# (last_mean * last_sample_count + new_sum) / updated_sample_count # breaks stability test

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this is line is how it was before and is necessary to have the stability test below to pass. However this line is the problem for the issue reported as last_mean * last_sample_count will explode.

So I feel a bit stuck. Either I change the test or ... Any thought?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So you're saying last_mean + (delta * new_sample_count) / updated_sample_count is unstable, but avoids overflow?

@thomasjpfan thomasjpfan Mar 28, 2019 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Some notes on this issue: (In the context of our test_incremental_variance_numerical_stability test)

The mean in this PR causes the tolerance in the variance to go to ~458, which is up from the original 177. All it takes to get ~458 is by factoring out last_sample_count:

(last_mean + new_sum/last_sample_count)*last_sample_count / updated_sample_count

This PRs updated_means differ from master on the order of 10-9, which is enough to cause the instability in the variance. (The variance is on the order of 10+15)

@agramfort agramfort changed the title use more robust mean online computation in StandardScaler [WIP] use more robust mean online computation in StandardScaler Jul 16, 2018
@jnothman

jnothman commented Oct 7, 2018

Copy link
Copy Markdown
Member

Is this something you're still completing, @agramfort?

@agramfort

Copy link
Copy Markdown
Member Author

@jnothman please take over as it cannot be priority for me right now :(

@agramfort

agramfort commented Mar 28, 2019 via email

Copy link
Copy Markdown
Member Author

@dariomx

dariomx commented Aug 27, 2019

Copy link
Copy Markdown

Hello folks,

Any workaround meanwhile this gets fixed? I do not have an online source, hence I would not bother using batch flavor. But sure if that is an option in StandardScaler ... or maybe I should just to the scaling manually on numpy or something meanwhile.

Thanks.

@thomasjpfan

Copy link
Copy Markdown
Member

This PR uses the parallel algorithm which is less numerically stable as described here: https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm

@agramfort

Copy link
Copy Markdown
Member Author

indeed. As far as I remember my fix was breaking some test. Either we relax the tests or I am not sure what to do here.

@lorentzenchr

Copy link
Copy Markdown
Member

Seems this is not the way to fix the issue, therefore closing.

@giorgiop

Copy link
Copy Markdown
Contributor

Good to see someone picking up my issue from October 2015. Never give up! @lorentzenchr 👏

@lorentzenchr

Copy link
Copy Markdown
Member

@giorgiop I did not pick up the issue you opened. Just triaging this PR as not the way to go. On the other side: you had 10 years to fix it, too - in case it mattered.

tatopenn-cell added a commit to tatopenn-cell/scikit-learn that referenced this pull request Oct 27, 2026
The cumulative-sum formula reconstructs last_sum = last_mean *
last_sample_count and new_sum, both of which overflow on long
streams with large values. The delta-based update of Chan, Golub
and LeVeque (1979) is algebraically equivalent but never
reconstructs the sum.

This fixes the mean and the variance in one pass. The previous
attempt (scikit-learn#11549) fixed only the mean and left the cross-term of the
variance untouched, which is why it was closed as incomplete.

Two non-regression tests:
- test_incremental_mean_and_var_large_values_no_overflow
- test_incremental_mean_and_var_constant_values

Fixes scikit-learn#5602.
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.

BUG: StandardScaler partial_fit overflows

7 participants