-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution_reference.py
121 lines (95 loc) · 3.66 KB
/
solution_reference.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
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pprint import pprint
from scipy.linalg import expm
import math
#https://arxiv.org/pdf/1105.3115.pdf
## BASIC VARIABLES
max_inventory_Q = 30
min_inventory_Q = -max_inventory_Q
iterations_reference = 2 * max_inventory_Q + 1
#sigma = 0.3
sigma = 0.3
A = 0.9
kappa = 0.3
gamma = 0.01
T = 600
dt = 1
## ALPHA, Nill
alpha = kappa / 2 * gamma * sigma ** 2
nill = A * (1 + gamma / kappa) ** - (1 + kappa / gamma)
assert len(list(range(min_inventory_Q, max_inventory_Q + 1, 1))) == iterations_reference
matrix_M = []
first_row = []
last_row = []
list_middle_rows = []
for i in range(min_inventory_Q, max_inventory_Q + 1, 1):
# Populating first row
if i == min_inventory_Q:
first_row.append(alpha * max_inventory_Q ** 2)
first_row.append(-nill)
first_row.extend([0 for j in range(iterations_reference - 2)])
# Populating last row
elif i == max_inventory_Q:
last_row.extend([0 for j in range(iterations_reference - 2)])
last_row.append(-nill)
last_row.append(alpha * max_inventory_Q ** 2)
# Populating middle rows
else:
middle_row = []
if i < 0:
middle_row.extend([-nill, alpha * ( max_inventory_Q - (max_inventory_Q - abs(i)) ) ** 2, -nill])
middle_row.extend([0 for j in range(iterations_reference - 3)])
if i > -max_inventory_Q + 1:
for j in range(0, max_inventory_Q + i - 1, 1):
middle_row.insert(0,middle_row.pop())
if i == 0:
middle_row = [0 for j in range(iterations_reference)]
middle_row[max_inventory_Q + 1] = -nill
middle_row[max_inventory_Q - 1] = -nill
assert len(middle_row) == iterations_reference
if i > 0:
middle_row.extend([0 for j in range(iterations_reference - 3)])
middle_row.extend([-nill, alpha * ( max_inventory_Q - (max_inventory_Q - abs(i)) ) ** 2, -nill])
for j in range(0, max_inventory_Q - i - 1, 1):
middle_row.append(middle_row.pop(0))
assert len(middle_row) == iterations_reference
list_middle_rows.append(middle_row)
matrix_M.append(first_row)
matrix_M.extend(list_middle_rows)
matrix_M.append(last_row)
assert len(first_row) == iterations_reference
assert len(last_row) == iterations_reference
# Converting to numpy array
matrix_M = np.matrix(matrix_M)
# Simulating quotes
list_evolution_bid = []
for t in range(0, T , 1):
dict_bid = {}
dict_ask = {}
list_bid_at_t = []
container = -matrix_M * (T - t)
v_of_t = expm(container)
v_of_t = np.sum(np.multiply(v_of_t, np.ones(v_of_t.shape[0])), axis=1)
for q in range(0, iterations_reference -1, 1):
bid = (1 / kappa) * np.log(v_of_t[q] / v_of_t[q + 1]) + (1 / gamma) * np.log(1 + gamma / kappa)
ask = (1 / kappa) * np.log(v_of_t[q] / v_of_t[q - 1]) + (1 / gamma) * np.log(1 + gamma / kappa)
assert isinstance(bid, (int, float, np.float32, np.float64)) is True
dict_ask[q - max_inventory_Q] = ask
dict_bid[q - max_inventory_Q] = bid
dict_bid[max_inventory_Q] = None
dict_ask[min_inventory_Q] = None
dict_ask[max_inventory_Q] = dict_bid[min_inventory_Q]
list_evolution_bid.append(list(dict_bid.values())[1:-2])
# Plotting
X = np.arange(1, T+1, 1)
Y = np.arange(min_inventory_Q+1, max_inventory_Q-1 , 1)
X, Y = np.meshgrid(X, Y)
Z = np.array(list_evolution_bid).T
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='hot', linewidth=0, antialiased=False)
ax.invert_xaxis()
plt.show()