mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-15 00:36:55 +08:00
Merge nested main (#597)
* MVP for Indian Stocks in qlib using yahooquery * cleaned with black * cleaned with black * add YahooNormalizeIN and YahooNormalizeIN1d * cleaned the code * added 1min for IN and also updated readme * update comments * fix comments * recorder support upload both raw file and directory * fix comments * Update README.md * Fix docs of QlibRecorder * sort index after loader (#538) make sure the fetch method is based on a index-sorted pd.DataFrame * refactor online serving rolling api * refactor TRA * format by black * fix horizon * fix TRA when use single head * clean up * improve pretrain * update README * fix tra when logdir is None * fix tra when logdir is None * Update strategy.py * Update README.md * Update README.md * Conda Suggestion * code standard docs * Update ensemble.py (#560) * Fix CI Bug (#575) Co-authored-by: yuxwang <anduinnn@foxmail.com> * Update gen.py (#576) * Fix multi-process loop calls (#574) * check lexsort in the 'lazy_sort_index' function (#566) * check lexsort * check lexsort * lexsort comment * lexsort comment * Delete .DS_Store * Update README.md * bug fix & use oracle transport pretrain * mend * Add `backend_freq_config` parameter, support multi-freq uri * Add sample_config to QlibDataLoader, support multi-freq * add multi-freq example * get_cls_kwargs renamed get_callable_kwargs * support multi-freq uri * Add inst_processors to D.features * Fix typo * Fix the index type of the multi-freq example * Fix duplicate mlflow directories in tests * Add DataPathManager to QlibConfig && modify inst_processors to supports list only * Modify the default value in the multi_freq example * Modify client-server mode and dataset-cache to disable inst_processor * Add wheel package to github CI * fix comment * Update FAQ.rst * Update README.md Fix wrong link * Update the docs of TaskManager (#586) * Update manage.py * update yaml * update run_all_model * Modify the Feature to be case sensitive (#589) * update README * remove verbose * fix spell bug * fix typos (#592) * Update Release Note * fix portfolio bug * Add calendar support for resample * add freq kwargs * test.yml: Remove redundant code (#595) * Supporting shared processor (#596) * Supporting shared processor * fix readonly reverse bug * remove pytests dependency * with fit bug * fix parameter error * fix comments * Fix undefined names in Python code (#599) * Update pytorch_tabnet.py $ `flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics` ``` ./qlib/qlib/contrib/model/pytorch_tabnet.py:567:38: F821 undefined name 'inp' self.independ.append(GLU(inp, out_dim, vbs=vbs)) ^ ./qlib/examples/model_rolling/task_manager_rolling.py:75:18: F821 undefined name 'task_train' run_task(task_train, self.task_pool, experiment_name=self.experiment_name) ^ 2 F821 undefined name 'task_train' 2 ``` * Fix undefined names in Python code * from qlib.model.trainer import task_train * update seed * fix some docstring * add comments * Fix SimpleDatasetCache * Update setup.py updated classifiers * Update setup.py change to matplotlib==3.3 * Update python-publish.yml added python 3.9 * updategrade version number * Update model list * fix the type of filter_pipe * fix comment * fix record_temp * update cvxpy version * Update code_standard.rst (#587) * Update code_standard.rst * Update docs/developer/code_standard.rst Co-authored-by: you-n-g <you-n-g@users.noreply.github.com> Co-authored-by: you-n-g <you-n-g@users.noreply.github.com> * Add file lock for MLflowExpManager (#619) * fix torch version * Share version number (#620) * Update initialization.rst (#622) * Update initialization.rst * Update docs/start/initialization.rst Co-authored-by: you-n-g <you-n-g@users.noreply.github.com> * Update docs/start/initialization.rst Co-authored-by: you-n-g <you-n-g@users.noreply.github.com> Co-authored-by: you-n-g <you-n-g@users.noreply.github.com> * fix bugs for running previous exmaple * fix deal amount bug * update change doc (#623) * Add files via upload * Update README.md * Update README.md * Update README.md * Delete change doc.gif * Add files via upload * Update README.md * Delete change doc.gif * Add files via upload * Delete change doc.gif * Add files via upload * Update README.md Co-authored-by: you-n-g <you-n-g@users.noreply.github.com> Co-authored-by: you-n-g <you-n-g@users.noreply.github.com> * update doc * simplify run all model * fix run all model bug * Fix Models (#483) * fix gat dataset * fix tft model * Update tft.py * Fix tft.py Co-authored-by: Pengrong Zhu <zhu.pengrong@foxmail.com> * type and skip empty exp * fix model yaml config * fix tft import bug * skip empty result * fix model and yaml bug * fix wrong generate parameter * Modify multi-freq example (#626) * modify the example of multi-freq * add Copyright * add a comment to average_ops.py * modify the example of multi-freq * add comment to multi_freq_handler.py * add the Ref expression description to multi_freq_handler.py * add expression description to multi_freq_handler.py * update images * fix workflow and update framework Co-authored-by: Gaurav <2796gaurav@gmail.com> Co-authored-by: 2796gaurav <17353992+2796gaurav@users.noreply.github.com> Co-authored-by: bxdd <bxd98@126.com> Co-authored-by: Young <afe.young@gmail.com> Co-authored-by: you-n-g <you-n-g@users.noreply.github.com> Co-authored-by: Dong Zhou <Zhou.Dong@microsoft.com> Co-authored-by: ZhangTP1996 <ztp18@mails.tsinghua.edu.cn> Co-authored-by: demon143 <59681577+demon143@users.noreply.github.com> Co-authored-by: Wangwuyi123 <51237097+Wangwuyi123@users.noreply.github.com> Co-authored-by: yuxwang <anduinnn@foxmail.com> Co-authored-by: Pengrong Zhu <zhu.pengrong@foxmail.com> Co-authored-by: Mark Zhao <50850474+markzhao98@users.noreply.github.com> Co-authored-by: cslwqxx <cslwqxx@users.noreply.github.com> Co-authored-by: Dong Zhou <evanzd@users.noreply.github.com> Co-authored-by: SaintMalik <37118134+saintmalik@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: Anurag Kumar <mailanu98@gmail.com> Co-authored-by: demon143 <785696300@qq.com>
This commit is contained in:
committed by
GitHub
parent
163e3c6266
commit
3760a18a8d
346
qlib/contrib/data/dataset.py
Normal file
346
qlib/contrib/data/dataset.py
Normal file
@@ -0,0 +1,346 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# Licensed under the MIT License.
|
||||
|
||||
import copy
|
||||
import torch
|
||||
import warnings
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
from qlib.utils import init_instance_by_config
|
||||
from qlib.data.dataset import DatasetH, DataHandler
|
||||
|
||||
|
||||
device = "cuda" if torch.cuda.is_available() else "cpu"
|
||||
|
||||
|
||||
def _to_tensor(x):
|
||||
if not isinstance(x, torch.Tensor):
|
||||
return torch.tensor(x, dtype=torch.float, device=device)
|
||||
return x
|
||||
|
||||
|
||||
def _create_ts_slices(index, seq_len):
|
||||
"""
|
||||
create time series slices from pandas index
|
||||
|
||||
Args:
|
||||
index (pd.MultiIndex): pandas multiindex with <instrument, datetime> order
|
||||
seq_len (int): sequence length
|
||||
"""
|
||||
assert isinstance(index, pd.MultiIndex), "unsupported index type"
|
||||
assert seq_len > 0, "sequence length should be larger than 0"
|
||||
assert index.is_monotonic_increasing, "index should be sorted"
|
||||
|
||||
# number of dates for each instrument
|
||||
sample_count_by_insts = index.to_series().groupby(level=0).size().values
|
||||
|
||||
# start index for each instrument
|
||||
start_index_of_insts = np.roll(np.cumsum(sample_count_by_insts), 1)
|
||||
start_index_of_insts[0] = 0
|
||||
|
||||
# all the [start, stop) indices of features
|
||||
# features between [start, stop) will be used to predict label at `stop - 1`
|
||||
slices = []
|
||||
for cur_loc, cur_cnt in zip(start_index_of_insts, sample_count_by_insts):
|
||||
for stop in range(1, cur_cnt + 1):
|
||||
end = cur_loc + stop
|
||||
start = max(end - seq_len, 0)
|
||||
slices.append(slice(start, end))
|
||||
slices = np.array(slices, dtype="object")
|
||||
|
||||
assert len(slices) == len(index) # the i-th slice = index[i]
|
||||
|
||||
return slices
|
||||
|
||||
|
||||
def _get_date_parse_fn(target):
|
||||
"""get date parse function
|
||||
|
||||
This method is used to parse date arguments as target type.
|
||||
|
||||
Example:
|
||||
get_date_parse_fn('20120101')('2017-01-01') => '20170101'
|
||||
get_date_parse_fn(20120101)('2017-01-01') => 20170101
|
||||
"""
|
||||
if isinstance(target, pd.Timestamp):
|
||||
_fn = lambda x: pd.Timestamp(x) # Timestamp('2020-01-01')
|
||||
elif isinstance(target, int):
|
||||
_fn = lambda x: int(str(x).replace("-", "")[:8]) # 20200201
|
||||
elif isinstance(target, str) and len(target) == 8:
|
||||
_fn = lambda x: str(x).replace("-", "")[:8] # '20200201'
|
||||
else:
|
||||
_fn = lambda x: x # '2021-01-01'
|
||||
return _fn
|
||||
|
||||
|
||||
def _maybe_padding(x, seq_len, zeros=None):
|
||||
"""padding 2d <time * feature> data with zeros
|
||||
|
||||
Args:
|
||||
x (np.ndarray): 2d data with shape <time * feature>
|
||||
seq_len (int): target sequence length
|
||||
zeros (np.ndarray): zeros with shape <seq_len * feature>
|
||||
"""
|
||||
assert seq_len > 0, "sequence length should be larger than 0"
|
||||
if zeros is None:
|
||||
zeros = np.zeros((seq_len, x.shape[1]), dtype=np.float32)
|
||||
else:
|
||||
assert len(zeros) >= seq_len, "zeros matrix is not large enough for padding"
|
||||
if len(x) != seq_len: # padding zeros
|
||||
x = np.concatenate([zeros[: seq_len - len(x), : x.shape[1]], x], axis=0)
|
||||
return x
|
||||
|
||||
|
||||
class MTSDatasetH(DatasetH):
|
||||
"""Memory Augmented Time Series Dataset
|
||||
|
||||
Args:
|
||||
handler (DataHandler): data handler
|
||||
segments (dict): data split segments
|
||||
seq_len (int): time series sequence length
|
||||
horizon (int): label horizon
|
||||
num_states (int): how many memory states to be added
|
||||
memory_mode (str): memory mode (daily or sample)
|
||||
batch_size (int): batch size (<0 will use daily sampling)
|
||||
n_samples (int): number of samples in the same day
|
||||
shuffle (bool): whether shuffle data
|
||||
drop_last (bool): whether drop last batch < batch_size
|
||||
input_size (int): reshape flatten rows as this input_size (backward compatibility)
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
handler,
|
||||
segments,
|
||||
seq_len=60,
|
||||
horizon=0,
|
||||
num_states=0,
|
||||
memory_mode="sample",
|
||||
batch_size=-1,
|
||||
n_samples=None,
|
||||
shuffle=True,
|
||||
drop_last=False,
|
||||
input_size=None,
|
||||
**kwargs,
|
||||
):
|
||||
|
||||
assert num_states == 0 or horizon > 0, "please specify `horizon` to avoid data leakage"
|
||||
assert memory_mode in ["sample", "daily"], "unsupported memory mode"
|
||||
assert memory_mode == "sample" or batch_size < 0, "daily memory requires daily sampling (`batch_size < 0`)"
|
||||
assert batch_size != 0, "invalid batch size"
|
||||
|
||||
if batch_size > 0 and n_samples is not None:
|
||||
warnings.warn("`n_samples` can only be used for daily sampling (`batch_size < 0`)")
|
||||
|
||||
self.seq_len = seq_len
|
||||
self.horizon = horizon
|
||||
self.num_states = num_states
|
||||
self.memory_mode = memory_mode
|
||||
self.batch_size = batch_size
|
||||
self.n_samples = n_samples
|
||||
self.shuffle = shuffle
|
||||
self.drop_last = drop_last
|
||||
self.input_size = input_size
|
||||
self.params = (batch_size, n_samples, drop_last, shuffle) # for train/eval switch
|
||||
|
||||
super().__init__(handler, segments, **kwargs)
|
||||
|
||||
def setup_data(self, handler_kwargs: dict = None, **kwargs):
|
||||
|
||||
super().setup_data(**kwargs)
|
||||
|
||||
if handler_kwargs is not None:
|
||||
self.handler.setup_data(**handler_kwargs)
|
||||
|
||||
# pre-fetch data and change index to <code, date>
|
||||
# NOTE: we will use inplace sort to reduce memory use
|
||||
try:
|
||||
df = self.handler._learn.copy() # use copy otherwise recorder will fail
|
||||
# FIXME: currently we cannot support switching from `_learn` to `_infer` for inference
|
||||
except:
|
||||
warnings.warn("cannot access `_learn`, will load raw data")
|
||||
df = self.handler._data.copy()
|
||||
df.index = df.index.swaplevel()
|
||||
df.sort_index(inplace=True)
|
||||
|
||||
# convert to numpy
|
||||
self._data = df["feature"].values.astype("float32")
|
||||
np.nan_to_num(self._data, copy=False) # NOTE: fillna in case users forget using the fillna processor
|
||||
self._label = df["label"].squeeze().values.astype("float32")
|
||||
self._index = df.index
|
||||
|
||||
if self.input_size is not None and self.input_size != self._data.shape[1]:
|
||||
warnings.warn("the data has different shape from input_size and the data will be reshaped")
|
||||
assert self._data.shape[1] % self.input_size == 0, "data mismatch, please check `input_size`"
|
||||
|
||||
# create batch slices
|
||||
self._batch_slices = _create_ts_slices(self._index, self.seq_len)
|
||||
|
||||
# create daily slices
|
||||
daily_slices = {date: [] for date in sorted(self._index.unique(level=1))} # sorted by date
|
||||
for i, (code, date) in enumerate(self._index):
|
||||
daily_slices[date].append(self._batch_slices[i])
|
||||
self._daily_slices = np.array(list(daily_slices.values()), dtype="object")
|
||||
self._daily_index = pd.Series(list(daily_slices.keys())) # index is the original date index
|
||||
|
||||
# add memory (sample wise and daily)
|
||||
if self.memory_mode == "sample":
|
||||
self._memory = np.zeros((len(self._data), self.num_states), dtype=np.float32)
|
||||
elif self.memory_mode == "daily":
|
||||
self._memory = np.zeros((len(self._daily_index), self.num_states), dtype=np.float32)
|
||||
else:
|
||||
raise ValueError(f"invalid memory_mode `{self.memory_mode}`")
|
||||
|
||||
# padding tensor
|
||||
self._zeros = np.zeros((self.seq_len, max(self.num_states, self._data.shape[1])), dtype=np.float32)
|
||||
|
||||
def _prepare_seg(self, slc, **kwargs):
|
||||
fn = _get_date_parse_fn(self._index[0][1])
|
||||
start_date = fn(slc.start)
|
||||
end_date = fn(slc.stop)
|
||||
obj = copy.copy(self) # shallow copy
|
||||
# NOTE: Seriable will disable copy `self._data` so we manually assign them here
|
||||
obj._data = self._data # reference (no copy)
|
||||
obj._label = self._label
|
||||
obj._index = self._index
|
||||
obj._memory = self._memory
|
||||
obj._zeros = self._zeros
|
||||
# update index for this batch
|
||||
date_index = self._index.get_level_values(1)
|
||||
obj._batch_slices = self._batch_slices[(date_index >= start_date) & (date_index <= end_date)]
|
||||
mask = (self._daily_index.values >= start_date) & (self._daily_index.values <= end_date)
|
||||
obj._daily_slices = self._daily_slices[mask]
|
||||
obj._daily_index = self._daily_index[mask]
|
||||
return obj
|
||||
|
||||
def restore_index(self, index):
|
||||
return self._index[index]
|
||||
|
||||
def restore_daily_index(self, daily_index):
|
||||
return pd.Index(self._daily_index.loc[daily_index])
|
||||
|
||||
def assign_data(self, index, vals):
|
||||
if self.num_states == 0:
|
||||
raise ValueError("cannot assign data as `num_states==0`")
|
||||
if isinstance(vals, torch.Tensor):
|
||||
vals = vals.detach().cpu().numpy()
|
||||
self._memory[index] = vals
|
||||
|
||||
def clear_memory(self):
|
||||
if self.num_states == 0:
|
||||
raise ValueError("cannot clear memory as `num_states==0`")
|
||||
self._memory[:] = 0
|
||||
|
||||
def train(self):
|
||||
"""enable traning mode"""
|
||||
self.batch_size, self.n_samples, self.drop_last, self.shuffle = self.params
|
||||
|
||||
def eval(self):
|
||||
"""enable evaluation mode"""
|
||||
self.batch_size = -1
|
||||
self.n_samples = None
|
||||
self.drop_last = False
|
||||
self.shuffle = False
|
||||
|
||||
def _get_slices(self):
|
||||
if self.batch_size < 0: # daily sampling
|
||||
slices = self._daily_slices.copy()
|
||||
batch_size = -1 * self.batch_size
|
||||
else: # normal sampling
|
||||
slices = self._batch_slices.copy()
|
||||
batch_size = self.batch_size
|
||||
return slices, batch_size
|
||||
|
||||
def __len__(self):
|
||||
slices, batch_size = self._get_slices()
|
||||
if self.drop_last:
|
||||
return len(slices) // batch_size
|
||||
return (len(slices) + batch_size - 1) // batch_size
|
||||
|
||||
def __iter__(self):
|
||||
slices, batch_size = self._get_slices()
|
||||
indices = np.arange(len(slices))
|
||||
if self.shuffle:
|
||||
np.random.shuffle(indices)
|
||||
|
||||
for i in range(len(indices))[::batch_size]:
|
||||
if self.drop_last and i + batch_size > len(indices):
|
||||
break
|
||||
|
||||
data = [] # store features
|
||||
label = [] # store labels
|
||||
index = [] # store index
|
||||
state = [] # store memory states
|
||||
daily_index = [] # store daily index
|
||||
daily_count = [] # store number of samples for each day
|
||||
|
||||
for j in indices[i : i + batch_size]:
|
||||
|
||||
# normal sampling: self.batch_size > 0 => slices is a list => slices_subset is a slice
|
||||
# daily sampling: self.batch_size < 0 => slices is a nested list => slices_subset is a list
|
||||
slices_subset = slices[j]
|
||||
|
||||
# daily sampling
|
||||
# each slices_subset contains a list of slices for multiple stocks
|
||||
# NOTE: daily sampling is used in 1) eval mode, 2) train mode with self.batch_size < 0
|
||||
if self.batch_size < 0:
|
||||
|
||||
# store daily index
|
||||
idx = self._daily_index.index[j] # daily_index.index is the index of the original data
|
||||
daily_index.append(idx)
|
||||
|
||||
# store daily memory if specified
|
||||
# NOTE: daily memory always requires daily sampling (self.batch_size < 0)
|
||||
if self.memory_mode == "daily":
|
||||
slc = slice(max(idx - self.seq_len - self.horizon, 0), max(idx - self.horizon, 0))
|
||||
state.append(_maybe_padding(self._memory[slc], self.seq_len, self._zeros))
|
||||
|
||||
# down-sample stocks and store count
|
||||
if self.n_samples and 0 < self.n_samples < len(slices_subset): # intraday subsample
|
||||
slices_subset = np.random.choice(slices_subset, self.n_samples, replace=False)
|
||||
daily_count.append(len(slices_subset))
|
||||
|
||||
# normal sampling
|
||||
# each slices_subset is a single slice
|
||||
# NOTE: normal sampling is used in train mode with self.batch_size > 0
|
||||
else:
|
||||
slices_subset = [slices_subset]
|
||||
|
||||
for slc in slices_subset:
|
||||
|
||||
# legacy support for Alpha360 data by `input_size`
|
||||
if self.input_size:
|
||||
data.append(self._data[slc.stop - 1].reshape(self.input_size, -1).T)
|
||||
else:
|
||||
data.append(_maybe_padding(self._data[slc], self.seq_len, self._zeros))
|
||||
|
||||
if self.memory_mode == "sample":
|
||||
state.append(_maybe_padding(self._memory[slc], self.seq_len, self._zeros)[: -self.horizon])
|
||||
|
||||
label.append(self._label[slc.stop - 1])
|
||||
index.append(slc.stop - 1)
|
||||
|
||||
# end slices loop
|
||||
|
||||
# end indices batch loop
|
||||
|
||||
# concate
|
||||
data = _to_tensor(np.stack(data))
|
||||
state = _to_tensor(np.stack(state))
|
||||
label = _to_tensor(np.stack(label))
|
||||
index = np.array(index)
|
||||
daily_index = np.array(daily_index)
|
||||
daily_count = np.array(daily_count)
|
||||
|
||||
# yield -> generator
|
||||
yield {
|
||||
"data": data,
|
||||
"label": label,
|
||||
"state": state,
|
||||
"index": index,
|
||||
"daily_index": daily_index,
|
||||
"daily_count": daily_count,
|
||||
}
|
||||
|
||||
# end indice loop
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
from ...data.dataset.handler import DataHandlerLP
|
||||
from ...data.dataset.processor import Processor
|
||||
from ...utils import get_cls_kwargs
|
||||
from ...utils import get_callable_kwargs
|
||||
from ...data.dataset import processor as processor_module
|
||||
from ...log import TimeInspector
|
||||
from inspect import getfullargspec
|
||||
@@ -14,7 +14,7 @@ def check_transform_proc(proc_l, fit_start_time, fit_end_time):
|
||||
new_l = []
|
||||
for p in proc_l:
|
||||
if not isinstance(p, Processor):
|
||||
klass, pkwargs = get_cls_kwargs(p, processor_module)
|
||||
klass, pkwargs = get_callable_kwargs(p, processor_module)
|
||||
args = getfullargspec(klass).args
|
||||
if "fit_start_time" in args and "fit_end_time" in args:
|
||||
assert (
|
||||
@@ -58,6 +58,7 @@ class Alpha360(DataHandlerLP):
|
||||
fit_start_time=None,
|
||||
fit_end_time=None,
|
||||
filter_pipe=None,
|
||||
inst_processor=None,
|
||||
**kwargs,
|
||||
):
|
||||
infer_processors = check_transform_proc(infer_processors, fit_start_time, fit_end_time)
|
||||
@@ -72,6 +73,7 @@ class Alpha360(DataHandlerLP):
|
||||
},
|
||||
"filter_pipe": filter_pipe,
|
||||
"freq": freq,
|
||||
"inst_processor": inst_processor,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -144,6 +146,7 @@ class Alpha158(DataHandlerLP):
|
||||
fit_end_time=None,
|
||||
process_type=DataHandlerLP.PTYPE_A,
|
||||
filter_pipe=None,
|
||||
inst_processor=None,
|
||||
**kwargs,
|
||||
):
|
||||
infer_processors = check_transform_proc(infer_processors, fit_start_time, fit_end_time)
|
||||
@@ -158,6 +161,7 @@ class Alpha158(DataHandlerLP):
|
||||
},
|
||||
"filter_pipe": filter_pipe,
|
||||
"freq": freq,
|
||||
"inst_processor": inst_processor,
|
||||
},
|
||||
}
|
||||
super().__init__(
|
||||
|
||||
@@ -36,9 +36,10 @@ def risk_analysis(r, N: int = None, freq: str = "day"):
|
||||
|
||||
def cal_risk_analysis_scaler(freq):
|
||||
_count, _freq = Freq.parse(freq)
|
||||
# len(D.calendar(start_time='2010-01-01', end_time='2019-12-31', freq='day')) = 2384
|
||||
_freq_scaler = {
|
||||
Freq.NORM_FREQ_MINUTE: 240 * 252,
|
||||
Freq.NORM_FREQ_DAY: 252,
|
||||
Freq.NORM_FREQ_MINUTE: 240 * 238,
|
||||
Freq.NORM_FREQ_DAY: 238,
|
||||
Freq.NORM_FREQ_WEEK: 50,
|
||||
Freq.NORM_FREQ_MONTH: 12,
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ from ...contrib.model.pytorch_gru import GRUModel
|
||||
|
||||
class DailyBatchSampler(Sampler):
|
||||
def __init__(self, data_source):
|
||||
|
||||
self.data_source = data_source
|
||||
# calculate number of samples in each batch
|
||||
self.daily_count = pd.Series(index=self.data_source.get_index()).groupby("datetime").size().values
|
||||
|
||||
@@ -564,7 +564,7 @@ class FeatureTransformer(nn.Module):
|
||||
self.shared = None
|
||||
self.independ = nn.ModuleList()
|
||||
if first:
|
||||
self.independ.append(GLU(inp, out_dim, vbs=vbs))
|
||||
self.independ.append(GLU(inp_dim, out_dim, vbs=vbs))
|
||||
for x in range(first, n_ind):
|
||||
self.independ.append(GLU(out_dim, out_dim, vbs=vbs))
|
||||
self.scale = float(np.sqrt(0.5))
|
||||
|
||||
944
qlib/contrib/model/pytorch_tra.py
Normal file
944
qlib/contrib/model/pytorch_tra.py
Normal file
@@ -0,0 +1,944 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# Licensed under the MIT License.
|
||||
|
||||
import io
|
||||
import os
|
||||
import copy
|
||||
import math
|
||||
import json
|
||||
import collections
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import seaborn as sns
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import torch.optim as optim
|
||||
import torch.nn.functional as F
|
||||
|
||||
try:
|
||||
from torch.utils.tensorboard import SummaryWriter
|
||||
except:
|
||||
SummaryWriter = None
|
||||
|
||||
from tqdm import tqdm
|
||||
|
||||
from qlib.utils import get_or_create_path
|
||||
from qlib.log import get_module_logger
|
||||
from qlib.model.base import Model
|
||||
from qlib.contrib.data.dataset import MTSDatasetH
|
||||
|
||||
device = "cuda" if torch.cuda.is_available() else "cpu"
|
||||
|
||||
|
||||
class TRAModel(Model):
|
||||
"""
|
||||
TRA Model
|
||||
|
||||
Args:
|
||||
model_config (dict): model config (will be used by RNN or Transformer)
|
||||
tra_config (dict): TRA config (will be used by TRA)
|
||||
model_type (str): which backbone model to use (RNN/Transformer)
|
||||
lr (float): learning rate
|
||||
n_epochs (int): number of total epochs
|
||||
early_stop (int): early stop when performance not improved at this step
|
||||
update_freq (int): gradient update frequency
|
||||
max_steps_per_epoch (int): maximum number of steps in one epoch
|
||||
lamb (float): regularization parameter
|
||||
rho (float): exponential decay rate for `lamb`
|
||||
alpha (float): fusion parameter for calculating transport loss matrix
|
||||
seed (int): random seed
|
||||
logdir (str): local log directory
|
||||
eval_train (bool): whether evaluate train set between epochs
|
||||
eval_test (bool): whether evaluate test set between epochs
|
||||
pretrain (bool): whether pretrain the backbone model before training TRA.
|
||||
Note that only TRA will be optimized after pretraining
|
||||
init_state (str): model init state path
|
||||
freeze_model (bool): whether freeze backbone model parameters
|
||||
freeze_predictors (bool): whether freeze predictors parameters
|
||||
transport_method (str): transport method, can be none/router/oracle
|
||||
memory_mode (str): memory mode, the same argument for MTSDatasetH
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
model_config,
|
||||
tra_config,
|
||||
model_type="RNN",
|
||||
lr=1e-3,
|
||||
n_epochs=500,
|
||||
early_stop=50,
|
||||
update_freq=1,
|
||||
max_steps_per_epoch=None,
|
||||
lamb=0.0,
|
||||
rho=0.99,
|
||||
alpha=1.0,
|
||||
seed=0,
|
||||
logdir=None,
|
||||
eval_train=False,
|
||||
eval_test=False,
|
||||
pretrain=False,
|
||||
init_state=None,
|
||||
reset_router=False,
|
||||
freeze_model=False,
|
||||
freeze_predictors=False,
|
||||
transport_method="none",
|
||||
memory_mode="sample",
|
||||
):
|
||||
|
||||
self.logger = get_module_logger("TRA")
|
||||
|
||||
assert memory_mode in ["sample", "daily"], "invalid memory mode"
|
||||
assert transport_method in ["none", "router", "oracle"], f"invalid transport method {transport_method}"
|
||||
assert transport_method == "none" or tra_config["num_states"] > 1, "optimal transport requires `num_states` > 1"
|
||||
assert (
|
||||
memory_mode != "daily" or tra_config["src_info"] == "TPE"
|
||||
), "daily transport can only support TPE as `src_info`"
|
||||
|
||||
if transport_method == "router" and not eval_train:
|
||||
self.logger.warning("`eval_train` will be ignored when using TRA.router")
|
||||
|
||||
np.random.seed(seed)
|
||||
torch.manual_seed(seed)
|
||||
|
||||
self.model_config = model_config
|
||||
self.tra_config = tra_config
|
||||
self.model_type = model_type
|
||||
self.lr = lr
|
||||
self.n_epochs = n_epochs
|
||||
self.early_stop = early_stop
|
||||
self.update_freq = update_freq
|
||||
self.max_steps_per_epoch = max_steps_per_epoch
|
||||
self.lamb = lamb
|
||||
self.rho = rho
|
||||
self.alpha = alpha
|
||||
self.seed = seed
|
||||
self.logdir = logdir
|
||||
self.eval_train = eval_train
|
||||
self.eval_test = eval_test
|
||||
self.pretrain = pretrain
|
||||
self.init_state = init_state
|
||||
self.reset_router = reset_router
|
||||
self.freeze_model = freeze_model
|
||||
self.freeze_predictors = freeze_predictors
|
||||
self.transport_method = transport_method
|
||||
self.use_daily_transport = memory_mode == "daily"
|
||||
self.transport_fn = transport_daily if self.use_daily_transport else transport_sample
|
||||
|
||||
self._writer = None
|
||||
if self.logdir is not None:
|
||||
if os.path.exists(self.logdir):
|
||||
self.logger.warning(f"logdir {self.logdir} is not empty")
|
||||
os.makedirs(self.logdir, exist_ok=True)
|
||||
if SummaryWriter is not None:
|
||||
self._writer = SummaryWriter(log_dir=self.logdir)
|
||||
|
||||
self._init_model()
|
||||
|
||||
def _init_model(self):
|
||||
|
||||
self.logger.info("init TRAModel...")
|
||||
|
||||
self.model = eval(self.model_type)(**self.model_config).to(device)
|
||||
print(self.model)
|
||||
|
||||
self.tra = TRA(self.model.output_size, **self.tra_config).to(device)
|
||||
print(self.tra)
|
||||
|
||||
if self.init_state:
|
||||
self.logger.warning(f"load state dict from `init_state`")
|
||||
state_dict = torch.load(self.init_state, map_location="cpu")
|
||||
self.model.load_state_dict(state_dict["model"])
|
||||
res = load_state_dict_unsafe(self.tra, state_dict["tra"])
|
||||
self.logger.warning(str(res))
|
||||
|
||||
if self.reset_router:
|
||||
self.logger.warning(f"reset TRA.router parameters")
|
||||
self.tra.fc.reset_parameters()
|
||||
self.tra.router.reset_parameters()
|
||||
|
||||
if self.freeze_model:
|
||||
self.logger.warning(f"freeze model parameters")
|
||||
for param in self.model.parameters():
|
||||
param.requires_grad_(False)
|
||||
|
||||
if self.freeze_predictors:
|
||||
self.logger.warning(f"freeze TRA.predictors parameters")
|
||||
for param in self.tra.predictors.parameters():
|
||||
param.requires_grad_(False)
|
||||
|
||||
self.logger.info("# model params: %d" % sum([p.numel() for p in self.model.parameters() if p.requires_grad]))
|
||||
self.logger.info("# tra params: %d" % sum([p.numel() for p in self.tra.parameters() if p.requires_grad]))
|
||||
|
||||
self.optimizer = optim.Adam(list(self.model.parameters()) + list(self.tra.parameters()), lr=self.lr)
|
||||
|
||||
self.fitted = False
|
||||
self.global_step = -1
|
||||
|
||||
def train_epoch(self, epoch, data_set, is_pretrain=False):
|
||||
|
||||
self.model.train()
|
||||
self.tra.train()
|
||||
data_set.train()
|
||||
self.optimizer.zero_grad()
|
||||
|
||||
P_all = []
|
||||
prob_all = []
|
||||
choice_all = []
|
||||
max_steps = len(data_set)
|
||||
if self.max_steps_per_epoch is not None:
|
||||
if epoch == 0 and self.max_steps_per_epoch < max_steps:
|
||||
self.logger.info(f"max steps updated from {max_steps} to {self.max_steps_per_epoch}")
|
||||
max_steps = min(self.max_steps_per_epoch, max_steps)
|
||||
|
||||
cur_step = 0
|
||||
total_loss = 0
|
||||
total_count = 0
|
||||
for batch in tqdm(data_set, total=max_steps):
|
||||
cur_step += 1
|
||||
if cur_step > max_steps:
|
||||
break
|
||||
|
||||
if not is_pretrain:
|
||||
self.global_step += 1
|
||||
|
||||
data, state, label, count = batch["data"], batch["state"], batch["label"], batch["daily_count"]
|
||||
index = batch["daily_index"] if self.use_daily_transport else batch["index"]
|
||||
|
||||
with torch.set_grad_enabled(not self.freeze_model):
|
||||
hidden = self.model(data)
|
||||
|
||||
all_preds, choice, prob = self.tra(hidden, state)
|
||||
|
||||
if is_pretrain or self.transport_method != "none":
|
||||
# NOTE: use oracle transport for pre-training
|
||||
loss, pred, L, P = self.transport_fn(
|
||||
all_preds,
|
||||
label,
|
||||
choice,
|
||||
prob,
|
||||
state.mean(dim=1),
|
||||
count,
|
||||
self.transport_method if not is_pretrain else "oracle",
|
||||
self.alpha,
|
||||
training=True,
|
||||
)
|
||||
data_set.assign_data(index, L) # save loss to memory
|
||||
if self.use_daily_transport: # only save for daily transport
|
||||
P_all.append(pd.DataFrame(P.detach().cpu().numpy(), index=index))
|
||||
prob_all.append(pd.DataFrame(prob.detach().cpu().numpy(), index=index))
|
||||
choice_all.append(pd.DataFrame(choice.detach().cpu().numpy(), index=index))
|
||||
decay = self.rho ** (self.global_step // 100) # decay every 100 steps
|
||||
lamb = 0 if is_pretrain else self.lamb * decay
|
||||
reg = prob.log().mul(P).sum(dim=1).mean() # train router to predict OT assignment
|
||||
if self._writer is not None and not is_pretrain:
|
||||
self._writer.add_scalar("training/router_loss", -reg.item(), self.global_step)
|
||||
self._writer.add_scalar("training/reg_loss", loss.item(), self.global_step)
|
||||
self._writer.add_scalar("training/lamb", lamb, self.global_step)
|
||||
if not self.use_daily_transport:
|
||||
P_mean = P.mean(axis=0).detach()
|
||||
self._writer.add_scalar("training/P", P_mean.max() / P_mean.min(), self.global_step)
|
||||
loss = loss - lamb * reg
|
||||
else:
|
||||
pred = all_preds.mean(dim=1)
|
||||
loss = loss_fn(pred, label)
|
||||
|
||||
(loss / self.update_freq).backward()
|
||||
if cur_step % self.update_freq == 0:
|
||||
self.optimizer.step()
|
||||
self.optimizer.zero_grad()
|
||||
|
||||
if self._writer is not None and not is_pretrain:
|
||||
self._writer.add_scalar("training/total_loss", loss.item(), self.global_step)
|
||||
|
||||
total_loss += loss.item()
|
||||
total_count += 1
|
||||
|
||||
if self.use_daily_transport and len(P_all):
|
||||
P_all = pd.concat(P_all, axis=0)
|
||||
prob_all = pd.concat(prob_all, axis=0)
|
||||
choice_all = pd.concat(choice_all, axis=0)
|
||||
P_all.index = data_set.restore_daily_index(P_all.index)
|
||||
prob_all.index = P_all.index
|
||||
choice_all.index = P_all.index
|
||||
if not is_pretrain:
|
||||
self._writer.add_image("P", plot(P_all), epoch, dataformats="HWC")
|
||||
self._writer.add_image("prob", plot(prob_all), epoch, dataformats="HWC")
|
||||
self._writer.add_image("choice", plot(choice_all), epoch, dataformats="HWC")
|
||||
|
||||
total_loss /= total_count
|
||||
|
||||
if self._writer is not None and not is_pretrain:
|
||||
self._writer.add_scalar("training/loss", total_loss, epoch)
|
||||
|
||||
return total_loss
|
||||
|
||||
def test_epoch(self, epoch, data_set, return_pred=False, prefix="test", is_pretrain=False):
|
||||
|
||||
self.model.eval()
|
||||
self.tra.eval()
|
||||
data_set.eval()
|
||||
|
||||
preds = []
|
||||
probs = []
|
||||
P_all = []
|
||||
metrics = []
|
||||
for batch in tqdm(data_set):
|
||||
data, state, label, count = batch["data"], batch["state"], batch["label"], batch["daily_count"]
|
||||
index = batch["daily_index"] if self.use_daily_transport else batch["index"]
|
||||
|
||||
with torch.no_grad():
|
||||
hidden = self.model(data)
|
||||
all_preds, choice, prob = self.tra(hidden, state)
|
||||
|
||||
if is_pretrain or self.transport_method != "none":
|
||||
loss, pred, L, P = self.transport_fn(
|
||||
all_preds,
|
||||
label,
|
||||
choice,
|
||||
prob,
|
||||
state.mean(dim=1),
|
||||
count,
|
||||
self.transport_method if not is_pretrain else "oracle",
|
||||
self.alpha,
|
||||
training=False,
|
||||
)
|
||||
data_set.assign_data(index, L) # save loss to memory
|
||||
if P is not None and return_pred:
|
||||
P_all.append(pd.DataFrame(P.cpu().numpy(), index=index))
|
||||
else:
|
||||
pred = all_preds.mean(dim=1)
|
||||
|
||||
X = np.c_[pred.cpu().numpy(), label.cpu().numpy(), all_preds.cpu().numpy()]
|
||||
columns = ["score", "label"] + ["score_%d" % d for d in range(all_preds.shape[1])]
|
||||
pred = pd.DataFrame(X, index=batch["index"], columns=columns)
|
||||
|
||||
metrics.append(evaluate(pred))
|
||||
|
||||
if return_pred:
|
||||
preds.append(pred)
|
||||
if prob is not None:
|
||||
columns = ["prob_%d" % d for d in range(all_preds.shape[1])]
|
||||
probs.append(pd.DataFrame(prob.cpu().numpy(), index=index, columns=columns))
|
||||
|
||||
metrics = pd.DataFrame(metrics)
|
||||
metrics = {
|
||||
"MSE": metrics.MSE.mean(),
|
||||
"MAE": metrics.MAE.mean(),
|
||||
"IC": metrics.IC.mean(),
|
||||
"ICIR": metrics.IC.mean() / metrics.IC.std(),
|
||||
}
|
||||
|
||||
if self._writer is not None and epoch >= 0 and not is_pretrain:
|
||||
for key, value in metrics.items():
|
||||
self._writer.add_scalar(prefix + "/" + key, value, epoch)
|
||||
|
||||
if return_pred:
|
||||
preds = pd.concat(preds, axis=0)
|
||||
preds.index = data_set.restore_index(preds.index)
|
||||
preds.index = preds.index.swaplevel()
|
||||
preds.sort_index(inplace=True)
|
||||
|
||||
if probs:
|
||||
probs = pd.concat(probs, axis=0)
|
||||
if self.use_daily_transport:
|
||||
probs.index = data_set.restore_daily_index(probs.index)
|
||||
else:
|
||||
probs.index = data_set.restore_index(probs.index)
|
||||
probs.index = probs.index.swaplevel()
|
||||
probs.sort_index(inplace=True)
|
||||
|
||||
if len(P_all):
|
||||
P_all = pd.concat(P_all, axis=0)
|
||||
if self.use_daily_transport:
|
||||
P_all.index = data_set.restore_daily_index(P_all.index)
|
||||
else:
|
||||
P_all.index = data_set.restore_index(P_all.index)
|
||||
P_all.index = P_all.index.swaplevel()
|
||||
P_all.sort_index(inplace=True)
|
||||
|
||||
return metrics, preds, probs, P_all
|
||||
|
||||
def _fit(self, train_set, valid_set, test_set, evals_result, is_pretrain=True):
|
||||
|
||||
best_score = -1
|
||||
best_epoch = 0
|
||||
stop_rounds = 0
|
||||
best_params = {
|
||||
"model": copy.deepcopy(self.model.state_dict()),
|
||||
"tra": copy.deepcopy(self.tra.state_dict()),
|
||||
}
|
||||
# train
|
||||
if not is_pretrain and self.transport_method != "none":
|
||||
self.logger.info("init memory...")
|
||||
self.test_epoch(-1, train_set)
|
||||
|
||||
for epoch in range(self.n_epochs):
|
||||
self.logger.info("Epoch %d:", epoch)
|
||||
|
||||
self.logger.info("training...")
|
||||
self.train_epoch(epoch, train_set, is_pretrain=is_pretrain)
|
||||
|
||||
self.logger.info("evaluating...")
|
||||
# NOTE: during evaluating, the whole memory will be refreshed
|
||||
if not is_pretrain and (self.transport_method == "router" or self.eval_train):
|
||||
train_set.clear_memory() # NOTE: clear the shared memory
|
||||
train_metrics = self.test_epoch(epoch, train_set, is_pretrain=is_pretrain, prefix="train")[0]
|
||||
evals_result["train"].append(train_metrics)
|
||||
self.logger.info("train metrics: %s" % train_metrics)
|
||||
|
||||
valid_metrics = self.test_epoch(epoch, valid_set, is_pretrain=is_pretrain, prefix="valid")[0]
|
||||
evals_result["valid"].append(valid_metrics)
|
||||
self.logger.info("valid metrics: %s" % valid_metrics)
|
||||
|
||||
if self.eval_test:
|
||||
test_metrics = self.test_epoch(epoch, test_set, is_pretrain=is_pretrain, prefix="test")[0]
|
||||
evals_result["test"].append(test_metrics)
|
||||
self.logger.info("test metrics: %s" % test_metrics)
|
||||
|
||||
if valid_metrics["IC"] > best_score:
|
||||
best_score = valid_metrics["IC"]
|
||||
stop_rounds = 0
|
||||
best_epoch = epoch
|
||||
best_params = {
|
||||
"model": copy.deepcopy(self.model.state_dict()),
|
||||
"tra": copy.deepcopy(self.tra.state_dict()),
|
||||
}
|
||||
if self.logdir is not None:
|
||||
torch.save(best_params, self.logdir + "/model.bin")
|
||||
else:
|
||||
stop_rounds += 1
|
||||
if stop_rounds >= self.early_stop:
|
||||
self.logger.info("early stop @ %s" % epoch)
|
||||
break
|
||||
|
||||
self.logger.info("best score: %.6lf @ %d" % (best_score, best_epoch))
|
||||
self.model.load_state_dict(best_params["model"])
|
||||
self.tra.load_state_dict(best_params["tra"])
|
||||
|
||||
return best_score
|
||||
|
||||
def fit(self, dataset, evals_result=dict()):
|
||||
|
||||
assert isinstance(dataset, MTSDatasetH), "TRAModel only supports `qlib.contrib.data.dataset.MTSDatasetH`"
|
||||
|
||||
train_set, valid_set, test_set = dataset.prepare(["train", "valid", "test"])
|
||||
|
||||
self.fitted = True
|
||||
self.global_step = -1
|
||||
|
||||
evals_result["train"] = []
|
||||
evals_result["valid"] = []
|
||||
evals_result["test"] = []
|
||||
|
||||
if self.pretrain:
|
||||
self.logger.info("pretraining...")
|
||||
self.optimizer = optim.Adam(
|
||||
list(self.model.parameters()) + list(self.tra.predictors.parameters()), lr=self.lr
|
||||
)
|
||||
self._fit(train_set, valid_set, test_set, evals_result, is_pretrain=True)
|
||||
|
||||
# reset optimizer
|
||||
self.optimizer = optim.Adam(list(self.model.parameters()) + list(self.tra.parameters()), lr=self.lr)
|
||||
|
||||
self.logger.info("training...")
|
||||
best_score = self._fit(train_set, valid_set, test_set, evals_result, is_pretrain=False)
|
||||
|
||||
self.logger.info("inference")
|
||||
train_metrics, train_preds, train_probs, train_P = self.test_epoch(-1, train_set, return_pred=True)
|
||||
self.logger.info("train metrics: %s" % train_metrics)
|
||||
|
||||
valid_metrics, valid_preds, valid_probs, valid_P = self.test_epoch(-1, valid_set, return_pred=True)
|
||||
self.logger.info("valid metrics: %s" % valid_metrics)
|
||||
|
||||
test_metrics, test_preds, test_probs, test_P = self.test_epoch(-1, test_set, return_pred=True)
|
||||
self.logger.info("test metrics: %s" % test_metrics)
|
||||
|
||||
if self.logdir:
|
||||
self.logger.info("save model & pred to local directory")
|
||||
|
||||
pd.concat({name: pd.DataFrame(evals_result[name]) for name in evals_result}, axis=1).to_csv(
|
||||
self.logdir + "/logs.csv", index=False
|
||||
)
|
||||
|
||||
torch.save({"model": self.model.state_dict(), "tra": self.tra.state_dict()}, self.logdir + "/model.bin")
|
||||
|
||||
train_preds.to_pickle(self.logdir + "/train_pred.pkl")
|
||||
valid_preds.to_pickle(self.logdir + "/valid_pred.pkl")
|
||||
test_preds.to_pickle(self.logdir + "/test_pred.pkl")
|
||||
|
||||
if len(train_probs):
|
||||
train_probs.to_pickle(self.logdir + "/train_prob.pkl")
|
||||
valid_probs.to_pickle(self.logdir + "/valid_prob.pkl")
|
||||
test_probs.to_pickle(self.logdir + "/test_prob.pkl")
|
||||
|
||||
if len(train_P):
|
||||
train_P.to_pickle(self.logdir + "/train_P.pkl")
|
||||
valid_P.to_pickle(self.logdir + "/valid_P.pkl")
|
||||
test_P.to_pickle(self.logdir + "/test_P.pkl")
|
||||
|
||||
info = {
|
||||
"config": {
|
||||
"model_config": self.model_config,
|
||||
"tra_config": self.tra_config,
|
||||
"model_type": self.model_type,
|
||||
"lr": self.lr,
|
||||
"n_epochs": self.n_epochs,
|
||||
"early_stop": self.early_stop,
|
||||
"max_steps_per_epoch": self.max_steps_per_epoch,
|
||||
"lamb": self.lamb,
|
||||
"rho": self.rho,
|
||||
"alpha": self.alpha,
|
||||
"seed": self.seed,
|
||||
"logdir": self.logdir,
|
||||
"pretrain": self.pretrain,
|
||||
"init_state": self.init_state,
|
||||
"transport_method": self.transport_method,
|
||||
"use_daily_transport": self.use_daily_transport,
|
||||
},
|
||||
"best_eval_metric": -best_score, # NOTE: -1 for minimize
|
||||
"metrics": {"train": train_metrics, "valid": valid_metrics, "test": test_metrics},
|
||||
}
|
||||
with open(self.logdir + "/info.json", "w") as f:
|
||||
json.dump(info, f)
|
||||
|
||||
def predict(self, dataset, segment="test"):
|
||||
|
||||
assert isinstance(dataset, MTSDatasetH), "TRAModel only supports `qlib.contrib.data.dataset.MTSDatasetH`"
|
||||
|
||||
if not self.fitted:
|
||||
raise ValueError("model is not fitted yet!")
|
||||
|
||||
test_set = dataset.prepare(segment)
|
||||
|
||||
metrics, preds, _, _ = self.test_epoch(-1, test_set, return_pred=True)
|
||||
self.logger.info("test metrics: %s" % metrics)
|
||||
|
||||
return preds
|
||||
|
||||
|
||||
class RNN(nn.Module):
|
||||
|
||||
"""RNN Model
|
||||
|
||||
Args:
|
||||
input_size (int): input size (# features)
|
||||
hidden_size (int): hidden size
|
||||
num_layers (int): number of hidden layers
|
||||
rnn_arch (str): rnn architecture
|
||||
use_attn (bool): whether use attention layer.
|
||||
we use concat attention as https://github.com/fulifeng/Adv-ALSTM/
|
||||
dropout (float): dropout rate
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
input_size=16,
|
||||
hidden_size=64,
|
||||
num_layers=2,
|
||||
rnn_arch="GRU",
|
||||
use_attn=True,
|
||||
dropout=0.0,
|
||||
**kwargs,
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
self.input_size = input_size
|
||||
self.hidden_size = hidden_size
|
||||
self.num_layers = num_layers
|
||||
self.rnn_arch = rnn_arch
|
||||
self.use_attn = use_attn
|
||||
|
||||
if hidden_size < input_size:
|
||||
# compression
|
||||
self.input_proj = nn.Linear(input_size, hidden_size)
|
||||
else:
|
||||
self.input_proj = None
|
||||
|
||||
self.rnn = getattr(nn, rnn_arch)(
|
||||
input_size=min(input_size, hidden_size),
|
||||
hidden_size=hidden_size,
|
||||
num_layers=num_layers,
|
||||
batch_first=True,
|
||||
dropout=dropout,
|
||||
)
|
||||
|
||||
if self.use_attn:
|
||||
self.W = nn.Linear(hidden_size, hidden_size)
|
||||
self.u = nn.Linear(hidden_size, 1, bias=False)
|
||||
self.output_size = hidden_size * 2
|
||||
else:
|
||||
self.output_size = hidden_size
|
||||
|
||||
def forward(self, x):
|
||||
|
||||
if self.input_proj is not None:
|
||||
x = self.input_proj(x)
|
||||
|
||||
rnn_out, last_out = self.rnn(x)
|
||||
if self.rnn_arch == "LSTM":
|
||||
last_out = last_out[0]
|
||||
last_out = last_out.mean(dim=0)
|
||||
|
||||
if self.use_attn:
|
||||
laten = self.W(rnn_out).tanh()
|
||||
scores = self.u(laten).softmax(dim=1)
|
||||
att_out = (rnn_out * scores).sum(dim=1)
|
||||
last_out = torch.cat([last_out, att_out], dim=1)
|
||||
|
||||
return last_out
|
||||
|
||||
|
||||
class PositionalEncoding(nn.Module):
|
||||
# reference: https://pytorch.org/tutorials/beginner/transformer_tutorial.html
|
||||
def __init__(self, d_model, dropout=0.1, max_len=5000):
|
||||
super(PositionalEncoding, self).__init__()
|
||||
self.dropout = nn.Dropout(p=dropout)
|
||||
|
||||
pe = torch.zeros(max_len, d_model)
|
||||
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
|
||||
div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))
|
||||
pe[:, 0::2] = torch.sin(position * div_term)
|
||||
pe[:, 1::2] = torch.cos(position * div_term)
|
||||
pe = pe.unsqueeze(0).transpose(0, 1)
|
||||
self.register_buffer("pe", pe)
|
||||
|
||||
def forward(self, x):
|
||||
x = x + self.pe[: x.size(0), :]
|
||||
return self.dropout(x)
|
||||
|
||||
|
||||
class Transformer(nn.Module):
|
||||
|
||||
"""Transformer Model
|
||||
|
||||
Args:
|
||||
input_size (int): input size (# features)
|
||||
hidden_size (int): hidden size
|
||||
num_layers (int): number of transformer layers
|
||||
num_heads (int): number of heads in transformer
|
||||
dropout (float): dropout rate
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
input_size=16,
|
||||
hidden_size=64,
|
||||
num_layers=2,
|
||||
num_heads=2,
|
||||
dropout=0.0,
|
||||
**kwargs,
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
self.input_size = input_size
|
||||
self.hidden_size = hidden_size
|
||||
self.num_layers = num_layers
|
||||
self.num_heads = num_heads
|
||||
|
||||
self.input_proj = nn.Linear(input_size, hidden_size)
|
||||
|
||||
self.pe = PositionalEncoding(input_size, dropout)
|
||||
layer = nn.TransformerEncoderLayer(
|
||||
nhead=num_heads, dropout=dropout, d_model=hidden_size, dim_feedforward=hidden_size * 4
|
||||
)
|
||||
self.encoder = nn.TransformerEncoder(layer, num_layers=num_layers)
|
||||
|
||||
self.output_size = hidden_size
|
||||
|
||||
def forward(self, x):
|
||||
|
||||
x = x.permute(1, 0, 2).contiguous() # the first dim need to be time
|
||||
x = self.pe(x)
|
||||
|
||||
x = self.input_proj(x)
|
||||
out = self.encoder(x)
|
||||
|
||||
return out[-1]
|
||||
|
||||
|
||||
class TRA(nn.Module):
|
||||
|
||||
"""Temporal Routing Adaptor (TRA)
|
||||
|
||||
TRA takes historical prediction erros & latent representation as inputs,
|
||||
then routes the input sample to a specific predictor for training & inference.
|
||||
|
||||
Args:
|
||||
input_size (int): input size (RNN/Transformer's hidden size)
|
||||
num_states (int): number of latent states (i.e., trading patterns)
|
||||
If `num_states=1`, then TRA falls back to traditional methods
|
||||
hidden_size (int): hidden size of the router
|
||||
tau (float): gumbel softmax temperature
|
||||
src_info (str): information for the router
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
input_size,
|
||||
num_states=1,
|
||||
hidden_size=8,
|
||||
rnn_arch="GRU",
|
||||
num_layers=1,
|
||||
dropout=0.0,
|
||||
tau=1.0,
|
||||
src_info="LR_TPE",
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
assert src_info in ["LR", "TPE", "LR_TPE"], "invalid `src_info`"
|
||||
|
||||
self.num_states = num_states
|
||||
self.tau = tau
|
||||
self.rnn_arch = rnn_arch
|
||||
self.src_info = src_info
|
||||
|
||||
self.predictors = nn.Linear(input_size, num_states)
|
||||
|
||||
if self.num_states > 1:
|
||||
if "TPE" in src_info:
|
||||
self.router = getattr(nn, rnn_arch)(
|
||||
input_size=num_states,
|
||||
hidden_size=hidden_size,
|
||||
num_layers=num_layers,
|
||||
batch_first=True,
|
||||
dropout=dropout,
|
||||
)
|
||||
self.fc = nn.Linear(hidden_size + input_size if "LR" in src_info else hidden_size, num_states)
|
||||
else:
|
||||
self.fc = nn.Linear(input_size, num_states)
|
||||
|
||||
def reset_parameters(self):
|
||||
for child in self.children():
|
||||
child.reset_parameters()
|
||||
|
||||
def forward(self, hidden, hist_loss):
|
||||
|
||||
preds = self.predictors(hidden)
|
||||
|
||||
if self.num_states == 1: # no need for router when having only one prediction
|
||||
return preds, None, None
|
||||
|
||||
if "TPE" in self.src_info:
|
||||
out = self.router(hist_loss)[1] # TPE
|
||||
if self.rnn_arch == "LSTM":
|
||||
out = out[0]
|
||||
out = out.mean(dim=0)
|
||||
if "LR" in self.src_info:
|
||||
out = torch.cat([hidden, out], dim=-1) # LR_TPE
|
||||
else:
|
||||
out = hidden # LR
|
||||
|
||||
out = self.fc(out)
|
||||
|
||||
choice = F.gumbel_softmax(out, dim=-1, tau=self.tau, hard=True)
|
||||
prob = torch.softmax(out / self.tau, dim=-1)
|
||||
|
||||
return preds, choice, prob
|
||||
|
||||
|
||||
def evaluate(pred):
|
||||
pred = pred.rank(pct=True) # transform into percentiles
|
||||
score = pred.score
|
||||
label = pred.label
|
||||
diff = score - label
|
||||
MSE = (diff ** 2).mean()
|
||||
MAE = (diff.abs()).mean()
|
||||
IC = score.corr(label, method="spearman")
|
||||
return {"MSE": MSE, "MAE": MAE, "IC": IC}
|
||||
|
||||
|
||||
def shoot_infs(inp_tensor):
|
||||
"""Replaces inf by maximum of tensor"""
|
||||
mask_inf = torch.isinf(inp_tensor)
|
||||
ind_inf = torch.nonzero(mask_inf, as_tuple=False)
|
||||
if len(ind_inf) > 0:
|
||||
for ind in ind_inf:
|
||||
if len(ind) == 2:
|
||||
inp_tensor[ind[0], ind[1]] = 0
|
||||
elif len(ind) == 1:
|
||||
inp_tensor[ind[0]] = 0
|
||||
m = torch.max(inp_tensor)
|
||||
for ind in ind_inf:
|
||||
if len(ind) == 2:
|
||||
inp_tensor[ind[0], ind[1]] = m
|
||||
elif len(ind) == 1:
|
||||
inp_tensor[ind[0]] = m
|
||||
return inp_tensor
|
||||
|
||||
|
||||
def sinkhorn(Q, n_iters=3, epsilon=0.1):
|
||||
# epsilon should be adjusted according to logits value's scale
|
||||
with torch.no_grad():
|
||||
Q = torch.exp(Q / epsilon)
|
||||
Q = shoot_infs(Q)
|
||||
for i in range(n_iters):
|
||||
Q /= Q.sum(dim=0, keepdim=True)
|
||||
Q /= Q.sum(dim=1, keepdim=True)
|
||||
return Q
|
||||
|
||||
|
||||
def loss_fn(pred, label):
|
||||
mask = ~torch.isnan(label)
|
||||
if len(pred.shape) == 2:
|
||||
label = label[:, None]
|
||||
return (pred[mask] - label[mask]).pow(2).mean(dim=0)
|
||||
|
||||
|
||||
def minmax_norm(x):
|
||||
xmin = x.min(dim=-1, keepdim=True).values
|
||||
xmax = x.max(dim=-1, keepdim=True).values
|
||||
mask = (xmin == xmax).squeeze()
|
||||
x = (x - xmin) / (xmax - xmin + 1e-12)
|
||||
x[mask] = 1
|
||||
return x
|
||||
|
||||
|
||||
def transport_sample(all_preds, label, choice, prob, hist_loss, count, transport_method, alpha, training=False):
|
||||
"""
|
||||
sample-wise transport
|
||||
|
||||
Args:
|
||||
all_preds (torch.Tensor): predictions from all predictors, [sample x states]
|
||||
label (torch.Tensor): label, [sample]
|
||||
choice (torch.Tensor): gumbel softmax choice, [sample x states]
|
||||
prob (torch.Tensor): router predicted probility, [sample x states]
|
||||
hist_loss (torch.Tensor): history loss matrix, [sample x states]
|
||||
count (list): sample counts for each day, empty list for sample-wise transport
|
||||
transport_method (str): transportation method
|
||||
alpha (float): fusion parameter for calculating transport loss matrix
|
||||
training (bool): indicate training or inference
|
||||
"""
|
||||
assert all_preds.shape == choice.shape
|
||||
assert len(all_preds) == len(label)
|
||||
assert transport_method in ["oracle", "router"]
|
||||
|
||||
all_loss = torch.zeros_like(all_preds)
|
||||
mask = ~torch.isnan(label)
|
||||
all_loss[mask] = (all_preds[mask] - label[mask, None]).pow(2) # [sample x states]
|
||||
|
||||
L = minmax_norm(all_loss.detach())
|
||||
Lh = L * alpha + minmax_norm(hist_loss) * (1 - alpha) # add hist loss for transport
|
||||
Lh = minmax_norm(Lh)
|
||||
P = sinkhorn(-Lh)
|
||||
del Lh
|
||||
|
||||
if transport_method == "router":
|
||||
if training:
|
||||
pred = (all_preds * choice).sum(dim=1) # gumbel softmax
|
||||
else:
|
||||
pred = all_preds[range(len(all_preds)), prob.argmax(dim=-1)] # argmax
|
||||
else:
|
||||
pred = (all_preds * P).sum(dim=1)
|
||||
|
||||
if transport_method == "router":
|
||||
loss = loss_fn(pred, label)
|
||||
else:
|
||||
loss = (all_loss * P).sum(dim=1).mean()
|
||||
|
||||
return loss, pred, L, P
|
||||
|
||||
|
||||
def transport_daily(all_preds, label, choice, prob, hist_loss, count, transport_method, alpha, training=False):
|
||||
"""
|
||||
daily transport
|
||||
|
||||
Args:
|
||||
all_preds (torch.Tensor): predictions from all predictors, [sample x states]
|
||||
label (torch.Tensor): label, [sample]
|
||||
choice (torch.Tensor): gumbel softmax choice, [days x states]
|
||||
prob (torch.Tensor): router predicted probility, [days x states]
|
||||
hist_loss (torch.Tensor): history loss matrix, [days x states]
|
||||
count (list): sample counts for each day, [days]
|
||||
transport_method (str): transportation method
|
||||
alpha (float): fusion parameter for calculating transport loss matrix
|
||||
training (bool): indicate training or inference
|
||||
"""
|
||||
assert len(prob) == len(count)
|
||||
assert len(all_preds) == sum(count)
|
||||
assert transport_method in ["oracle", "router"]
|
||||
|
||||
all_loss = [] # loss of all predictions
|
||||
start = 0
|
||||
for i, cnt in enumerate(count):
|
||||
slc = slice(start, start + cnt) # samples from the i-th day
|
||||
start += cnt
|
||||
tloss = loss_fn(all_preds[slc], label[slc]) # loss of the i-th day
|
||||
all_loss.append(tloss)
|
||||
all_loss = torch.stack(all_loss, dim=0) # [days x states]
|
||||
|
||||
L = minmax_norm(all_loss.detach())
|
||||
Lh = L * alpha + minmax_norm(hist_loss) * (1 - alpha) # add hist loss for transport
|
||||
Lh = minmax_norm(Lh)
|
||||
P = sinkhorn(-Lh)
|
||||
del Lh
|
||||
|
||||
pred = []
|
||||
start = 0
|
||||
for i, cnt in enumerate(count):
|
||||
slc = slice(start, start + cnt) # samples from the i-th day
|
||||
start += cnt
|
||||
if transport_method == "router":
|
||||
if training:
|
||||
tpred = all_preds[slc] @ choice[i] # gumbel softmax
|
||||
else:
|
||||
tpred = all_preds[slc][:, prob[i].argmax(dim=-1)] # argmax
|
||||
else:
|
||||
tpred = all_preds[slc] @ P[i]
|
||||
pred.append(tpred)
|
||||
pred = torch.cat(pred, dim=0) # [samples]
|
||||
|
||||
if transport_method == "router":
|
||||
loss = loss_fn(pred, label)
|
||||
else:
|
||||
loss = (all_loss * P).sum(dim=1).mean()
|
||||
|
||||
return loss, pred, L, P
|
||||
|
||||
|
||||
def load_state_dict_unsafe(model, state_dict):
|
||||
"""
|
||||
Load state dict to provided model while ignore exceptions.
|
||||
"""
|
||||
|
||||
missing_keys = []
|
||||
unexpected_keys = []
|
||||
error_msgs = []
|
||||
|
||||
# copy state_dict so _load_from_state_dict can modify it
|
||||
metadata = getattr(state_dict, "_metadata", None)
|
||||
state_dict = state_dict.copy()
|
||||
if metadata is not None:
|
||||
state_dict._metadata = metadata
|
||||
|
||||
def load(module, prefix=""):
|
||||
local_metadata = {} if metadata is None else metadata.get(prefix[:-1], {})
|
||||
module._load_from_state_dict(
|
||||
state_dict, prefix, local_metadata, True, missing_keys, unexpected_keys, error_msgs
|
||||
)
|
||||
for name, child in module._modules.items():
|
||||
if child is not None:
|
||||
load(child, prefix + name + ".")
|
||||
|
||||
load(model)
|
||||
load = None # break load->load reference cycle
|
||||
|
||||
return {"unexpected_keys": unexpected_keys, "missing_keys": missing_keys, "error_msgs": error_msgs}
|
||||
|
||||
|
||||
def plot(P):
|
||||
assert isinstance(P, pd.DataFrame)
|
||||
|
||||
fig, axes = plt.subplots(1, 2, figsize=(10, 4))
|
||||
P.plot.area(ax=axes[0], xlabel="")
|
||||
P.idxmax(axis=1).value_counts().sort_index().plot.bar(ax=axes[1], xlabel="")
|
||||
plt.tight_layout()
|
||||
|
||||
with io.BytesIO() as buf:
|
||||
plt.savefig(buf, format="png")
|
||||
buf.seek(0)
|
||||
img = plt.imread(buf)
|
||||
plt.close()
|
||||
|
||||
return np.uint8(img * 255)
|
||||
@@ -120,7 +120,7 @@ class Operator:
|
||||
# generate and save order list
|
||||
order_list = user.strategy.generate_trade_decision(
|
||||
score_series=score_series,
|
||||
current=user.account.current,
|
||||
current=user.account.current_position,
|
||||
trade_exchange=trade_exchange,
|
||||
trade_date=trade_date,
|
||||
)
|
||||
@@ -202,8 +202,8 @@ class Operator:
|
||||
score_series = load_score_series((pathlib.Path(path) / user_id), trade_date)
|
||||
update_account(user.account, trade_info, trade_exchange, trade_date)
|
||||
|
||||
report = user.account.report.generate_report_dataframe()
|
||||
self.logger.info(report)
|
||||
portfolio_metrics = user.account.portfolio_metrics.generate_portfolio_metrics_dataframe()
|
||||
self.logger.info(portfolio_metrics)
|
||||
um.save_user_data(user_id)
|
||||
self.logger.info("Update account state {} for {}".format(trade_date, user_id))
|
||||
|
||||
@@ -258,7 +258,7 @@ class Operator:
|
||||
# 3. generate and save order list
|
||||
order_list = user.strategy.generate_trade_decision(
|
||||
score_series=score_series,
|
||||
current=user.account.current,
|
||||
current=user.account.current_position,
|
||||
trade_exchange=trade_exchange,
|
||||
trade_date=trade_date,
|
||||
)
|
||||
@@ -273,8 +273,8 @@ class Operator:
|
||||
# 5. update account state
|
||||
trade_info = executor.load_trade_info_from_executed_file(user_path=user_path, trade_date=trade_date)
|
||||
update_account(user.account, trade_info, trade_exchange, trade_date)
|
||||
report = user.account.report.generate_report_dataframe()
|
||||
self.logger.info(report)
|
||||
portfolio_metrics = user.account.portfolio_metrics.generate_portfolio_metrics_dataframe()
|
||||
self.logger.info(portfolio_metrics)
|
||||
um.save_user_data(id)
|
||||
self.show(id, path, bench)
|
||||
|
||||
@@ -295,12 +295,12 @@ class Operator:
|
||||
if id not in um.users:
|
||||
raise ValueError("Cannot find user ".format(id))
|
||||
bench = D.features([bench], ["$change"]).loc[bench, "$change"]
|
||||
report = um.users[id].account.report.generate_report_dataframe()
|
||||
report["bench"] = bench
|
||||
portfolio_metrics = um.users[id].account.portfolio_metrics.generate_portfolio_metrics_dataframe()
|
||||
portfolio_metrics["bench"] = bench
|
||||
analysis_result = {}
|
||||
r = (report["return"] - report["bench"]).dropna()
|
||||
r = (portfolio_metrics["return"] - portfolio_metrics["bench"]).dropna()
|
||||
analysis_result["excess_return_without_cost"] = risk_analysis(r)
|
||||
r = (report["return"] - report["bench"] - report["cost"]).dropna()
|
||||
r = (portfolio_metrics["return"] - portfolio_metrics["bench"] - portfolio_metrics["cost"]).dropna()
|
||||
analysis_result["excess_return_with_cost"] = risk_analysis(r)
|
||||
print("Result:")
|
||||
print("excess_return_without_cost:")
|
||||
|
||||
@@ -59,16 +59,16 @@ class User:
|
||||
bench that to be compared, 'SH000905' for csi500
|
||||
"""
|
||||
bench = D.features([benchmark], ["$change"], disk_cache=True).loc[benchmark, "$change"]
|
||||
report = self.account.report.generate_report_dataframe()
|
||||
report["bench"] = bench
|
||||
portfolio_metrics = self.account.portfolio_metrics.generate_portfolio_metrics_dataframe()
|
||||
portfolio_metrics["bench"] = bench
|
||||
analysis_result = {"pred": {}, "excess_return_without_cost": {}, "excess_return_with_cost": {}}
|
||||
r = (report["return"] - report["bench"]).dropna()
|
||||
r = (portfolio_metrics["return"] - portfolio_metrics["bench"]).dropna()
|
||||
analysis_result["excess_return_without_cost"][0] = risk_analysis(r)
|
||||
r = (report["return"] - report["bench"] - report["cost"]).dropna()
|
||||
r = (portfolio_metrics["return"] - portfolio_metrics["bench"] - portfolio_metrics["cost"]).dropna()
|
||||
analysis_result["excess_return_with_cost"][0] = risk_analysis(r)
|
||||
self.logger.info("Result of porfolio:")
|
||||
self.logger.info("excess_return_without_cost:")
|
||||
self.logger.info(analysis_result["excess_return_without_cost"][0])
|
||||
self.logger.info("excess_return_with_cost:")
|
||||
self.logger.info(analysis_result["excess_return_with_cost"][0])
|
||||
return report
|
||||
return portfolio_metrics
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
import math
|
||||
import importlib
|
||||
from pathlib import Path
|
||||
from typing import Iterable
|
||||
|
||||
import pandas as pd
|
||||
@@ -14,8 +13,6 @@ import plotly.graph_objs as go
|
||||
from plotly.subplots import make_subplots
|
||||
from plotly.figure_factory import create_distplot
|
||||
|
||||
from ...utils import get_module_by_module_path
|
||||
|
||||
|
||||
class BaseGraph:
|
||||
""" """
|
||||
@@ -138,7 +135,7 @@ class BaseGraph:
|
||||
:return:
|
||||
"""
|
||||
_figure = go.Figure(data=self.data, layout=self._get_layout())
|
||||
# NOTE: using default 3.x theme
|
||||
# NOTE: Use the default theme from plotly version 3.x, template=None
|
||||
_figure["layout"].update(template=None)
|
||||
return _figure
|
||||
|
||||
@@ -378,8 +375,9 @@ class SubplotsGraph:
|
||||
for k, v in self._sub_graph_layout.items():
|
||||
self._figure["layout"][k].update(v)
|
||||
|
||||
# NOTE: using default 3.x theme
|
||||
self._figure["layout"].update(self._layout, template=None)
|
||||
# NOTE: Use the default theme from plotly version 3.x: template=None
|
||||
self._figure["layout"].update(template=None)
|
||||
self._figure["layout"].update(self._layout)
|
||||
|
||||
@property
|
||||
def figure(self):
|
||||
|
||||
@@ -6,7 +6,7 @@ import pandas as pd
|
||||
|
||||
from ...utils.resam import resam_ts_data
|
||||
from ...strategy.base import ModelStrategy
|
||||
from ...backtest.order import Order, BaseTradeDecision, OrderDir, TradeDecisionWO
|
||||
from ...backtest.decision import Order, BaseTradeDecision, OrderDir, TradeDecisionWO
|
||||
|
||||
from .order_generator import OrderGenWInteract
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ This order generator is for strategies based on WeightStrategyBase
|
||||
"""
|
||||
from ...backtest.position import Position
|
||||
from ...backtest.exchange import Exchange
|
||||
from ...backtest.order import BaseTradeDecision, TradeDecisionWO
|
||||
from ...backtest.decision import BaseTradeDecision, TradeDecisionWO
|
||||
|
||||
import pandas as pd
|
||||
import copy
|
||||
|
||||
@@ -10,7 +10,7 @@ from qlib.utils import lazy_sort_index
|
||||
from ...utils.resam import resam_ts_data, ts_data_last
|
||||
from ...data.data import D
|
||||
from ...strategy.base import BaseStrategy
|
||||
from ...backtest.order import BaseTradeDecision, Order, TradeDecisionWO, TradeRange
|
||||
from ...backtest.decision import BaseTradeDecision, Order, TradeDecisionWO, TradeRange
|
||||
from ...backtest.exchange import Exchange, OrderHelper
|
||||
from ...backtest.utils import CommonInfrastructure, LevelInfrastructure
|
||||
from qlib.utils.file import get_io_object
|
||||
|
||||
Reference in New Issue
Block a user