Skip to content

fix: mask degenerate windows in Rsquare expanding path (N=0) - #2341

Open
tongflau-dongzhu wants to merge 1 commit into
microsoft:mainfrom
tongflau-dongzhu:fix/rsquare-expanding-degenerate-windows
Open

fix: mask degenerate windows in Rsquare expanding path (N=0)#2341
tongflau-dongzhu wants to merge 1 commit into
microsoft:mainfrom
tongflau-dongzhu:fix/rsquare-expanding-degenerate-windows

Conversation

@tongflau-dongzhu

Copy link
Copy Markdown

What

Mirrors the existing near-constant-window NaN guard from the rolling branch onto the expanding (N == 0) branch of Rsquare in qlib/data/ops.py.

Near-constant input makes �ar(y) ~= 0, so expanding_rsquare (kernel:
um / sqrt(var_x * var_y)) leaks inf / garbage values on the expanding path, while the rolling path already masks those windows:

`python

rolling branch (already guarded):

series.loc[np.isclose(_series.rolling(self.N, min_periods=1).std(), 0, atol=2e-05)] = np.nan
`

This PR applies the equivalent guard with expanding(min_periods=1).std() on the N == 0 branch.

Tests

Added ests/ops/test_rsquare.py:

  • expanding path: degenerate windows fully inside a constant run are NaN, no inf anywhere
  • expanding path on ordinary varying input stays finite
  • rolling path keeps its existing guard (regression safety)

The tests use a mocked feature (no data download / no qlib.init needed).

Fixes #2297

Near-constant input made var(y) ~= 0, so the expanding rsquare kernel leaked
inf / garbage values. The NaN guard (isclose(std, 0)) existed only on the
rolling branch; mirror it on the expanding branch.

Fixes microsoft#2297
@tongflau-dongzhu

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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