-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathevaluation.py
359 lines (303 loc) · 12.5 KB
/
evaluation.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import os
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import torch
# local imports
from agent import Agent, optimal_max_pos_vec
from models import Actor
def test_models(
path_weights, env, fc1_units=16, fc2_units=8, random_state=1024, n_episodes=100
):
"""
Description
---------------
Evaluate saved models on a number of environments simulated with a fixed random state.
The evaluation is the average score over the total number of test episodes.
Parameters
---------------
path_weights : String, path to the saved training weights.
env : Object of class Environment, the evaluation environment (It should be the same
as the training environment).
fc1_units : Int, number of nodes in the first layer of the network.
fc2_units : Int, number of nodes in the second layer of the network.
random_state : Int, a fixed random state to evaluate all models on the same episodes.
n_episodes : Int, the total number of evaluation episodes.
Returns
---------------
scores : Dict, - keys : training iteration of the saved model.
- values : average score over the evaluation episodes.
scores_episodes : Dict, - keys : training iteration of the saved model.
- values : Dict, - keys : index of the evaluation episode.
- values : score over the evaluation episode.
scores_cumsum : Dict, - keys : training iteration of the saved model.
- values : Dict,
- keys : index of the evaluation episode.
- values : cumulative sum of the reward at each time step
per episode.
pnls : Dict, - keys : training iteration of the saved model.
- values : Dict, - keys : index of the evaluation episode.
- values : pnl per episode.
positions : Dict, - keys : training iteration of the saved model.
- values : Dict, - keys : index of the evaluation episode.
- values : positions per episode.
"""
models_names = os.listdir(path_weights)
if ".ipynb_checkpoints" in models_names:
models_names.remove(".ipynb_checkpoints")
agent = Agent()
rng = np.random.RandomState(random_state)
random_states = rng.randint(0, int(1e6), size=n_episodes)
scores = {}
scores_episodes = {}
scores_cumsum = {}
positions = {}
pnls = {}
for model_name in models_names:
# print(model_name)
state_dict = torch.load(path_weights + model_name)
model = Actor(env.state_size, fc1_units=fc1_units, fc2_units=fc2_units)
model.load_state_dict(state_dict)
score, score_episode, score_cumsum, pnl, position = env.test(
agent, model, total_episodes=n_episodes, random_states=random_states
)
scores[int(model_name[5:][:-4])] = score
scores_episodes[int(model_name[5:][:-4])] = score_episode
scores_cumsum[int(model_name[5:][:-4])] = score_cumsum
positions[int(model_name[5:][:-4])] = position
pnls[int(model_name[5:][:-4])] = pnl
# print('Average score : %.2f' % score)
# print('\n')
return scores, scores_episodes, scores_cumsum, pnls, positions
def plot_bars(scores):
"""
Description
---------------
Bar plot of the evaluation score across the models. Note that the optimal model is indexed by 0
Parameters
---------------
scores : First return of test_models function (see its doc for more details)
Returns
---------------
"""
scores = dict(sorted(scores.items()))
_ = plt.figure(figsize=(30, 10))
plt.bar(range(len(scores)), list(scores.values()), align="center")
plt.xticks(range(len(scores)), list(scores.keys()))
plt.title("Overview of the perfomance of each model", fontsize=20)
plt.show()
def plot_min_max(env, models_keys, scores_episodes):
"""
Description
---------------
For each model indexed by models_keys and the optimal model, plot the min score signal
and max score signals along with their respective scores. Note that the optimal model
is indexed by 0
Parameters
---------------
env : Object of class Environment, the evaluation environment (It should be the same
as the training environment).
models_keys : List of 5 elements containing the indices of the models to use.
scores_episodes : Second return of test_models function (see its doc for more
details).
Returns
---------------
"""
_ = plt.figure(figsize=(20, 10))
models_keys = [-1] + models_keys
for i, model_key in enumerate(models_keys):
score_episode = scores_episodes[model_key]
random_state_min, random_state_max = (
min(score_episode, key=score_episode.get),
max(score_episode, key=score_episode.get),
)
env.reset(random_state=random_state_min)
signal_min = env.signal
env.reset(random_state=random_state_max)
signal_max = env.signal
plt.subplot(2, 3, i + 1)
plt.plot(
range(len(signal_min)),
signal_min,
label="min %.2f" % score_episode[random_state_min],
)
plt.plot(
range(len(signal_max)),
signal_max,
label="max %.2f" % score_episode[random_state_max],
)
plt.legend()
plt.title("model %d" % model_key)
plt.show()
def plot_hist(model_key, scores_episodes):
"""
Description
---------------
Plot histogram of scores across the evaluation episodes of model indexed by model_key
and compare it with the optimal model
Parameters
---------------
model_key : Int, the index of the considered model.
scores_episodes : Second return of test_models function (see its doc for more
details).
Returns
---------------
"""
_ = plt.figure(figsize=(15, 4))
plt.subplot(121)
sns.distplot(list(scores_episodes[model_key].values()))
plt.title("score distribution of model %d" % model_key)
plt.subplot(122)
sns.distplot(list(scores_episodes[-1].values()))
plt.title("score distribution of optimal model")
plt.show()
def optimal_f(p, pi, lambd=0.5, psi=0.3, cost="trade_l2"):
"""
Description
--------------
Optimal solution for cost models with l2, l1 or no trading costs.
Parameters
--------------
p : Float, the next signal value.
pi : Float, the current position.
lambd : Float > 0, Parameter of the cost model.
psi : Float > 0, Parameter of our model defining the trading cost.
cost : String in ['none', 'trade_l1', 'trade_l2'], cost model.
Returns
--------------
Float, The optimal solution evaluation (which is the next position).
"""
if cost == "trade_0":
return p / (2 * lambd) - pi
elif cost == "trade_l2":
return p / (2 * (lambd + psi)) + psi * pi / (lambd + psi) - pi
elif cost == "trade_l1":
if p <= -psi + 2 * lambd * pi:
return (p + psi) / (2 * lambd) - pi
elif -psi + 2 * lambd * pi < p < psi + 2 * lambd * pi:
return 0
elif p >= psi + 2 * lambd * pi:
return (p - psi) / (2 * lambd) - pi
# Vectorizing the optimal solution.
optimal_f_vec = np.vectorize(optimal_f, excluded=set(["pi", "lambd", "psi", "cost"]))
def plot_function(
path_weights,
env,
models_keys,
fc1_units=16,
fc2_units=8,
low=-1,
high=1,
step=0.01,
pi=0.5,
psi=0.3,
lambd=0.5,
thresh=2,
clip=True,
):
"""
Description
---------------
Plot the functions of each model along with the optimal function given a position and
a range for the signal.
Parameters
---------------
path_weights : String, path to the saved training weights.
env : Object of class Environment, the evaluation environment (It should be the same
as the training environment).
models_keys : List of 6 elements containing the indices of the models to use.
fc1_units : Int, number of nodes in the first layer of the network.
fc2_units : Int, number of nodes in the second layer of the network.
low : Float, minimum of the signal range.
high : Float, maximum of the signal range.
step : Float, step size along the signal range.
pi : Float, plot using -pi, 0 and pi.
psi : Float or None, parameter of the solution for both second and third
model.
None -> Only plot the myopic solution function.
Float -> Also plot the solution function with the given parameter
psi.
lambd : Float, parameter controlling the slope of the solution outside the
band.
thresh : Float>0, price threshold to make a trade.
clip : Boolean, whether to clip positions beyond maxpos or not.
Returns
---------------
"""
range_values = np.arange(low, high, step)
signal_zeros = torch.tensor(
np.vstack((range_values, np.zeros(len(range_values)))).T, dtype=torch.float
)
signal_ones_pos = torch.tensor(
np.vstack((range_values, pi * np.ones(len(range_values)))).T, dtype=torch.float
)
signal_ones_neg = torch.tensor(
np.vstack((range_values, -pi * np.ones(len(range_values)))).T, dtype=torch.float
)
if env.squared_risk:
optimal1 = optimal_f_vec(
signal_ones_neg[:, 0].numpy(), -pi, lambd=lambd, psi=psi, cost=env.cost
)
optimal2 = optimal_f_vec(
signal_zeros[:, 0].numpy(), 0, lambd=lambd, psi=psi, cost=env.cost
)
optimal3 = optimal_f_vec(
signal_ones_pos[:, 0].numpy(), pi, lambd=lambd, psi=psi, cost=env.cost
)
else:
optimal1 = optimal_max_pos_vec(
signal_ones_neg[:, 0].numpy(), -pi, thresh, env.max_pos
)
optimal2 = optimal_max_pos_vec(
signal_zeros[:, 0].numpy(), 0, thresh, env.max_pos
)
optimal3 = optimal_max_pos_vec(
signal_ones_pos[:, 0].numpy(), pi, thresh, env.max_pos
)
_ = plt.figure(figsize=(20, 30))
for i, model_key in enumerate(models_keys):
state_dict = torch.load(path_weights + "ddpg_" + str(model_key) + ".pth")
model = Actor(env.state_size, fc1_units=fc1_units, fc2_units=fc2_units)
model.load_state_dict(state_dict)
model.eval()
with torch.no_grad():
if clip:
model1 = np.clip(
model(signal_ones_neg)[:, 0].data.numpy(),
-env.max_pos + pi,
env.max_pos + pi,
)
model2 = np.clip(
model(signal_zeros)[:, 0].data.numpy(), -env.max_pos, env.max_pos
)
model3 = np.clip(
model(signal_ones_pos)[:, 0].data.numpy(),
-env.max_pos - pi,
env.max_pos - pi,
)
else:
model1 = model(signal_ones_neg)[:, 0].data.numpy()
model2 = model(signal_zeros)[:, 0].data.numpy()
model3 = model(signal_ones_pos)[:, 0].data.numpy()
plt.subplot(len(models_keys), 3, i * 3 + 1)
plt.plot(signal_ones_neg[:, 0].numpy(), model1, label="model")
plt.plot(signal_ones_neg[:, 0].numpy(), optimal1, label="optimal")
plt.xlim(-4, 4)
plt.ylim(-4, 4)
plt.title(r"model : %d, $\pi = -%s$" % (model_key, str(pi)))
plt.legend()
plt.subplot(len(models_keys), 3, i * 3 + 2)
plt.plot(signal_zeros[:, 0].numpy(), model2, label="model")
plt.plot(signal_zeros[:, 0].numpy(), optimal2, label="optimal")
plt.xlim(-4, 4)
plt.ylim(-4, 4)
plt.title(r"model : %d, $\pi = 0$" % model_key)
plt.legend()
plt.subplot(len(models_keys), 3, i * 3 + 3)
plt.plot(signal_ones_pos[:, 0].numpy(), model3, label="model")
plt.plot(signal_ones_pos[:, 0].numpy(), optimal3, label="optimal")
plt.xlim(-4, 4)
plt.ylim(-4, 4)
plt.title(r"model : %d, $\pi = %s$" % (model_key, str(pi)))
plt.legend()
plt.show()