-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathV_vs_hidden_size_algo_parametric.py
124 lines (107 loc) · 3.77 KB
/
V_vs_hidden_size_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
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
###
### NN parameters
input_dim = 2
num_layer = 1
hidden_size_list = [5, 10, 20, 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))
plt.figure('Figure 1')
plt.axhline(y=V_opt, xmin=0, xmax=V_max, color='black')
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
V_est = np.zeros((len(hidden_size_list), n_iter), dtype=int)
for k in range(len(hidden_size_list)):
hidden_size = hidden_size_list[k]
tables = [np.zeros((V_max+1,3), dtype=int) for _ in range(N)]
models = dict()
for i in range(N):
deco_print('Creating model %d' %i, end='\r')
models[i] = Parametric_Model('nn_%d_%d'%(k,i), V_max+1, input_dim=input_dim, num_layer=num_layer, hidden_size=hidden_size)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
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)
V_est[k,t] = 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])
print('\n')
plt.plot(np.arange(n_iter), V_est[0], color='b', label='hidden size %d'%hidden_size_list[0])
plt.plot(np.arange(n_iter), V_est[1], color='r', label='hidden size %d'%hidden_size_list[1])
plt.plot(np.arange(n_iter), V_est[2], color='y', label='hidden size %d'%hidden_size_list[2])
plt.plot(np.arange(n_iter), V_est[3], color='m', label='hidden size %d'%hidden_size_list[3])
plt.xlabel('t')
plt.ylabel(r'Volume $\hat{V}^t$')
plt.legend()
plt.title(title)
plt.show()