Repository navigation
FIX: prevent overflow in _incremental_mean_and_var - #35249
tatopenn-cell wants to merge 3 commits into
Conversation
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.
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) never reconstructs the sum. The fix uses the cumulative-sum formula when it does not overflow, and falls back to the delta-based update when it does. This keeps the precision of the cumulative-sum formula on normal inputs (the test_incremental_variance_numerical_stability test) and fixes the overflow on large values (the issue scikit-learn#5602 reproducer). Two non-regression tests: - test_incremental_mean_and_var_large_values_no_overflow - test_incremental_mean_and_var_constant_values Fixes scikit-learn#5602.
ruff check on extmath.py reports a pre-existing set of errors (lines 568, 775, 908, 998) and one new error introduced by this PR at line 1289 (E501, 92 > 88). The new error is fixed here by splitting the assignment across two lines. The pre-existing errors are left untouched, to keep the scope clean.
|
Thank you for opening your first pull request to scikit-learn! 🎉 To help get your contribution reviewed, please make sure that:
|
❌ Linting issuesThis PR is introducing linting issues. Here's a summary of the issues. Note that you can avoid having linting issues by enabling You can see the details of the linting issues under the
|
Fixes #5602.
What does this implement/fix?
StandardScaler.partial_fitkeeps the cumulative sum of the samples(
last_sum = last_mean * last_sample_count) and the cross-term of thevariance. Both overflow on long streams or on values with a large
magnitude, and the mean becomes
inf.The fix keeps the cumulative-sum formula when it does not overflow
(it is more precise), and falls back to the delta-based update of Chan,
Golub and LeVeque (1979) when the cumulative sum overflows:
The formula is algebraically equivalent to the cumulative-sum one, but it
never reconstructs the sum. This is a deliberate deviation from a pure
replacement: a pure replacement of the mean formula with the delta-based
one loses precision on
test_incremental_variance_numerical_stability.The fallback is declared in the docstring of
_incremental_mean_and_var.The previous attempt (#11549) fixed the mean but left the variance
cross-term untouched, which is why it was closed. This PR fixes both.
The patch is written against the array API (
xp), notnp, so it keepsworking on the non-NumPy backends that
_incremental_mean_and_varalreadysupports.
Tests
Two non-regression tests in
sklearn/utils/tests/test_extmath.py:test_incremental_mean_and_var_large_values_no_overflow: thereproducer of the issue. On
main, the mean becomesinf. With thefix, it is finite and matches
fit()within1e-10.test_incremental_mean_and_var_constant_values: constant data hasvariance 0, matching
fit(). This is a regression check, not a proofof the fix: it passes on
maintoo.The first test fails on
main(the mean isinf) and passes on thisbranch.
Full suite:
pytest sklearn/utils/tests/test_extmath.py -q-> 262passed, 45 skipped.
Full analysis
The case study lives at
https://github.com/tatopenn-cell/Dense-Evolution-Discovery/tree/case/scikit-overflow/experiments/scikit_overflow
(full write-up, the paper, the numbers, the reference to Eq. 2.1a and
2.1b of STAN-CS-79-773).
The pytest failure on
mainis captured inhttps://github.com/tatopenn-cell/Dense-Evolution-Discovery/blob/case/scikit-overflow/experiments/scikit_overflow/main_failure.txt
AI usage disclosure
I used an AI assistant to explore the code, run the experiments, and draft
the patch. I reviewed every line, ran the tests locally, and I can explain
every change. The delta-based update is Eq. 2.1b of Chan, Golub, LeVeque
(1979), page 4 of the Stanford tech report STAN-CS-79-773.
Any other comments
The
_incremental_mean_and_vardocstring already cites Chan, Golub, andLeVeque. The change is in the same function the docstring describes.
The 4 ruff errors in
extmath.py(lines 568, 775, 908, 998) and the 2mypy errors (scipy stubs, Cython extension) are pre-existing on
mainand are not introduced by this PR.
Tagging @lorentzenchr and @EdenRochmanSharabi for review.