1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-15 00:36:55 +08:00

Update test scipts

This commit is contained in:
Jactus
2020-11-22 13:38:40 +08:00
parent f11c3ab483
commit 8958656222
3 changed files with 19 additions and 35 deletions

View File

@@ -198,6 +198,10 @@ def gen_and_save_md_table(results):
# function to run the all the models # function to run the all the models
def run(): def run():
"""
Please be aware that this function can only work under Linux. MacOS and Windows will be supported in the future.
Any PR to enhance this method is highly welcomed.
"""
# get all folders # get all folders
folders = get_all_folders() folders = get_all_folders()
# set up # set up

View File

@@ -24,31 +24,6 @@ class RecordTemp:
This is the Records Template class that enables user to generate experiment results such as IC and This is the Records Template class that enables user to generate experiment results such as IC and
backtest in a certain format. backtest in a certain format.
""" """
artifact_path = None
@classmethod
def get_path(cls, path=None):
names = []
if cls.artifact_path is not None:
names.append(cls.artifact_path)
if path is not None:
names.append(path)
return "/".join(names)
artifact_path = None
@classmethod
def get_path(cls, path=None):
names = []
if cls.artifact_path is not None:
names.append(cls.artifact_path)
if path is not None:
names.append(path)
return "/".join(names)
artifact_path = None artifact_path = None

View File

@@ -8,7 +8,6 @@ from pathlib import Path
import numpy as np import numpy as np
import pandas as pd import pandas as pd
from scipy.stats import pearsonr
import qlib import qlib
from qlib.config import REG_CN, C from qlib.config import REG_CN, C
@@ -22,7 +21,7 @@ from qlib.contrib.evaluate import (
) )
from qlib.utils import exists_qlib_data, init_instance_by_config, flatten_dict from qlib.utils import exists_qlib_data, init_instance_by_config, flatten_dict
from qlib.workflow import R from qlib.workflow import R
from qlib.workflow.record_temp import SignalRecord, PortAnaRecord from qlib.workflow.record_temp import SignalRecord, SigAnaRecord, PortAnaRecord
market = "csi300" market = "csi300"
@@ -123,11 +122,13 @@ def train():
sr.generate() sr.generate()
pred_score = sr.load() pred_score = sr.load()
y_test = dataset.prepare("test", col_set="label") # calculate ic and ric
pred_score, y_test, __ = drop_nan_by_y_index(pred_score, y_test) sar = SigAnaRecord(recorder)
model_pearsonr = pearsonr(np.ravel(pred_score.values), np.ravel(y_test.values))[0] sar.generate()
ic = sar.load(sar.get_path("ic.pkl"))
ric = sar.load(sar.get_path("ric.pkl"))
return pred_score, {"model_pearsonr": model_pearsonr}, rid return pred_score, {"ic": ic, "ric": ric}, rid
def backtest_analysis(pred, rid): def backtest_analysis(pred, rid):
@@ -135,12 +136,15 @@ def backtest_analysis(pred, rid):
Parameters Parameters
---------- ----------
pred: pandas.DataFrame pred : pandas.DataFrame
predict scores predict scores
rid : str
the id of the recorder to be used in this function
Returns Returns
------- -------
analysis result : pandas.DataFrame analysis : pandas.DataFrame
the analysis result
""" """
recorder = R.get_recorder(experiment_name="workflow", recorder_id=rid) recorder = R.get_recorder(experiment_name="workflow", recorder_id=rid)
@@ -177,8 +181,9 @@ class TestAllFlow(unittest.TestCase):
shutil.rmtree(str(Path(C["exp_manager"]["kwargs"]["uri"].strip("file:")).resolve())) shutil.rmtree(str(Path(C["exp_manager"]["kwargs"]["uri"].strip("file:")).resolve()))
def test_0_train(self): def test_0_train(self):
TestAllFlow.PRED_SCORE, model_pearsonr, TestAllFlow.RID = train() TestAllFlow.PRED_SCORE, ic_ric, TestAllFlow.RID = train()
self.assertGreaterEqual(model_pearsonr["model_pearsonr"], 0, "train failed") self.assertGreaterEqual(ic_ric["ic"].all(), 0, "train failed")
self.assertGreaterEqual(ic_ric["ric"].all(), 0, "train failed")
def test_1_backtest(self): def test_1_backtest(self):
analyze_df = backtest_analysis(TestAllFlow.PRED_SCORE, TestAllFlow.RID) analyze_df = backtest_analysis(TestAllFlow.PRED_SCORE, TestAllFlow.RID)