-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew-repeated-simulation.py
More file actions
413 lines (305 loc) · 11.3 KB
/
Copy pathnew-repeated-simulation.py
File metadata and controls
413 lines (305 loc) · 11.3 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import json
import os
import sys
from copy import deepcopy
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backends.backend_pdf import PdfPages
"""
This file runs the same simulation multiple times with the same parameters and averages the results.
The final product is a PDF file with multiple plots.
"""
def usage():
print(
"usage: python3 new-repeated-simulation.py (infl | not-infl) [<output-pdf>] [<output-csv>]"
)
exit()
if len(sys.argv) < 2:
usage()
# A very small constant
EPS = np.finfo(float).eps
# Sometimes the price might get too high or too low and Python complains, in
# those chases, either tweak the parameters or reduce the time horizon.
TIME_HORIZON = 3000
# Number of independent runs to perform to average the results
RUNS = 10
# Save the results in the Overleaf folder for automatic upload
home_directory = os.path.expanduser("~")
CSV_DEST_TEMPLATE = (
home_directory + "/Dropbox/Applicazioni/Overleaf/LOB & RL/plots/learning-simulation.csv"
)
# Write in the CSV one round every {CSV_ROUND_SKIP} to avoid crowded and slow plots
CSV_ROUND_SKIP = 10
# ---- Price noise
NOISE_STD = 0.5
# ---- Define feasibility constraints
MIN_INV = 0
MIN_CASH = 0
# --- A more readable dict
class dotdict(dict):
"""dot.notation access to dictionary attributes"""
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
def __add__(self, other):
return dotdict({k: v + other[k] for (k, v) in self.items()})
def __sub__(self, other):
return dotdict({k: v - other[k] for (k, v) in self.items()})
def __truediv__(self, div):
return dotdict({k: v / div for (k, v) in self.items()})
def __pow__(self, p):
return dotdict({k: v**p for (k, v) in self.items()})
# ---- Choose a strategy
if sys.argv[1] == "infl":
PHI = 0.7
KALPHA = 0.5
KBETA = 0.5
VALPHA = 0.5
VBETA = 0.5
elif sys.argv[1] == "not-infl":
PHI = 0.3
KALPHA = 0.5
KBETA = 0.5
VALPHA = 0.5
VBETA = 0.5
else:
usage()
KAPPA = PHI * KALPHA * VALPHA - (1 - PHI) * KBETA * VBETA
MU = PHI * np.log(1 + KALPHA * VALPHA) + (1 - PHI) * np.log(1 - KBETA * VBETA)
def inflationary():
return MU > 0
# ---- Set initial state
INITIAL_STATE = dotdict(
{
"price": 1,
"tradedcash": 0,
"kappa": 0,
"taker": dotdict({"inv": 1, "cash": 1, "wealth": 1}),
"maker": dotdict({"inv": 1, "cash": 1, "wealth": 1}),
}
)
# --- Quantity selection
def A(state):
return min(state.maker.inv - MIN_INV, (state.taker.cash - MIN_CASH) / state.price)
def B(state):
return min((state.maker.cash - MIN_CASH) / state.price, state.taker.inv - MIN_INV)
def pick_quantity(state, side):
return KALPHA**2 * A(state) if side else -(KBETA**2) * B(state)
# ---- Misc
def avg(xs):
return np.cumsum(xs) / np.arange(1, len(xs) + 1)
def reject_outliers(mean, std, err=6):
return np.clip(mean, a_max=std * err, a_min=-std * err)
# ---- Price evolution
def noise():
# return np.random.uniform(0.6, 1.4)
return np.random.lognormal(0, NOISE_STD)
def delta(state, quantity):
if quantity > 0:
return state.price * KALPHA * VALPHA
return -state.price * KBETA * VBETA
def update(state, quantity):
dt = delta(state, quantity)
epsilon = noise()
next_price = state.price + dt
price_difference = epsilon * dt - (1 - epsilon) * state.price
return dotdict(
{
"price": next_price * epsilon,
"tradedcash": next_price * quantity,
"kappa": dt / state.price,
"taker": dotdict(
{
"inv": state.taker.inv + quantity,
"cash": state.taker.cash - next_price * quantity,
"wealth": state.taker.wealth + price_difference * state.taker.inv,
}
),
"maker": dotdict(
{
"inv": state.maker.inv - quantity,
"cash": state.maker.cash + next_price * quantity,
"wealth": state.maker.wealth + price_difference * state.maker.inv,
}
),
}
)
# ---- Data visualization
MAX_INV = INITIAL_STATE.taker.inv + INITIAL_STATE.maker.inv
MAX_CASH = INITIAL_STATE.taker.cash + INITIAL_STATE.maker.cash
def plot_history(history, std):
time = np.arange(TIME_HORIZON)
Ps = np.array([s.price for (s, _) in history])
std_Ps = np.array([s.price for (s, _) in std])
Ps = reject_outliers(Ps, std_Ps)
W_Ts = np.array([s.taker.wealth for (s, _) in history])
std_W_Ts = np.array([s.taker.wealth for (s, _) in std])
W_Ts = reject_outliers(W_Ts, std_W_Ts)
W_Ms = np.array([s.maker.wealth for (s, _) in history])
std_W_Ms = np.array([s.maker.wealth for (s, _) in std])
W_Ms = reject_outliers(W_Ms, std_W_Ms)
Qs = np.array([q for (_, q) in history])
std_Qs = np.array([q for (_, q) in std])
Qs = reject_outliers(Qs, std_Qs)
I_Ts = np.array([s.taker.inv for (s, _) in history])
std_I_Ts = np.array([s.taker.inv for (s, _) in std])
I_Ts = reject_outliers(I_Ts, std_I_Ts)
I_Ms = np.array([s.maker.inv for (s, _) in history])
std_I_Ms = np.array([s.maker.inv for (s, _) in std])
I_Ms = reject_outliers(I_Ms, std_I_Ms)
C_Ts = np.array([s.taker.cash for (s, _) in history])
std_C_Ts = np.array([s.taker.cash for (s, _) in std])
C_Ts = reject_outliers(C_Ts, std_C_Ts)
C_Ms = np.array([s.maker.cash for (s, _) in history])
std_C_Ms = np.array([s.maker.cash for (s, _) in std])
C_Ms = reject_outliers(C_Ms, std_C_Ms)
QPs = np.array([s.tradedcash for (s, _) in history])
std_QPs = np.array([s.tradedcash for (s, _) in std])
QPs = reject_outliers(QPs, std_QPs)
As = np.array([A(s) for (s, _) in history])
# std_As = np.array([A(s) for (s, _) in std])
Bs = np.array([B(s) for (s, _) in history])
# std_Bs = np.array([B(s) for (s, _) in std])
kappas = np.array([s.kappa for (s, q) in history])
mpl.rcParams.update(
{
"font.size": 16,
"text.usetex": True,
"text.latex.preamble": r"""
\usepackage{libertine}
\usepackage[libertine]{newtxmath}
\newcommand{\taker}{{\mathbb{T}}}
\newcommand{\maker}{{\mathbb{M}}}
""",
"axes.linewidth": 0.4,
"lines.linewidth": 1.5,
"lines.markersize": 3.5,
"xtick.direction": "in",
"ytick.direction": "in",
"xtick.minor.visible": False,
"ytick.minor.visible": False,
"xtick.major.width": 0.4,
"ytick.major.width": 0.4,
"grid.linewidth": 0.2,
"axes.grid": True,
"grid.linestyle": "--",
"savefig.pad_inches": 0.02,
"axes.prop_cycle": plt.cycler(color=plt.cm.tab10.colors),
}
)
taker_color = "C1"
maker_color = "C2"
other_color = "C4"
other_other_color = "C6"
alt_color = "C9"
zero_color = "grey"
# initial_color = "C6"
pp = PdfPages(sys.argv[2])
FIG_SIZE = (10, 4)
fig, axs = plt.subplots(1, 2, figsize=FIG_SIZE, constrained_layout=True, sharex=True)
axs[0].fill_between(time, Ps + std_Ps, Ps - std_Ps, color=other_color, alpha=0.3)
axs[0].plot(Ps, c=other_color)
# axs[0].set_title("$P_t$")
axs[0].set_yscale("log")
axs[1].fill_between(time, W_Ms + std_W_Ms, W_Ms - std_W_Ms, color=maker_color, alpha=0.3)
axs[1].plot(W_Ms, label="$W^\\maker_t$", c=maker_color)
axs[1].fill_between(time, W_Ts + std_W_Ts, W_Ts - std_W_Ts, color=taker_color, alpha=0.3)
axs[1].plot(W_Ts, label="$W^\\taker_t$", c=taker_color)
# axs[1].set_title("Wealth")
axs[1].set_yscale("log")
axs[1].legend()
pp.savefig(fig)
fig, axs = plt.subplots(1, 2, figsize=FIG_SIZE, constrained_layout=True, sharex=True)
axs[0].fill_between(time, Qs + std_Qs, Qs - std_Qs, color=other_color, alpha=0.3)
axs[0].scatter(time, Qs, s=0.5, c=other_color)
if not inflationary():
axs[0].plot(avg(Qs), label="avg", c=alt_color, linestyle="--", lw=2)
axs[0].set_yscale("symlog", linthresh=1e-8)
axs[0].yaxis.get_major_locator().numticks = 6
# axs[0].set_title("$Q_t$")
axs[1].fill_between(time, QPs + std_QPs, QPs - std_QPs, color=other_color, alpha=0.3)
axs[1].scatter(time, QPs, s=0.5, c=other_color)
if inflationary():
axs[1].plot(avg(QPs), label="avg", c=alt_color, linestyle="--", lw=2)
axs[1].axhline(0, c=zero_color, alpha=0.4)
# axs[1].set_title("$Q_t P_{t \\text{+} 1}$")
# axs[1].legend()
pp.savefig(fig)
fig, axs = plt.subplots(1, 2, figsize=FIG_SIZE, constrained_layout=True, sharex=True)
axs[0].fill_between(time, I_Ts + std_I_Ts, I_Ts - std_I_Ts, color=taker_color, alpha=0.3)
axs[0].plot(I_Ts, c=taker_color, label="$I^\\taker_t$")
if not inflationary():
axs[0].plot(avg(I_Ts), c=alt_color, linestyle="--", lw=2)
axs[0].fill_between(time, I_Ms + std_I_Ms, I_Ms - std_I_Ms, color=maker_color, alpha=0.3)
axs[0].plot(I_Ms, c=maker_color, label="$I^\\maker_t$")
if not inflationary():
axs[0].plot(avg(I_Ms), c=alt_color, linestyle="--", lw=2)
# axs[0].set_title("Inventory")
axs[0].legend()
axs[1].fill_between(time, C_Ts + std_C_Ts, C_Ts - std_C_Ts, color=taker_color, alpha=0.3)
axs[1].plot(C_Ts, c=taker_color, label="$C^\\taker_t$")
if inflationary():
axs[1].plot(avg(C_Ts), c=alt_color, linestyle="--", lw=2)
axs[1].fill_between(time, C_Ms + std_C_Ms, C_Ms - std_C_Ms, color=maker_color, alpha=0.3)
axs[1].plot(C_Ms, c=maker_color, label="$C^\\maker_t$")
if inflationary():
axs[1].plot(avg(C_Ms), c=alt_color, linestyle="--", lw=2)
# axs[1].set_title("Cash")
axs[1].legend()
pp.savefig(fig)
fig, axs = plt.subplots(1, 2, figsize=FIG_SIZE, constrained_layout=True, sharex=True)
# axs[0].fill_between(time, kappas + std_kappas, kappas - std_kappas, color=other_color, alpha=0.3)
axs[0].scatter(time, kappas, s=0.5, c=other_color)
axs[0].plot(avg(kappas), c=alt_color, linestyle="--", lw=2)
axs[0].axhline(KAPPA, label="$\\kappa$", c=other_other_color)
# axs[0].axhline(0, c=zero_color, alpha=0.4)
# axs[0].set_title("$\\delta_t / P_t$")
axs[0].legend()
axs[1].plot(As, label="$A_t$", c=other_color)
axs[1].plot(Bs, label="$B_t$", c=other_other_color)
# axs[1].set_title("Tradeable amounts")
axs[1].set_yscale("log")
axs[1].legend()
pp.savefig(fig)
pp.close()
# ---- Misc
def sanity_check(state):
assert state.price > 0, f"negative price ({state.price})"
assert state.taker.inv > MIN_INV, f"taker inv too low ({state.taker.inv})"
assert state.maker.inv > MIN_INV, f"maker inv too low ({state.maker.inv})"
assert state.taker.cash > MIN_CASH, f"taker cash too low ({state.taker.cash})"
assert state.maker.cash > MIN_CASH, f"maker cash too low ({state.maker.cash})"
# ---- Play the game
def main():
if inflationary():
print(f"The proposed strategy IS inflationary (mu: {MU:.5f})")
else:
print(f"The proposed strategy IS NOT inflationary (mu: {MU:.5f})")
states = [[deepcopy(INITIAL_STATE) for _ in range(RUNS)] for _ in range(TIME_HORIZON)]
quantities = [[0 for _ in range(RUNS)] for _ in range(TIME_HORIZON)]
# Set the seed
rng = np.random.default_rng() # 11235813
for run in range(RUNS):
state = deepcopy(INITIAL_STATE)
sides = rng.random(TIME_HORIZON) < PHI
for round, side in enumerate(sides):
quantity = pick_quantity(state, side)
state = update(state, quantity)
sanity_check(state)
states[round][run] = deepcopy(state)
quantities[round][run] = quantity
avg_state = [sum(ss, start=INITIAL_STATE) / RUNS for ss in states]
# Hand-compute std
avg_2_state = [(sum(ss, start=INITIAL_STATE) / RUNS) ** 2 for ss in states]
avg_state_2 = [sum([s**2 for s in ss], start=INITIAL_STATE) / RUNS for ss in states]
std_state = [(a - b) ** 1 / 2 for (a, b) in zip(avg_state_2, avg_2_state)]
avg_quantity = [np.sum(qs) / RUNS for qs in quantities]
std_quantity = [np.std(qs) for qs in quantities]
avg_history = list(zip(avg_state, avg_quantity))
std_history = list(zip(std_state, std_quantity))
print("Final average state")
print(json.dumps(avg_history[-1][0], indent=2))
plot_history(avg_history, std_history)
if __name__ == "__main__":
main()