-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
286 lines (261 loc) · 11.6 KB
/
utils.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
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
import numpy as np
from datetime import datetime as date
import matplotlib.pyplot as plt
from matplotlib import colors as mapcol
########################################
# Plot Bandit #
########################################
def plot_bandit(bandit):
plt.figure(figsize=(12,7))
plt.xticks(range(0,bandit.nbr_arms), range(0,bandit.nbr_arms))
plt.plot(range(0,bandit.nbr_arms), bandit.rewards)
plt.scatter(range(0,bandit.nbr_arms), bandit.rewards)
plt.show()
########################################
# KL Divergences #
########################################
def klBernoulli(mean_1, mean_2, eps=1e-15):
"""Kullback-Leibler divergence for Bernoulli distributions."""
x = np.minimum(np.maximum(mean_1, eps), 1-eps)
y = np.minimum(np.maximum(mean_2, eps), 1-eps)
return x*np.log(x/y) + (1-x)*np.log((1-x)/(1-y))
def klGaussian(mean_1, mean_2, sig2=1.):
"""Kullback-Leibler divergence for Gaussian distributions."""
return ((mean_1-mean_2)**2)/(2*sig2)
########################################
# Argument selectors #
########################################
def randamax(V, T=None, I=None):
"""
V: array of values
T: array used to break ties
I: array of indices from which we should return an amax
"""
if I is None:
idxs = np.where(V == np.amax(V))[0]
if T is None:
idx = np.random.choice(idxs)
else:
assert len(V) == len(T), f"Lengths should match: len(V)={len(V)} - len(T)={len(T)}"
t_idxs = np.where(T[idxs] == np.amin(T[idxs]))[0]
t_idxs = np.random.choice(t_idxs)
idx = idxs[t_idxs]
else:
idxs = np.where(V[I] == np.amax(V[I]))[0]
if T is None:
idx = I[np.random.choice(idxs)]
else:
assert len(V) == len(T), f"Lengths should match: len(V)={len(V)} - len(T)={len(T)}"
t = T[I]
t_idxs = np.where(t[idxs] == np.amin(t[idxs]))[0]
t_idxs = np.random.choice(t_idxs)
idx = I[idxs[t_idxs]]
return idx
def randamin(V, T=None, I=None):
"""
V: array of values
T: array used to break ties
I: array of indices from which we should return an amax
"""
if I is None:
idxs = np.where(V == np.amin(V))[0]
if T is None:
idx = np.random.choice(idxs)
else:
assert len(V) == len(T), f"Lengths should match: len(V)={len(V)} - len(T)={len(T)}"
t_idxs = np.where(T[idxs] == np.amin(T[idxs]))[0]
t_idxs = np.random.choice(t_idxs)
idx = idxs[t_idxs]
else:
idxs = np.where(V[I] == np.amin(V[I]))[0]
if T is None:
idx = I[np.random.choice(idxs)]
else:
assert len(V) == len(T), f"Lengths should match: len(V)={len(V)} - len(T)={len(T)}"
t = T[I]
t_idxs = np.where(t[idxs] == np.amin(t[idxs]))[0]
t_idxs = np.random.choice(t_idxs)
idx = I[idxs[t_idxs]]
return idx
########################################
# Experiments #
########################################
class Experiment:
def __init__(self, sequential_algorithms, bandit,
statistics={'mean':True, 'std':True, 'quantile':False, 'pulls':False},
quantile_ticks=[0.1, 0.9, 0.5],
kullback=None, complexity=False):
assert len(sequential_algorithms) > 0
self.algorithms = sequential_algorithms
self.bandit = bandit
self.nbr_algo = len(sequential_algorithms)
self.algo_idx = 0
self.statistics = {}
self.regret = None
self.path = None
self.horizon = None
self.nbr_exp = None
self.complexity = None
if complexity:
if kullback is not None:
self.complexity = bandit.complexity(kullback)
else:
self.complexity = bandit.complexity()
for algo_idx, algo in enumerate(self.algorithms):
self.statistics[algo_idx] = {'name': algo.name}
for s in statistics.items():
if s[1]:
self.statistics[algo_idx][s[0]] = None
stats = self.statistics[0].keys()
self.stats = stats
self.regret_flag = ('mean' in stats) or ('std' in stats) or ('quantile' in stats)
self.quantile_ticks = quantile_ticks
self.path_flag = 'pulls' in stats
def __call__(self, arm, r, t):
if self.regret_flag:
self.regret[t] = self.bandit.regrets[arm]
if self.path_flag:
self.path[t] = arm
def run(self, nbr_exp=500, horizon=50):
self.nbr_exp = nbr_exp
self.horizon = horizon
for algo_idx, algo in enumerate(self.algorithms):
if self.regret_flag:
regret = np.zeros((nbr_exp, horizon))
if self.path_flag:
path = np.zeros((nbr_exp, horizon), int)
for i in range(nbr_exp):
if self.regret_flag:
self.regret = regret[i]
if self.path_flag:
self.path = path[i]
algo.fit(horizon, experiment=self)
if self.regret_flag:
regret[i] = np.cumsum(self.regret)
if self.path_flag:
path[i] = self.path
for k in self.stats:
if k == 'mean':
self.statistics[algo_idx][k] = np.mean(regret, 0)
elif k == 'std':
self.statistics[algo_idx][k] = np.std(regret, 0)
elif k == 'pulls':
self.statistics[algo_idx][k] = path
elif k == 'quantile':
self.statistics[algo_idx][k] = np.quantile(regret, q=self.quantile_ticks, axis=0)
def plot(self, save=False, complexity=True, dimensions=[(0,1)], functions=[]):
ticks = np.arange(self.horizon)
exp_info = f"Horizon = {self.horizon} - Nbr. of experiments = {self.nbr_exp}"
if 'mean' in self.stats:
plt.figure(figsize=(12,6))
if 'std' in self.stats:
title = "Mean cumulative regret and standard deviation tube\n"
title += exp_info
plt.title(title)
for algo_stats in self.statistics.values():
name = algo_stats['name']
mean = algo_stats['mean']
std = algo_stats['std']
plt.plot(ticks, mean, label = name + f" - R = {mean[-1]:.2f}")
plt.fill_between(ticks, np.maximum(0, mean-std), mean+std, alpha=0.3)
else:
title = "Mean cumulative regret\n"
title += exp_info
plt.title(title)
for algo_stats in self.statistics.values():
name = algo_stats['name']
mean = algo_stats['mean']
plt.plot(ticks, mean, label = name + f" - R = {mean[-1]:.2f}")
if complexity and (self.complexity is not None):
plt.plot(ticks, self.complexity*np.log(ticks+1), label="Regret lower Bound")
if len(functions) > 0:
for f, n in functions:
plt.plot(ticks, f(ticks), label=n)
plt.legend()
plt.show()
if save:
plt.savefig(f"./images/exp_mean_std_{date.now().strftime('%d_%m_%Y_%H_%M_%S')}",
bbox_inches='tight')
plt.close()
elif 'std' in self.stats:
plt.figure(figsize=(12,6))
title = "Standard deviation of the regret\n"
title += exp_info
plt.title(title)
for algo_stats in self.statistics.values():
name = algo_stats['name']
std = algo_stats['std']
plt.plot(ticks, std, label = name + f" - R = {std[-1]:.2f}")
if len(functions) > 0:
for f, n in functions:
plt.plot(ticks, f(ticks), label=n)
plt.legend()
plt.show()
if save:
plt.savefig(f"./images/exp_std_{date.now().strftime('%d_%m_%Y_%H_%M_%S')}",
bbox_inches='tight')
plt.close()
if 'quantile' in self.stats:
plt.figure(figsize=(12,6))
title = f"Median cumulative regret and quantile {self.quantile_ticks[:-1]} tube\n"
title += exp_info
plt.title(title)
for algo_stats in self.statistics.values():
name = algo_stats['name']
quantile = algo_stats['quantile']
# By convention, the last quantile is the median
plt.plot(ticks, quantile[-1], label = name + f" - R = {quantile[-1][-1]:.2f}")
nbr_quantile = len(quantile)
ptr_1 = 0
ptr_2 = nbr_quantile-2
while ptr_1 < ptr_2:
plt.fill_between(ticks, quantile[ptr_1], quantile[ptr_2], alpha=0.3)
ptr_1 += 1
ptr_2 -= 1
if complexity and (self.complexity is not None):
plt.plot(ticks, self.complexity*np.log(ticks+1), label="Regret lower Bound")
if len(functions) > 0:
for f, n in functions:
plt.plot(ticks, f(ticks), label=n)
plt.legend()
plt.show()
if save:
plt.savefig(f"./images/exp_quantile_{date.now().strftime('%d_%m_%Y_%H_%M_%S')}",
bbox_inches='tight')
plt.close()
if 'pulls' in self.stats:
# basis = np.eye(self.bandit.nbr_arms, dtype=int)
basis = np.eye(2, dtype=int)
colors = list(mapcol.TABLEAU_COLORS.values())
len_colors = len(colors)
for dims in dimensions:
plt.figure(figsize=(10,10))
title = f"Sampling strategy as a random walk - abs.: arm {dims[0]}, ord.: arm {dims[1]}\n"
title += exp_info
plt.title(title)
color_ctr = 0
alpha = (self.bandit.nbr_arms+0.1*np.log(float(self.horizon)))/self.nbr_exp
for algo_stats in self.statistics.values():
name = algo_stats['name']
paths = algo_stats['pulls']
for i, p in enumerate(paths):
path = np.zeros((2, self.horizon+1), int)
for t, arm in enumerate(p):
if arm == dims[0]:
path[:,t+1] = path[:,t] + basis[0]
elif arm == dims[1]:
path[:,t+1] = path[:,t] + basis[1]
if i == 0:
plt.plot(path[0], path[1], alpha=alpha, color=colors[color_ctr], label=f"{name}")
else:
plt.plot(path[0], path[1], alpha=alpha, color=colors[color_ctr])
color_ctr += 1
color_ctr = color_ctr % len_colors
leg = plt.legend()
for l in leg.get_lines():
l.set_alpha(1)
plt.show()
if save:
plt.savefig(f"./images/rw_dim_{dims[0]}_{dims[1]}_{date.now().strftime('%d_%m_%Y_%H_%M_%S')}",
bbox_inches='tight')
plt.close()