-
Notifications
You must be signed in to change notification settings - Fork 0
/
ALBA_YELIM.py
188 lines (131 loc) · 6.68 KB
/
ALBA_YELIM.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
import numpy as np
import itertools
import logging
import time
class ALBA_YELIM(object):
def __init__(self, X, theta_star, delta):
self.X = X
self.K = len(X)
self.d = X.shape[1]
self.theta_star = theta_star
self.opt_arm = np.argmax(X@theta_star)
self.delta = delta
def algorithm(self, seed, binary=False):
self.seed = seed
np.random.seed(self.seed)
self.active_arms = list(range(self.K))
self.arm_counts = np.zeros(self.K)
self.N = 0
r = 0
self.norm_bound = np.linalg.norm(self.theta_star)
A = np.zeros((self.d, self.d))
b = np.zeros((self.d, 1))
while len(self.active_arms) > 1:
delta_r = 6*self.delta/(np.pi**2*(r+1)**2)
p = np.floor(self.d/(2**r))
s = 1
num_round_active = len(self.active_arms)
while len(self.active_arms) > p:
delta_s = 6*delta_r/(np.pi**2*s**2)
self.e_s = 1/(2**s)
c_0 = max(4*self.norm_bound**2, 3)
l_s = 4*c_0*(2+(6 + 4/1.1**s)*self.d)*(1.1**s/4)**2
num_samples = np.ceil(l_s*np.log(5*num_round_active**2/(2*delta_s))).astype(int)
design = self.optimal_allocation()
allocation = np.random.choice(self.K, num_samples, True, p=design).tolist()
allocation = np.array([allocation.count(i) for i in range(self.K)])
self.M = self.X.T@np.diag(design)@self.X
self.M_inv = np.linalg.pinv(self.M)
pulls = np.vstack([np.tile(self.X[i], (num, 1)) for i, num in enumerate(allocation) if num > 0])
if not binary:
rewards = pulls@self.theta_star + np.random.randn(num_samples, 1)
else:
rewards = np.random.binomial(1, pulls@self.theta_star, (num_samples, 1))
A = num_samples*self.M
b = pulls.T@rewards
self.theta_hat = np.linalg.pinv(A)@b
self.drop_arms(l_s, design)
s += 1
self.arm_counts += allocation
self.N += num_samples
logging.info('\n\n')
logging.info('arm counts %s' % str(self.arm_counts))
logging.info('round sample count %s' % str(num_samples))
logging.info('total sample count %s' % str(self.N))
logging.info('active arms %s' % str(self.active_arms))
logging.info('\n\n')
r += 1
logging.info('\n\n')
logging.info('finished elim phase %s' % str(r-1))
logging.info('arm counts %s' % str(self.arm_counts))
logging.info('round sample count %s' % str(num_samples))
logging.info('total sample count %s' % str(self.N))
logging.info('active arms %s' % str(self.active_arms))
logging.info('\n\n')
del self.M_inv
del self.M
self.success = (self.opt_arm in self.active_arms)
logging.critical('Succeeded? %s' % str(self.success))
logging.critical('Sample complexity %s' % str(self.N))
def optimal_allocation(self):
span_arms = self.active_arms.copy()
rank = np.linalg.matrix_rank(self.X[self.active_arms])
for arm_idx in range(self.K):
if arm_idx in span_arms:
continue
else:
if np.linalg.matrix_rank(self.X[self.active_arms + [arm_idx]]) == rank:
span_arms.append(arm_idx)
span_arms = sorted(span_arms)
design = np.ones(len(span_arms))
design /= design.sum()
Xhat = self.X[span_arms]
max_iter = 5000
for count in range(1, max_iter):
A_inv = np.linalg.pinv(Xhat.T@np.diag(design)@Xhat)
U,D,V = np.linalg.svd(A_inv)
Ainvhalf = U@np.diag(np.sqrt(D))@V.T
newX = (Xhat@Ainvhalf)**2
rho = newX@np.ones((newX.shape[1], 1))
idx = np.argmax(rho)
x = Xhat[idx, :, None]
g = ((Xhat@A_inv@x)*(Xhat@A_inv@x)).flatten().tolist()
g_idx = np.argmax(g)
gamma = 2/(count+2)
design_update = -gamma*design
design_update[g_idx] += gamma
relative = np.linalg.norm(design_update)/(np.linalg.norm(design))
design += design_update
if np.abs(np.max(rho) - min(self.d, len(self.active_arms))) < 0.01:
break
design_ = np.zeros(self.K)
design_[span_arms] = design
idx_fix = np.where(design_ < 1e-5)[0]
drop_total = design_[idx_fix].sum()
design_[idx_fix] = 0
design_[np.argmax(design_)] += drop_total
return design_
def drop_arms(self, l, design):
active_arms = self.active_arms.copy()
max_arm_idx = np.argmax(self.X[active_arms]@self.theta_hat)
max_arm_idx = active_arms[max_arm_idx]
max_arm = self.X[max_arm_idx, :, None]
for arm_idx in active_arms:
if arm_idx == max_arm_idx:
continue
arm = self.X[arm_idx, :, None]
y = max_arm - arm
if self.conf(y, l, design) < y.T@self.theta_hat:
self.active_arms.remove(arm_idx)
def conf(self, y, l, design):
L = self.norm_bound
y_var = np.sqrt(y.T@self.M_inv@y)
y_norm = np.linalg.norm(y)
err = (np.sqrt(2)*L*y_var)/np.sqrt(l) \
+ (2*L*(y_norm + y_var*np.sqrt(self.d)))/(3*l) \
+ np.sqrt(2)*np.sqrt((y_var**2/l)*np.sqrt(3*self.d/l) + (y_var**2/l))
expectation = (design.reshape(-1, 1)*((y.T@self.M_inv@self.X.T).T*(np.abs(self.X@self.theta_hat) + self.e_s/2))**2).sum()
ERR = np.sqrt(2*expectation/l) \
+ (2*(np.abs(y.T@self.theta_hat) + self.e_s/2) + L*y_var*np.sqrt(self.d))/(3*l) \
+ np.sqrt(2*((y_var**2/l)*np.sqrt(3*self.d/l) + (y_var**2/l)))
return min(float(err), float(ERR))