-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug fix for Rank and WMA operators #1228
Conversation
… to 0-1, not length of array; 2) for (linear) weighted MA(n), weight should be n, n-1, ..., 1 instead of n-1, ..., 0
@you-n-g |
Nice shot! |
Thanks for reply. Updated as you suggested. Also corrected some wrong param list comments. |
@you-n-g any feedback please? |
@qianyun210603 |
@you-n-g It's because the base 'microsoft:main' on Aug 13, which was the base of my pull request, had failed CI . |
@you-n-g
I would guess it is caused by too low pandas version which rank has not been implemented in Rolling. |
Dig in some further, it looks the py3.7 tests fails because Therefore we need to fall back to the original scipy solution for rank for py3.7. Alternative would be drop the py37 support and use native pandas Let me know your thought. thx. |
@you-n-g any thoughts please? |
qlib/data/ops.py
Outdated
@@ -1154,18 +1148,32 @@ class Rank(Rolling): | |||
|
|||
def __init__(self, feature, N): | |||
super(Rank, self).__init__(feature, N, "rank") | |||
major_version, minor_version, *_ = pd.__version__.split(".") | |||
self._load_internal = ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would recommend implementing it as a method to override the parent method instead of setting the attribute for the following reasons.
- fewer attributes will make it simpler.
- It will not work in some special cases (for example, someone dump an instance of the operator in low version of pandas and load it in high version of pandas)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
think of a way to use hasattr
instead of version check. See latest commit. let me know if you have better idea. thx.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any update/feedback please?
@you-n-g any progress please? |
@you-n-g any comments on this? |
Sorry for the late response. |
* bug fix: 1) 100 should be used to scale down percentileofscore return to 0-1, not length of array; 2) for (linear) weighted MA(n), weight should be n, n-1, ..., 1 instead of n-1, ..., 0 * use native pandas fucntion for rank * remove useless import * require pandas 1.4+ * rank for py37+pandas 1.3.5 compatibility * lint improvement * lint black fix * use hasattr instead of version to check whether rolling.rank is implemented
* bug fix: 1) 100 should be used to scale down percentileofscore return to 0-1, not length of array; 2) for (linear) weighted MA(n), weight should be n, n-1, ..., 1 instead of n-1, ..., 0 * use native pandas fucntion for rank * remove useless import * require pandas 1.4+ * rank for py37+pandas 1.3.5 compatibility * lint improvement * lint black fix * use hasattr instead of version to check whether rolling.rank is implemented
Description
Motivation and Context
For 1): I would guess the goal is to scale the ranking result between 0 and 1. According to the scipy document,
percentileofscore
it doesn't make sense to divide by length to array size, instead, should divide by 100.
For 2): The common convention of linear weighted MA with window size d should be weighted by d, d-1, ..., 1, instead of d-1, d-2, ..., 0, see e.g. https://www.fidelity.com/learning-center/trading-investing/technical-analysis/technical-indicator-guide/wma and the
decay_linear
function in famous 101 Formulaic Alphas by Zura Kakushadze from WorldQuant.Though convention problem is arguable I think it's better to be more intuitive.
How Has This Been Tested?
pytest qlib/tests/test_all_pipeline.py
under upper directory ofqlib
.Screenshots of Test Results (if appropriate):
Pipeline test:

Your own tests:
Types of changes