mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-13 07:46:53 +08:00
format
This commit is contained in:
@@ -60,9 +60,7 @@ class RuleObs(BaseObs):
|
||||
prediction = [df_list[i].reshape(-1) for i in range(len(df_list))]
|
||||
for i, p in enumerate(prediction):
|
||||
if len(p) < interval_num:
|
||||
prediction[i] = np.concatenate(
|
||||
(p, np.zeros(interval_num - len(p))), axis=-1
|
||||
)
|
||||
prediction[i] = np.concatenate((p, np.zeros(interval_num - len(p))), axis=-1)
|
||||
# res = np.stack(prediction).transpose().reshape(-1)
|
||||
return np.concatenate(prediction)
|
||||
for i in range(len(self.features)):
|
||||
@@ -73,9 +71,7 @@ class RuleObs(BaseObs):
|
||||
if time == -1:
|
||||
predictions += [0.0] * size
|
||||
else:
|
||||
predictions += (
|
||||
df.iloc[size * time : size * (time + 1)].reshape(-1).tolist()
|
||||
)
|
||||
predictions += df.iloc[size * time : size * (time + 1)].reshape(-1).tolist()
|
||||
elif feature["type"] == "daily":
|
||||
predictions += df.reshape(-1)[:size].tolist()
|
||||
elif feature["type"] == "range":
|
||||
@@ -86,35 +82,19 @@ class RuleObs(BaseObs):
|
||||
else:
|
||||
predictions += df.iloc[time : size + time].reshape(-1).tolist()
|
||||
elif feature["type"] == "interval":
|
||||
if (
|
||||
len(df.iloc[interval * size : (interval + 1) * size].reshape(-1))
|
||||
== size
|
||||
):
|
||||
predictions += (
|
||||
df.iloc[interval * size : (interval + 1) * size]
|
||||
.reshape(-1)
|
||||
.tolist()
|
||||
)
|
||||
if len(df.iloc[interval * size : (interval + 1) * size].reshape(-1)) == size:
|
||||
predictions += df.iloc[interval * size : (interval + 1) * size].reshape(-1).tolist()
|
||||
else:
|
||||
predictions += [0.0] * size
|
||||
elif feature["type"] == "step":
|
||||
if (
|
||||
len(df.iloc[size * (time + 1) : size * (time + 2)].reshape(-1))
|
||||
== size
|
||||
):
|
||||
predictions += (
|
||||
df.iloc[size * (time + 1) : size * (time + 2)]
|
||||
.reshape(-1)
|
||||
.tolist()
|
||||
)
|
||||
if len(df.iloc[size * (time + 1) : size * (time + 2)].reshape(-1)) == size:
|
||||
predictions += df.iloc[size * (time + 1) : size * (time + 2)].reshape(-1).tolist()
|
||||
else:
|
||||
predictions += [0.0] * size
|
||||
|
||||
return np.array(predictions)
|
||||
|
||||
def get_obs(
|
||||
self, raw_df, feature_dfs, t, interval, position, target, is_buy, *args, **kargs
|
||||
):
|
||||
def get_obs(self, raw_df, feature_dfs, t, interval, position, target, is_buy, *args, **kargs):
|
||||
private_state = np.array([position, target, t, self.max_step_num])
|
||||
prediction_state = self.get_feature_res(feature_dfs, t, interval)
|
||||
return {
|
||||
|
||||
@@ -11,17 +11,7 @@ class PPOObs(RuleObs):
|
||||
"""The observation defined in IJCAI 2020. The action of previous state is included in private state"""
|
||||
|
||||
def get_obs(
|
||||
self,
|
||||
raw_df,
|
||||
feature_dfs,
|
||||
t,
|
||||
interval,
|
||||
position,
|
||||
target,
|
||||
is_buy,
|
||||
max_step_num,
|
||||
interval_num,
|
||||
action=0,
|
||||
self, raw_df, feature_dfs, t, interval, position, target, is_buy, max_step_num, interval_num, action=0,
|
||||
):
|
||||
if t == -1:
|
||||
self.private_states = []
|
||||
@@ -32,10 +22,7 @@ class PPOObs(RuleObs):
|
||||
self.private_states.append(private_state)
|
||||
list_private_state = np.concatenate(self.private_states)
|
||||
list_private_state = np.concatenate(
|
||||
(
|
||||
list_private_state,
|
||||
[0.0] * 3 * (interval_num + 1 - len(self.private_states)),
|
||||
)
|
||||
(list_private_state, [0.0] * 3 * (interval_num + 1 - len(self.private_states)),)
|
||||
)
|
||||
seqlen = np.array([interval])
|
||||
return np.concatenate((public_state, list_private_state, seqlen))
|
||||
|
||||
@@ -16,18 +16,7 @@ class TeacherObs(RuleObs):
|
||||
"""
|
||||
|
||||
def get_obs(
|
||||
self,
|
||||
raw_df,
|
||||
feature_dfs,
|
||||
t,
|
||||
interval,
|
||||
position,
|
||||
target,
|
||||
is_buy,
|
||||
max_step_num,
|
||||
interval_num,
|
||||
*args,
|
||||
**kargs,
|
||||
self, raw_df, feature_dfs, t, interval, position, target, is_buy, max_step_num, interval_num, *args, **kargs,
|
||||
):
|
||||
if t == -1:
|
||||
self.private_states = []
|
||||
@@ -36,18 +25,13 @@ class TeacherObs(RuleObs):
|
||||
self.private_states.append(private_state)
|
||||
list_private_state = np.concatenate(self.private_states)
|
||||
list_private_state = np.concatenate(
|
||||
(
|
||||
list_private_state,
|
||||
[0.0] * 2 * (interval_num + 1 - len(self.private_states)),
|
||||
)
|
||||
(list_private_state, [0.0] * 2 * (interval_num + 1 - len(self.private_states)),)
|
||||
)
|
||||
seqlen = np.array([interval])
|
||||
assert not (
|
||||
np.isnan(list_private_state).any() | np.isinf(list_private_state).any()
|
||||
), f"{private_state}, {target}"
|
||||
assert not (
|
||||
np.isnan(public_state).any() | np.isinf(public_state).any()
|
||||
), f"{public_state}"
|
||||
assert not (np.isnan(public_state).any() | np.isinf(public_state).any()), f"{public_state}"
|
||||
return np.concatenate((public_state, list_private_state, seqlen))
|
||||
|
||||
|
||||
@@ -55,35 +39,17 @@ class RuleTeacher(RuleObs):
|
||||
""" """
|
||||
|
||||
def get_obs(
|
||||
self,
|
||||
raw_df,
|
||||
feature_dfs,
|
||||
t,
|
||||
interval,
|
||||
position,
|
||||
target,
|
||||
is_buy,
|
||||
max_step_num,
|
||||
interval_num,
|
||||
*args,
|
||||
**kargs,
|
||||
self, raw_df, feature_dfs, t, interval, position, target, is_buy, max_step_num, interval_num, *args, **kargs,
|
||||
):
|
||||
if t == -1:
|
||||
self.private_states = []
|
||||
public_state = feature_dfs[0].reshape(-1)[: 6 * 240]
|
||||
private_state = np.array([position / target, (t + 1) / max_step_num])
|
||||
teacher_action = self.get_feature_res(feature_dfs, t, interval)[
|
||||
-self.features[1]["size"] :
|
||||
]
|
||||
teacher_action = self.get_feature_res(feature_dfs, t, interval)[-self.features[1]["size"] :]
|
||||
self.private_states.append(private_state)
|
||||
list_private_state = np.concatenate(self.private_states)
|
||||
list_private_state = np.concatenate(
|
||||
(
|
||||
list_private_state,
|
||||
[0.0] * 2 * (interval_num + 1 - len(self.private_states)),
|
||||
)
|
||||
(list_private_state, [0.0] * 2 * (interval_num + 1 - len(self.private_states)),)
|
||||
)
|
||||
seqlen = np.array([interval])
|
||||
return np.concatenate(
|
||||
(teacher_action, public_state, list_private_state, seqlen)
|
||||
)
|
||||
return np.concatenate((teacher_action, public_state, list_private_state, seqlen))
|
||||
|
||||
Reference in New Issue
Block a user