1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-12 07:16:54 +08:00
This commit is contained in:
Yuchen Fang
2021-01-28 00:34:32 +08:00
committed by you-n-g
parent 7f9216dc90
commit bcadf47f32
37 changed files with 4604 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
from .base import *
from .pa_penalty import *
from .ppo_reward import *
from .vp_penalty import *

View File

@@ -0,0 +1,38 @@
import numpy as np
class Abs_Reward(object):
"""The abstract class for Reward."""
def __init__(self, config):
return
def get_reward(self):
""":return: reward"""
reward = 0
return reward
def __call__(self, *args, **kargs):
return self.get_reward(*args, **kargs)
def isinstant(self):
""":return: Whether the reward should be given at every timestep or only at the end of this episode."""
raise NotImplementedError
class Instant_Reward(Abs_Reward):
def __init__(self, config):
self.ffr_ratio = config["ffr_ratio"]
self.vvr_ratio = config["vvr_ratio"]
def isinstant(self):
return True
class EndEpisode_Reward(Abs_Reward):
def __init__(self, config):
self.ffr_ratio = config["ffr_ratio"]
self.vvr_ratio = config["vvr_ratio"]
def isinstant(self):
return False

View File

@@ -0,0 +1,14 @@
import numpy as np
from .base import Instant_Reward
class PA_Penalty(Instant_Reward):
"""Reward: (Abs(tt_ratio_t - 1) * 10000 * v_t / target - v_t^2 * penalty) / 100"""
def __init__(self, config):
self.penalty = config["penalty"]
def get_reward(self, performance_raise, v_t, target, PA_t, *args):
reward = PA_t * v_t / target
reward -= self.penalty * (v_t / target) ** 2
return reward / 100

View File

@@ -0,0 +1,22 @@
import numpy as np
from .base import Abs_Reward
class PPO_Reward(Abs_Reward):
"""The reward function defined in IJCAI 2020"""
def __init__(self, *args):
pass
def isinstant(self):
return False
def get_reward(self, performace_raise, ffr, this_tt_ratio, is_buy):
if is_buy:
this_tt_ratio = 1 / this_tt_ratio
if this_tt_ratio < 1:
return -1.0
elif this_tt_ratio < 1.1:
return 0.0
else:
return 1.0

View File

@@ -0,0 +1,41 @@
import numpy as np
from .base import Instant_Reward
class VP_Penalty_small(Instant_Reward):
"""Reward: (Abs(vv_ratio_t - 1) * 10000 - v_t^2 * penalty) / 100"""
def __init__(self, config):
self.penalty = config["penalty"]
def get_reward(self, performance_raise, v_t, target, *args):
"""
:param performance_raise: Abs(vv_ratio_t - 1) * 10000.
:param target: Target volume
:param v_t: The traded volume
"""
assert target > 0
reward = performance_raise * v_t / target
reward -= self.penalty * (v_t / target) ** 2
assert not (
np.isnan(reward) or np.isinf(reward)
), f"{performance_raise}, {v_t}, {target}"
return reward / 100
class VP_Penalty_small_vec(VP_Penalty_small):
def get_reward(self, performance_raise, v_t, target, *args):
"""
:param performance_raise: Abs(vv_ratio_t - 1) * 10000.
:param target: Target volume
:param v_t: The traded volume
"""
assert target > 0
reward = performance_raise * v_t.sum() / target
reward -= self.penalty * ((v_t / target) ** 2).sum()
assert not (
np.isnan(reward) or np.isinf(reward)
), f"{performance_raise}, {v_t}, {target}"
return reward / 100