fix(ops): Corr crashes in expanding mode (N=0)#2291
Open
UditDewan wants to merge 1 commit into
Open
Conversation
PairRolling documents N=0 as expanding mode and Cov(x, y, 0) works, but Corr(x, y, 0) raises ValueError because the zero-std NaN mask unconditionally calls rolling(0, min_periods=1), which pandas rejects (min_periods 1 must be <= window 0). Use expanding std for the mask when N == 0; the rolling path is unchanged.
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.
Description
Corr(x, y, 0)(expanding mode) crashes withValueError: min_periods 1 must be <= window 0.PairRollingdocumentsN=0as expanding mode (same convention asRolling), andCov(x, y, 0)works. ButCorr._load_internalapplies its zero-std NaN mask with an unconditionalseries.rolling(self.N, min_periods=1).std(), and pandas rejectsrolling(0):This PR computes the mask with
expanding(min_periods=1).std()whenN == 0and keeps the rolling path byte-for-byte identical forN > 0.Motivation and Context
Any expression using expanding correlation, e.g.
Corr($close, ChangeInstrument('SH000300', $close), 0), currently fails, while the equivalentCov(..., 0)succeeds. The masking line was added to suppress spurious correlations of near-constant series and simply never handled the expanding branch.How Has This Been Tested?
pytest qlib/tests/test_all_pipeline.pyunder upper directory ofqlib.Added
tests/ops/test_pair_rolling.py(no data download needed — uses a stubExpression):Corrmatchespandas.Series.expanding().corr()(previously: crash)N > 0) results are unchangedScreenshots of Test Results (if appropriate):
Types of changes