fix(qlib): prevent MultiIndex duplication from groupby().rolling() pattern + custom baseline features#1400
Closed
shin4 wants to merge 1 commit into
Closed
fix(qlib): prevent MultiIndex duplication from groupby().rolling() pattern + custom baseline features#1400shin4 wants to merge 1 commit into
shin4 wants to merge 1 commit into
Conversation
This PR introduces a **preventive fix** for pandas MultiIndex issues caused by `groupby().rolling()` patterns in LLM-generated factor code, complementing the remedial approach in microsoft#1375. Fixes microsoft#678 ## Problem When LLM generates factor code with rolling operations on MultiIndex data (index: `['datetime', 'instrument']`), a common pattern produces 3-level indices instead of the expected 2-level: ```python # ❌ WRONG - Creates 3-level index: ['instrument', 'datetime', 'instrument'] ma_20 = volume.groupby(level='instrument').rolling(window=20).mean() # ValueError: The name instrument occurs multiple times ``` This causes `pd.concat()` to fail with: ``` AssertionError: Length of new_levels (3) must be <= self.nlevels (2) ``` ## Solution We provide a **two-layer fix**: ### Layer 1: Preventive Code Fix (This PR) Auto-detect and fix the problematic pattern in generated factor code **before execution**: ```python # rdagent/scenarios/qlib/developer/utils.py def _fix_groupby_rolling_pattern(code: str) -> str: """Convert groupby().rolling().{op}() to groupby().transform(lambda x: x.rolling().{op}())""" # Pattern: .groupby(level='instrument').rolling(window=N).mean() # Fixed: .groupby(level='instrument').transform(lambda x: x.rolling(window=N).mean()) ``` **Advantages**: - ✅ Fixes root cause - factor code produces correct 2-level index from the start - ✅ No data loss or incorrect index ordering - ✅ Factor values can be used in subsequent operations (division, etc.) ### Layer 2: Remedial Index Fix (Complementary with microsoft#1375) Normalize index levels before concat as a **fallback safety net**: - PR microsoft#1375's approach handles any remaining edge cases - Both approaches work together for robustness ## Changes ### Core Fix - `rdagent/scenarios/qlib/developer/utils.py`: Add `_fix_groupby_rolling_pattern()` function - Auto-fixes `groupby().rolling().{mean|sum|std|min|max}()` patterns - Converts to `groupby().transform(lambda x: x.rolling().{op}())` - Applied before factor code execution ### Prompt Enhancement - `rdagent/scenarios/qlib/experiment/prompts.yaml`: Add documentation for correct pattern - Guides LLM to generate correct code from the start - Reduces occurrence of the problematic pattern ### Configuration Updates - `rdagent/scenarios/qlib/experiment/factor_template/conf_*.yaml`: Use local qlib_bin data, CSI500 market - `rdagent/oai/llm_conf.py`: Add `request_timeout`, `extra_headers` for LLM API flexibility - `rdagent/oai/backend/litellm.py`: Support API base/key override, custom headers ### CLI Enhancement - `rdagent/app/cli.py`: Add `--base_features_path` for custom baseline factors ### Bug Fix - `rdagent/components/runner/__init__.py`: Include `base_feature_codes` in cache key ## Additional Features ### Custom Baseline Factors - `baseline_features/`: 9 custom factors (Volatility, Momentum, etc.) + Alpha20 config - Enables starting factor evolution from optimized baseline ## Testing - All offline tests pass: `pytest -m offline` - Manual testing with qlib fin_factor scenario - Verified factor data produces correct 2-level MultiIndex ## Comparison with microsoft#1375 | Aspect | This PR (Preventive) | microsoft#1375 (Remedial) | |--------|---------------------|------------------| | Fix timing | Before code execution | Before concat | | Root cause | ✅ Yes |⚠️ Partially | | Data integrity | ✅ Preserved |⚠️ May drop level incorrectly | | Index ordering | ✅ Correct |⚠️ May need swaplevel | | Complementary | ✅ Works together | ✅ Works together | **Recommendation**: Merge both for defense-in-depth. ## Related - Fixes microsoft#678 - Complements microsoft#1375
Author
|
@microsoft-github-policy-service agree |
Author
|
Superseded by #1401 - this PR contained unrelated changes. A new clean PR has been created with only the core MultiIndex fix. |
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.
Summary
This PR introduces a preventive fix for pandas MultiIndex issues caused by
groupby().rolling()patterns in LLM-generated factor code, complementing the remedial approach in #1375.Fixes #678
Problem
When LLM generates factor code with rolling operations on MultiIndex data (index:
['datetime', 'instrument']), a common pattern produces 3-level indices instead of the expected 2-level:This causes
pd.concat()to fail with:Solution
We provide a two-layer fix:
Layer 1: Preventive Code Fix (This PR)
Auto-detect and fix the problematic pattern in generated factor code before execution:
Advantages:
Layer 2: Remedial Index Fix (Complementary with #1375)
Normalize index levels before concat as a fallback safety net:
Changes
Core Fix
rdagent/scenarios/qlib/developer/utils.py: Add_fix_groupby_rolling_pattern()functiongroupby().rolling().{mean|sum|std|min|max}()patternsgroupby().transform(lambda x: x.rolling().{op}())Prompt Enhancement
rdagent/scenarios/qlib/experiment/prompts.yaml: Add documentation for correct patternConfiguration Updates
rdagent/scenarios/qlib/experiment/factor_template/conf_*.yaml: Use local qlib_bin data, CSI500 marketrdagent/oai/llm_conf.py: Addrequest_timeout,extra_headersfor LLM API flexibilityrdagent/oai/backend/litellm.py: Support API base/key override, custom headersCLI Enhancement
rdagent/app/cli.py: Add--base_features_pathfor custom baseline factorsBug Fix
rdagent/components/runner/__init__.py: Includebase_feature_codesin cache keyAdditional Features
Custom Baseline Factors
baseline_features/: 9 custom factors (Volatility, Momentum, etc.) + Alpha20 configTesting
pytest -m offlineComparison with #1375
Recommendation: Merge both for defense-in-depth.
Related
📚 Documentation preview 📚: https://RDAgent--1400.org.readthedocs.build/en/1400/