-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathmarket_pg.py
150 lines (113 loc) · 4.7 KB
/
market_pg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import os
import numpy as np
from market_env import MarketEnv
from market_model_builder import MarketPolicyGradientModelBuilder
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class PolicyGradient:
def __init__(self, env, discount = 0.99, model_filename = None, history_filename = None):
self.env = env
self.discount = discount
self.model_filename = model_filename
self.history_filename = history_filename
from keras.optimizers import SGD
self.model = MarketPolicyGradientModelBuilder(modelFilename).getModel()
sgd = SGD(lr = 0.1, decay = 1e-6, momentum = 0.9, nesterov = True)
self.model.compile(loss='mse', optimizer='rmsprop')
def discount_rewards(self, r):
discounted_r = np.zeros_like(r)
running_add = 0
r = r.flatten()
for t in reversed(xrange(0, r.size)):
if r[t] != 0:
running_add = 0
running_add = running_add * self.discount + r[t]
discounted_r[t] = running_add
return discounted_r
def train(self, max_episode = 1000000, max_path_length = 200, verbose = 0):
env = self.env
model = self.model
avg_reward_sum = 0.
for e in xrange(max_episode):
env.reset()
observation = env.reset()
game_over = False
reward_sum = 0
inputs = []
outputs = []
predicteds = []
rewards = []
while not game_over:
aprob = model.predict(observation)[0]
inputs.append(observation)
predicteds.append(aprob)
if aprob.shape[0] > 1:
action = np.random.choice(self.env.action_space.n, 1, p = aprob / np.sum(aprob))[0]
y = np.zeros([self.env.action_space.n])
y[action] = 1.
outputs.append(y)
else:
action = 0 if np.random.uniform() < aprob else 1
y = [float(action)]
outputs.append(y)
observation, reward, game_over, info = self.env.step(action)
reward_sum += float(reward)
rewards.append(float(reward))
if verbose > 0:
if env.actions[action] == "LONG" or env.actions[action] == "SHORT":
color = bcolors.FAIL if env.actions[action] == "LONG" else bcolors.OKBLUE
print "%s:\t%s\t%.2f\t%.2f\t" % (info["dt"], color + env.actions[action] + bcolors.ENDC, reward_sum, info["cum"]) + ("\t".join(["%s:%.2f" % (l, i) for l, i in zip(env.actions, aprob.tolist())]))
avg_reward_sum = avg_reward_sum * 0.99 + reward_sum * 0.01
toPrint = "%d\t%s\t%s\t%.2f\t%.2f" % (e, info["code"], (bcolors.FAIL if reward_sum >= 0 else bcolors.OKBLUE) + ("%.2f" % reward_sum) + bcolors.ENDC, info["cum"], avg_reward_sum)
print toPrint
if self.history_filename != None:
os.system("echo %s >> %s" % (toPrint, self.history_filename))
dim = len(inputs[0])
inputs_ = [[] for i in xrange(dim)]
for obs in inputs:
for i, block in enumerate(obs):
inputs_[i].append(block[0])
inputs_ = [np.array(inputs_[i]) for i in xrange(dim)]
outputs_ = np.vstack(outputs)
predicteds_ = np.vstack(predicteds)
rewards_ = np.vstack(rewards)
discounted_rewards_ = self.discount_rewards(rewards_)
#discounted_rewards_ -= np.mean(discounted_rewards_)
discounted_rewards_ /= np.std(discounted_rewards_)
#outputs_ *= discounted_rewards_
for i, r in enumerate(zip(rewards, discounted_rewards_)):
reward, discounted_reward = r
if verbose > 1:
print outputs_[i],
#outputs_[i] = 0.5 + (2 * outputs_[i] - 1) * discounted_reward
if discounted_reward < 0:
outputs_[i] = 1 - outputs_[i]
outputs_[i] = outputs_[i] / sum(outputs_[i])
outputs_[i] = np.minimum(1, np.maximum(0, predicteds_[i] + (outputs_[i] - predicteds_[i]) * abs(discounted_reward)))
if verbose > 1:
print predicteds_[i], outputs_[i], reward, discounted_reward
model.fit(inputs_, outputs_, nb_epoch = 1, verbose = 0, shuffle = True)
model.save_weights(self.model_filename)
if __name__ == "__main__":
import sys
import codecs
codeListFilename = sys.argv[1]
modelFilename = sys.argv[2] if len(sys.argv) > 2 else None
historyFilename = sys.argv[3] if len(sys.argv) > 3 else None
codeMap = {}
f = codecs.open(codeListFilename, "r", "utf-8")
for line in f:
if line.strip() != "":
tokens = line.strip().split(",") if not "\t" in line else line.strip().split("\t")
codeMap[tokens[0]] = tokens[1]
f.close()
env = MarketEnv(dir_path = "./data/", target_codes = codeMap.keys(), input_codes = [], start_date = "2010-08-25", end_date = "2015-08-25", sudden_death = -1.0)
pg = PolicyGradient(env, discount = 0.9, model_filename = modelFilename, history_filename = historyFilename)
pg.train(verbose = 1)