-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvi_algo_parametric.py
139 lines (122 loc) · 3.89 KB
/
vi_algo_parametric.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
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from utils import deco_print
from utils import h2T
from utils import poisson_initialization_n
from utils import zipf_initialization_n
from utils import random_initialization_n
from utils import greedy_alpha
from utils import obj_fun
from utils import update_tables
from utils import sample
from model import Parametric_Model
from model import get_h_and_loss_from_model
from model import train_model
### define parameters
N = 20
V_max = 50
I = 30
alpha = 0.001
n_iter = 300 #1000 #100
n_simulation = 50
###
### NN parameters
input_dim = 2
num_layer = 1
hidden_size = 30
###
case = 1
deco_print('There are %d dark pools. ' %N)
if case in [1,2]:
if case == 1:
lams = np.array([10]*N, dtype=int)
rho = np.array([0.01]*N)
title = 'Case I'
else:
lams = np.array([5]*4+[10]*8+[20]*8, dtype=int)
rho = np.array([0.01]*8+[0.002]*8+[0.001]*4)
title = 'Case II'
### Case I & II
rv, T, h = poisson_initialization_n(N, lams, V_max)
deco_print('The maximum number of volume that can be executed in dark pools follow Poisson distribution with parameters ' + str(lams) + '. ')
elif case in [3,4]:
if case == 3:
hs = [np.array([0.1]*10+[0.2]*10+[0.15]*10)] * N
rho = np.array([0.01]*N)
title = 'Case III'
else:
hs = np.random.rand(N, 30) * 0.2
rho = np.random.rand(N) * 0.009 + 0.001
title = 'Case IV'
### Case III & IV
rv, T, h = random_initialization_n(N, hs, V_max)
deco_print('The maximum number of volume that can be executed in dark pools follow a distribution specified by h. ')
elif case in [5,6]:
if case == 5:
alphas = np.array([1.2]*N)
rho = np.array([0.002]*N)
title = 'Case V'
else:
alphas = np.array([1.2]*4+[1.5]*8+[2.0]*8)
rho = np.array([0.002]*8+[0.005]*8+[0.01]*4)
title = 'Case VI'
### Case V & VI
rv, T, h = zipf_initialization_n(N, alphas, V_max)
deco_print('The maximum number of volume that can be executed in dark pools follow Zipf\'s distribution with parameters ' + str(alphas) + '. ')
f, _ = obj_fun(rho, T, V_max)
v_opt, V_opt = greedy_alpha(N, rho, T, alpha)
deco_print('The discount factors are ' + str(rho) + '. ')
deco_print('Optimal V: %d' %V_opt)
deco_print('Optimal allocation: ' + str(v_opt))
### Output v
v_out = np.zeros((n_simulation, N))
###
models = dict()
for i in range(N):
deco_print('Creating model %d' %i, end='\r')
models[i] = Parametric_Model('nn%d'%i, V_max+1, input_dim=input_dim, num_layer=num_layer, hidden_size=hidden_size)
sess = tf.Session()
X_input = np.array([np.ones(V_max+1)]+[i*np.log(np.arange(V_max+1)+1) for i in range(1,input_dim)]).T
for k in range(n_simulation):
sess.run(tf.global_variables_initializer())
print('Iter #%d...'%k)
tables = [np.zeros((V_max+1,3), dtype=int) for _ in range(N)]
h_hat = np.zeros((N, V_max+1))
loss = np.zeros(N)
for t in range(n_iter):
for i in range(N):
h_hat[i], loss[i] = get_h_and_loss_from_model(models[i], X_input, tables[i], sess)
T_hat = h2T(h_hat)
v_t, V_t = greedy_alpha(N, rho, T_hat[:,:-1], alpha)
print(V_t)
for _ in range(I):
r_t = sample(N, rv, v_t)
update_tables(N, tables, r_t, v_t)
for i in range(N):
loss[i], _ = train_model(models[i], X_input, tables[i], sess, loss[i])
v_out[k] = v_t
print('\n')
ind = np.arange(N)
width = 0.35
fig, ax = plt.subplots()
rects1 = ax.bar(ind, v_opt, width, color='r')
rects2 = ax.bar(ind+width, np.mean(v_out,0), width, color='y', yerr=np.std(v_out,0))
ax.set_xlabel('Dark Pool')
ax.set_ylabel(r'Allocation $\hat{v}_i$')
ax.set_xticks(ind+width/2)
ax.set_xticklabels(tuple(str(i) for i in ind))
ax.legend((rects1[0], rects2[0]), ('Opt', 'Alg'), loc='best')
plt.title(title)
def autolabel(rects):
"""
Attach a text label above each bar displaying its height
"""
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
'%.0f' % float(height),
ha='center', va='bottom')
autolabel(rects1)
autolabel(rects2)
plt.show()