fix(ops): guard Rsquare expanding (N==0) path against degenerate windows#2298
Open
DogInfantry wants to merge 1 commit into
Open
fix(ops): guard Rsquare expanding (N==0) path against degenerate windows#2298DogInfantry wants to merge 1 commit into
DogInfantry wants to merge 1 commit into
Conversation
Rsquare masks windows whose std is ~0 to NaN so that the degenerate 0/0 regression (a near-constant window) does not leak `inf` or a spurious finite R^2 from the Cython kernel. This guard was only applied on the rolling (`N != 0`) branch; the expanding (`N == 0`) branch was unguarded, so `Rsquare($feature, 0)` returned `inf`/garbage on near-constant windows, silently poisoning downstream features and models (qlib/data/ops.py also sets `np.seterr(invalid="ignore")`, hiding the warning). Apply the same std~0 -> NaN mask on the expanding branch, using expanding std. Added tests/ops/test_rsquare.py (fails before this change, passes after).
d5ef304 to
9905d6d
Compare
Author
|
@microsoft-github-policy-service agree |
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.
Fixes #2297
Problem
Rsquaremasks windows whose std is ≈0 toNaNso a degenerate 0/0 regression(near-constant window) does not leak
inf/garbage R² out of the Cython kernel.That guard was only on the rolling (
N != 0) branch; the expanding (N == 0)branch was unguarded, so
Rsquare($feature, 0)returnedinf/spurious finitevalues on near-constant windows.
qlib/data/ops.pyalso setsnp.seterr(invalid="ignore"), so the bad values propagate silently intofeatures (Alpha158/Alpha360) and models.
Fix
Mirror the existing guard on the expanding branch, using expanding std:
Slope/Resiare unaffected — they divide by the x-variance (index1..N),which is always well-conditioned; only
Rsquaredivides by the y-variance.Test
Adds
tests/ops/test_rsquare.pyexercisingRsquare._load_internaldirectly(no market data needed). It fails before this change and passes after: