Skip to content

Expm typo fix add log#1271

Merged
you-n-g merged 3 commits intomicrosoft:mainfrom
wan9c9:expm_typo_fix_add_log
Sep 2, 2022
Merged

Expm typo fix add log#1271
you-n-g merged 3 commits intomicrosoft:mainfrom
wan9c9:expm_typo_fix_add_log

Conversation

@wan9c9
Copy link
Contributor

@wan9c9 wan9c9 commented Aug 29, 2022

fixed a typo and added a logger.info to log more info.

Description

Motivation and Context

How Has This Been Tested?

  • Pass the test by running: pytest qlib/tests/test_all_pipeline.py under upper directory of qlib.
  • If you are adding a new feature, test on your own test scripts.

Screenshots of Test Results (if appropriate):

  1. Pipeline test:
  2. Your own tests:

Types of changes

  • Fix bugs
  • Add new feature
  • Update documentation

wan9c9 added 3 commits August 29, 2022 09:33
implemenation from remote.
On branch main
Your branch is behind 'origin/main' by 127 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

Changes to be committed:
       modified:   qlib/data/ops.py

Changes not staged for commit:
       modified:   qlib/contrib/evaluate.py
       modified:   qlib/contrib/strategy/signal_strategy.py
       modified:   qlib/utils/__init__.py
       modified:   qlib/workflow/cli.py
       modified:   qlib/workflow/expm.py

Untracked files:
       .idea/

------------------------ >8 ------------------------
Do not modify or remove the line above.
Everything below it will be ignored.
diff --git a/qlib/data/ops.py b/qlib/data/ops.py
index bdc032c..23db25c 100644
--- a/qlib/data/ops.py
+++ b/qlib/data/ops.py
@@ -32,6 +32,90 @@ except ValueError as e:

 np.seterr(invalid="ignore")

+#################### Change instrument ########################
+# In some case, one may want to change to another instrument when calculating, for example
+# calculate beta of a stock with respect to a market index
+# this would require change the calculation of features from the stock (original instrument) to
+# the index (reference instrument)
+# #############################
+
+
+class ChangeInstrument(ExpressionOps):
+    """Change Instrument Operator
+    In some case, one may want to change to another instrument when calculating, for example, to
+    calculate beta of a stock with respect to a market index.
+    This would require changing the calculation of features from the stock (original instrument) to
+    the index (reference instrument)
+    Parameters
+    ----------
+    instrument: new instrument for which the downstream operations should be performed upon.
+                i.e., SH000300 (CSI300 index), or ^GPSC (SP500 index).
+
+    feature: the feature to be calculated for the new instrument.
+    Returns
+    ----------
+    Expression
+        feature operation output
+    """
+
+    def __init__(self, instrument, feature):
+        self.instrument = instrument
+        self.feature = feature
+
+    def __str__(self):
+        return "{}({},{})".format(type(self).__name__, self.instrument, self.feature)
+
+    def load(self, instrument, start_index, end_index, freq):
+        """load  feature
+
+        Parameters
+        ----------
+        instrument : str
+            instrument code, however, the actual instrument loaded is self.instrument through initialization
+        start_index : str
+            feature start index [in calendar].
+        end_index : str
+            feature end  index  [in calendar].
+        freq : str
+            feature frequency.
+
+        Returns
+        ----------
+        pd.Series
+            feature series: The index of the series is the calendar index
+        """
+        from .cache import H  # pylint: disable=C0415
+
+        # cache
+        args = str(self), self.instrument, start_index, end_index, freq
+        if args in H["f"]:
+            return H["f"][args]
+        if start_index is not None and end_index is not None and start_index > end_index:
+            raise ValueError("Invalid index range: {} {}".format(start_index, end_index))
+        try:
+            series = self._load_internal(self.instrument, start_index, end_index, freq)
+        except Exception as e:
+            get_module_logger("data").debug(
+                f"Loading data error: instrument={instrument}, expression={str(self)}, "
+                f"start_index={start_index}, end_index={end_index}, freq={freq}. "
+                f"error info: {str(e)}"
+            )
+            raise
+        series.name = str(self)
+        H["f"][args] = series
+        return series
+
+    def _load_internal(self, instrument, start_index, end_index, freq):
+        series = self.feature.load(self.instrument, start_index, end_index, freq)
+        return series
+
+    def get_longest_back_rolling(self):
+        return self.feature.get_longest_back_rolling()
+
+    def get_extended_window_size(self):
+        return self.feature.get_extended_window_size()
+
+
 #################### Element-Wise Operator ####################

@@ -1541,6 +1625,7 @@ class TResample(ElemOperator):

 TOpsList = [TResample]
 OpsList = [
+    ChangeInstrument,
     Rolling,
     Ref,
     Max,
@you-n-g
Copy link
Collaborator

you-n-g commented Sep 2, 2022

Thanks!
It looks great now!

@you-n-g you-n-g merged commit 80bf16f into microsoft:main Sep 2, 2022
@you-n-g you-n-g added the documentation Improvements or additions to documentation label Dec 9, 2022
qianyun210603 pushed a commit to qianyun210603/qlib that referenced this pull request Mar 23, 2023
* My own implementation of ChangeInstrument Op. There is a newer, simpler
implemenation from remote.
On branch main
Your branch is behind 'origin/main' by 127 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

Changes to be committed:
       modified:   qlib/data/ops.py

Changes not staged for commit:
       modified:   qlib/contrib/evaluate.py
       modified:   qlib/contrib/strategy/signal_strategy.py
       modified:   qlib/utils/__init__.py
       modified:   qlib/workflow/cli.py
       modified:   qlib/workflow/expm.py

Untracked files:
       .idea/

------------------------ >8 ------------------------
Do not modify or remove the line above.
Everything below it will be ignored.
diff --git a/qlib/data/ops.py b/qlib/data/ops.py
index bdc032c..23db25c 100644
--- a/qlib/data/ops.py
+++ b/qlib/data/ops.py
@@ -32,6 +32,90 @@ except ValueError as e:

 np.seterr(invalid="ignore")

+#################### Change instrument ########################
+# In some case, one may want to change to another instrument when calculating, for example
+# calculate beta of a stock with respect to a market index
+# this would require change the calculation of features from the stock (original instrument) to
+# the index (reference instrument)
+# #############################
+
+
+class ChangeInstrument(ExpressionOps):
+    """Change Instrument Operator
+    In some case, one may want to change to another instrument when calculating, for example, to
+    calculate beta of a stock with respect to a market index.
+    This would require changing the calculation of features from the stock (original instrument) to
+    the index (reference instrument)
+    Parameters
+    ----------
+    instrument: new instrument for which the downstream operations should be performed upon.
+                i.e., SH000300 (CSI300 index), or ^GPSC (SP500 index).
+
+    feature: the feature to be calculated for the new instrument.
+    Returns
+    ----------
+    Expression
+        feature operation output
+    """
+
+    def __init__(self, instrument, feature):
+        self.instrument = instrument
+        self.feature = feature
+
+    def __str__(self):
+        return "{}({},{})".format(type(self).__name__, self.instrument, self.feature)
+
+    def load(self, instrument, start_index, end_index, freq):
+        """load  feature
+
+        Parameters
+        ----------
+        instrument : str
+            instrument code, however, the actual instrument loaded is self.instrument through initialization
+        start_index : str
+            feature start index [in calendar].
+        end_index : str
+            feature end  index  [in calendar].
+        freq : str
+            feature frequency.
+
+        Returns
+        ----------
+        pd.Series
+            feature series: The index of the series is the calendar index
+        """
+        from .cache import H  # pylint: disable=C0415
+
+        # cache
+        args = str(self), self.instrument, start_index, end_index, freq
+        if args in H["f"]:
+            return H["f"][args]
+        if start_index is not None and end_index is not None and start_index > end_index:
+            raise ValueError("Invalid index range: {} {}".format(start_index, end_index))
+        try:
+            series = self._load_internal(self.instrument, start_index, end_index, freq)
+        except Exception as e:
+            get_module_logger("data").debug(
+                f"Loading data error: instrument={instrument}, expression={str(self)}, "
+                f"start_index={start_index}, end_index={end_index}, freq={freq}. "
+                f"error info: {str(e)}"
+            )
+            raise
+        series.name = str(self)
+        H["f"][args] = series
+        return series
+
+    def _load_internal(self, instrument, start_index, end_index, freq):
+        series = self.feature.load(self.instrument, start_index, end_index, freq)
+        return series
+
+    def get_longest_back_rolling(self):
+        return self.feature.get_longest_back_rolling()
+
+    def get_extended_window_size(self):
+        return self.feature.get_extended_window_size()
+
+
 #################### Element-Wise Operator ####################

@@ -1541,6 +1625,7 @@ class TResample(ElemOperator):

 TOpsList = [TResample]
 OpsList = [
+    ChangeInstrument,
     Rolling,
     Ref,
     Max,

* update expm.py

* removed duplicate implementation for ChangeInstrument
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants