Skip to content
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

Add util function to help automatically get horizon #1509

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/benchmarks/TRA/workflow_config_tra_Alpha158.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ task:
valid: [2015-01-01, 2016-12-31]
test: [2017-01-01, 2020-08-01]
seq_len: 60
horizon: 2
input_size:
num_states: *num_states
batch_size: 1024
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ task:
valid: [2015-01-01, 2016-12-31]
test: [2017-01-01, 2020-08-01]
seq_len: 60
horizon: 2
input_size:
num_states: *num_states
batch_size: 1024
Expand Down
1 change: 0 additions & 1 deletion examples/benchmarks/TRA/workflow_config_tra_Alpha360.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ task:
valid: [2015-01-01, 2016-12-31]
test: [2017-01-01, 2020-08-01]
seq_len: 60
horizon: 2
input_size: 6
num_states: *num_states
batch_size: 1024
Expand Down
8 changes: 8 additions & 0 deletions qlib/contrib/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import warnings
import numpy as np
import pandas as pd
from qlib.utils.data import guess_horizon
from qlib.utils import init_instance_by_config

from qlib.data.dataset import DatasetH

Expand Down Expand Up @@ -130,6 +132,12 @@ def __init__(
input_size=None,
**kwargs,
):
if horizon == 0:
# Try to guess horizon
if isinstance(handler, (dict, str)):
handler = init_instance_by_config(handler)
label = handler.data_loader.fields["label"][0][0]
horizon = guess_horizon(label)

assert num_states == 0 or horizon > 0, "please specify `horizon` to avoid data leakage"
assert memory_mode in ["sample", "daily"], "unsupported memory mode"
Expand Down
20 changes: 19 additions & 1 deletion qlib/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
"""
This module covers some utility functions that operate on data or basic object
"""
import re
from copy import deepcopy
from typing import List, Union
import pandas as pd

import numpy as np
import pandas as pd


def robust_zscore(x: pd.Series, zscore=False):
Expand Down Expand Up @@ -103,3 +105,19 @@ def update_config(base_config: dict, ext_config: Union[dict, List[dict]]):
# one of then are not dict. Then replace
base_config[key] = ec[key]
return base_config


def guess_horizon(label):
"""
Try to guess the horizon by parsing label
"""
regex = r"Ref\(\s*\$[a-zA-Z]+,\s*-(\d+)\)"
horizon_list = [int(x) for x in re.findall(regex, label)]

if len(horizon_list) == 0:
return 0
max_horizon = max(horizon_list)
# Unlikely the label doesn't use future information
if max_horizon < 2:
return 0
return max_horizon + 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it return max_horizon+1 here instead of return max_horizon or return max_horizon-min_horizon?

Copy link
Contributor Author

@chenditc chenditc May 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is consistent with exisiting deinition of "horizon" in other code like DDG-DA and TRA. There is no official documentation, TRA workflow config use max_horizon to truncate and DDG-DA use max_horizon + 1. I use max_horizon + 1 here just to make things safe.

Any suggestion from qlib team?

20 changes: 20 additions & 0 deletions tests/misc/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from qlib.log import TimeInspector
from qlib.constant import REG_CN, REG_US, REG_TW
from qlib.utils.time import cal_sam_minute as cal_sam_minute_new, get_min_cal, CN_TIME, US_TIME, TW_TIME
from qlib.utils.data import guess_horizon

REG_MAP = {REG_CN: CN_TIME, REG_US: US_TIME, REG_TW: TW_TIME}

Expand Down Expand Up @@ -112,5 +113,24 @@ def gen_args(cal: List):
cal_sam_minute_new(*args, region=region)


class DataUtils(TestCase):
@classmethod
def setUpClass(cls):
init()

def test_guess_horizon(self):
label = "Ref($close, -2) / Ref($close, -1) - 1"
result = guess_horizon(label)
assert result == 3

label = "Ref($close, -5) / Ref($close, -1) - 1"
result = guess_horizon(label)
assert result == 6

label = "Ref($close, -1) / Ref($close, -1) - 1"
result = guess_horizon(label)
assert result == 0


if __name__ == "__main__":
unittest.main()