1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-14 00:06:58 +08:00

optimize_CI (#1314)

This commit is contained in:
Linlang
2022-10-20 08:38:05 +08:00
committed by GitHub
parent 3c62d131a5
commit 1861c8edaf
11 changed files with 70 additions and 78 deletions

View File

@@ -56,39 +56,8 @@ def train(uri_path: str = None):
ic = sar.load("ic.pkl")
ric = sar.load("ric.pkl")
return pred_score, {"ic": ic, "ric": ric}, rid
def train_with_sigana(uri_path: str = None):
"""train model followed by SigAnaRecord
Returns
-------
pred_score: pandas.DataFrame
predict scores
performance: dict
model performance
"""
model = init_instance_by_config(CSI300_GBDT_TASK["model"])
dataset = init_instance_by_config(CSI300_GBDT_TASK["dataset"])
# start exp
with R.start(experiment_name="workflow_with_sigana", uri=uri_path):
R.log_params(**flatten_dict(CSI300_GBDT_TASK))
model.fit(dataset)
recorder = R.get_recorder()
sr = SignalRecord(model, dataset, recorder)
sr.generate()
pred_score = sr.load("pred.pkl")
# predict and calculate ic and ric
sar = SigAnaRecord(recorder)
sar.generate()
ic = sar.load("ic.pkl")
ric = sar.load("ric.pkl")
uri_path = R.get_uri()
return pred_score, {"ic": ic, "ric": ric}, uri_path
return pred_score, {"ic": ic, "ric": ric}, rid, uri_path
def fake_experiment():
@@ -186,19 +155,13 @@ class TestAllFlow(TestAutoData):
shutil.rmtree(cls.URI_PATH.lstrip("file:"))
@pytest.mark.slow
def test_0_train_with_sigana(self):
TestAllFlow.PRED_SCORE, ic_ric, uri_path = train_with_sigana(self.URI_PATH)
def test_0_train(self):
TestAllFlow.PRED_SCORE, ic_ric, TestAllFlow.RID, uri_path = train(self.URI_PATH)
self.assertGreaterEqual(ic_ric["ic"].all(), 0, "train failed")
self.assertGreaterEqual(ic_ric["ric"].all(), 0, "train failed")
@pytest.mark.slow
def test_1_train(self):
TestAllFlow.PRED_SCORE, ic_ric, TestAllFlow.RID = train(self.URI_PATH)
self.assertGreaterEqual(ic_ric["ic"].all(), 0, "train failed")
self.assertGreaterEqual(ic_ric["ric"].all(), 0, "train failed")
@pytest.mark.slow
def test_2_backtest(self):
def test_1_backtest(self):
analyze_df = backtest_analysis(TestAllFlow.PRED_SCORE, TestAllFlow.RID, self.URI_PATH)
self.assertGreaterEqual(
analyze_df.loc(axis=0)["excess_return_with_cost", "annualized_return"].values[0],
@@ -208,7 +171,7 @@ class TestAllFlow(TestAutoData):
self.assertTrue(not analyze_df.isna().any().any(), "backtest failed")
@pytest.mark.slow
def test_3_expmanager(self):
def test_2_expmanager(self):
pass_default, pass_current, uri_path = fake_experiment()
self.assertTrue(pass_default, msg="default uri is incorrect")
self.assertTrue(pass_current, msg="current uri is incorrect")
@@ -217,10 +180,9 @@ class TestAllFlow(TestAutoData):
def suite():
_suite = unittest.TestSuite()
_suite.addTest(TestAllFlow("test_0_train_with_sigana"))
_suite.addTest(TestAllFlow("test_1_train"))
_suite.addTest(TestAllFlow("test_2_backtest"))
_suite.addTest(TestAllFlow("test_3_expmanager"))
_suite.addTest(TestAllFlow("test_0_train"))
_suite.addTest(TestAllFlow("test_1_backtest"))
_suite.addTest(TestAllFlow("test_2_expmanager"))
return _suite

View File

@@ -11,7 +11,24 @@ from qlib.contrib.workflow import MultiSegRecord, SignalMseRecord
from qlib.utils import init_instance_by_config, flatten_dict
from qlib.workflow import R
from qlib.tests import TestAutoData
from qlib.tests.config import CSI300_GBDT_TASK
from qlib.tests.config import GBDT_MODEL, get_dataset_config, CSI300_MARKET
CSI300_GBDT_TASK = {
"model": GBDT_MODEL,
"dataset": get_dataset_config(
train=("2020-05-01", "2020-06-01"),
valid=("2020-06-01", "2020-07-01"),
test=("2020-07-01", "2020-08-01"),
handler_kwargs={
"start_time": "2020-05-01",
"end_time": "2020-08-01",
"fit_start_time": "<dataset.kwargs.segments.train.0>",
"fit_end_time": "<dataset.kwargs.segments.train.1>",
"instruments": CSI300_MARKET,
},
),
}
def train_multiseg(uri_path: str = None):

View File

@@ -19,10 +19,10 @@ class TestDataset(TestAutoData):
"class": "Alpha158",
"module_path": "qlib.contrib.data.handler",
"kwargs": {
"start_time": "2008-01-01",
"start_time": "2017-01-01",
"end_time": "2020-08-01",
"fit_start_time": "2008-01-01",
"fit_end_time": "2014-12-31",
"fit_start_time": "2017-01-01",
"fit_end_time": "2017-12-31",
"instruments": "csi300",
"infer_processors": [
{"class": "FilterCol", "kwargs": {"col_list": ["RESI5", "WVMA5", "RSQR5"]}},
@@ -36,9 +36,9 @@ class TestDataset(TestAutoData):
},
},
segments={
"train": ("2008-01-01", "2014-12-31"),
"valid": ("2015-01-01", "2016-12-31"),
"test": ("2017-01-01", "2020-08-01"),
"train": ("2017-01-01", "2017-12-31"),
"valid": ("2018-01-01", "2018-12-31"),
"test": ("2019-01-01", "2020-08-01"),
},
)
tsds_train = tsdh.prepare("train", data_key=DataHandlerLP.DK_L) # Test the correctness
@@ -63,13 +63,13 @@ class TestDataset(TestAutoData):
tsds[len(tsds) - 1]
# 2) sample by <datetime,instrument> index
data_from_ds = tsds["2016-12-31", "SZ300315"]
data_from_ds = tsds["2017-12-31", "SZ300315"]
# Check the data
# Get data from DataFrame Directly
data_from_df = (
tsdh.handler.fetch(data_key=DataHandlerLP.DK_L)
.loc(axis=0)["2015-01-01":"2016-12-31", "SZ300315"]
.loc(axis=0)["2017-01-01":"2017-12-31", "SZ300315"]
.iloc[-30:]
.values
)