Skip to content

fix: mask degenerate expanding rsquare windows - #2325

Closed
Parafee41 (koriyoshi2041) wants to merge 1 commit into
microsoft:mainfrom
koriyoshi2041:fix/expanding-rsquare-constant-window
Closed

fix: mask degenerate expanding rsquare windows#2325
Parafee41 (koriyoshi2041) wants to merge 1 commit into
microsoft:mainfrom
koriyoshi2041:fix/expanding-rsquare-constant-window

Conversation

@koriyoshi2041

Copy link
Copy Markdown

Problem

Rsquare(feature, 0) uses the expanding Cython kernel but, unlike the rolling path, does not mask windows where the feature standard deviation is effectively zero. Floating-point cancellation can therefore leak inf or misleading finite R-squared values into expanding features.

Fixes #2297.

Fix

Apply the same std ~= 0 guard used by rolling R-squared to the expanding path, using Series.expanding(min_periods=1).std() so the mask follows each expanding window.

Test

  • Added a regression for the reported near-constant series; every degenerate expanding window is now NaN.
  • Added a varying-series case to confirm valid expanding R-squared values remain finite.
  • python -m pytest tests/ops/test_rolling_operator.py -q — 2 passed.
  • Black check on the new test file and git diff --check passed.

Risk

Low. The change is limited to the N == 0 R-squared branch and reuses the existing rolling-path tolerance (atol=2e-05). Valid non-degenerate windows and all other rolling operators are unchanged.

@koriyoshi2041

Copy link
Copy Markdown
Author

Closing as duplicate. #2298 already contains the same fix plus regression coverage, and #2310 also covers the code change. I should have caught those existing PRs before opening this one.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Rsquare(N=0) expanding path leaks inf/garbage on near-constant windows (rolling path is guarded)

1 participant