-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbest_arm_algos.py
205 lines (173 loc) · 7.06 KB
/
best_arm_algos.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
import numpy as np
def action_elimination(bandit_means, bandit_scales, samples_per_epoch=1,
epsilon=0.01, delta=0.1, experiment=False, n_steps=0):
"""
Arguments:
- bandit_means: List of Gaussian random variable means
- bandit_scales : List of Gaussian random variable
standard deviations (same order)
- samples_per_epoch : number of times to sample each arm/epoch
- epsilon : parameter for LIL bound
- delta : parameter for LIL bound and confidence level
"""
arms_sampled = {}
n_iter = 0
n = len(bandit_means)
bandit_samples = {key: [] for key in range(n)}
bandit_estimates = [None]*n
converged = False
bandit_set = set(range(n))
num_epochs = 1
while not converged:
if experiment:
arms_sampled[n_iter] = []
for arm in bandit_set:
sample = np.random.normal(bandit_means[arm], bandit_scales[arm],
size=samples_per_epoch)
bandit_samples[arm].extend(sample)
bandit_estimates[arm] = np.mean(bandit_samples[arm])
if experiment:
arms_sampled[n_iter].append(arm)
bound = _compute_bound(t=samples_per_epoch*num_epochs,
eps=epsilon, delta=delta/n)
max_ref = np.max(bandit_estimates)
index_mask = set([])
for arm in bandit_set:
if bandit_estimates[arm]+bound <= max_ref - bound:
index_mask.add(arm)
bandit_set -= index_mask
if experiment:
converged = n_iter >= n_steps+n-1
elif len(bandit_set) == 1:
converged = True
n_iter += 1
num_epochs += 1
if experiment:
return arms_sampled
else:
return bandit_set.pop()
def ucb(bandit_means, bandit_scales, epsilon=0.01, delta=0.1, beta=1,
experiment=False, n_steps=0):
"""
Arguments:
- bandit_means: List of Gaussian random variable means
- bandit_scales : List of Gaussian random variable
standard deviations (same order)
- epsilon : parameter for LIL bound
- delta : parameter for LIL bound and confidence level
- beta : parameter for bound
"""
arms_sampled = {}
n_iter = 0
n = len(bandit_means)
m = ((2+beta)/beta)**2
alpha = m * (1 + (np.log(2*np.log(m*n/delta))/np.log(n/delta)))
bandit_samples = {key: [np.random.normal(bandit_means[key],
bandit_scales[key])] for key in range(n)}
bandit_estimates = [bandit_samples[key][0] for key in range(n)]
if experiment:
arms_sampled[n_iter] = []
arms_sampled[n_iter].extend(range(n))
n_iter += 1
bandit_bounds = [(1+beta)*_compute_bound(t=1,
eps=epsilon, delta=delta/n)]*n
converged = False
while not converged:
# Sample from current best arm
arm = np.argmax(np.array(bandit_bounds) + np.array(bandit_estimates))
if experiment:
arms_sampled[n_iter] = []
arms_sampled[n_iter].append(arm)
bandit_samples[arm].append(np.random.normal(
bandit_means[arm], bandit_scales[arm]))
# Recompute estimate for this arm
bandit_estimates[arm] = np.mean(bandit_samples[arm])
bandit_bounds[arm] = (1+beta)*_compute_bound(
t=len(bandit_samples[arm]),
eps=epsilon, delta=delta/n)
if experiment:
converged = n_iter >= n_steps+n-1
else:
converged = _check_convergence_ucb(alpha, bandit_samples, n)
n_iter += 1
if experiment:
return arms_sampled
else:
return np.argmax(bandit_estimates)
def lucb(bandit_means, bandit_scales, epsilon=0.01, delta=0.1,
experiment=False, n_steps=0):
"""
Arguments:
- bandit_means: List of Gaussian random variable means
- bandit_scales : List of Gaussian random variable
standard deviations (same order)
- epsilon : parameter for LIL bound
- delta : parameter for LIL bound and confidence level
"""
arms_sampled = {}
n_iter = 0
n = len(bandit_means)
bandit_samples = {key: [np.random.normal(bandit_means[key],
bandit_scales[key])] for key in range(n)}
bandit_estimates = [bandit_samples[key][0] for key in range(n)]
if experiment:
arms_sampled[n_iter] = []
arms_sampled[n_iter].extend(range(n))
n_iter += 1
bandit_bounds = [_compute_bound(t=1,
eps=epsilon, delta=delta/n)]*n
converged = False
while not converged:
h_t = np.argmax(bandit_estimates)
mask = np.ones(len(bandit_estimates))
mask[h_t] = -np.inf
l_t = np.argmax(np.array(bandit_estimates + np.array(bandit_bounds)) *
mask)
if experiment:
arms_sampled[n_iter] = []
arms_sampled[n_iter].append(h_t)
if not _check_convergence_lucb(h_t, l_t,
bandit_estimates,
bandit_bounds):
arms_sampled[n_iter].append(l_t)
# Sample arms
bandit_samples[h_t].append(np.random.normal(
bandit_means[h_t], bandit_scales[h_t]))
bandit_samples[l_t].append(np.random.normal(
bandit_means[l_t], bandit_scales[l_t]))
# Recompute estimate for arms
bandit_estimates[h_t] = np.mean(bandit_samples[h_t])
bandit_bounds[h_t] = _compute_bound(t=len(bandit_samples[h_t]),
eps=epsilon, delta=delta/n)
bandit_estimates[l_t] = np.mean(bandit_samples[l_t])
bandit_bounds[l_t] = _compute_bound(t=len(bandit_samples[l_t]),
eps=epsilon, delta=delta/n)
if experiment:
converged = n_iter >= n_steps+n-1
else:
converged = _check_convergence_lucb(h_t, l_t,
bandit_estimates,
bandit_bounds)
n_iter += 1
if experiment:
return arms_sampled
else:
return np.argmax(bandit_estimates)
def _compute_bound(t, eps, delta):
num = (1+eps)*np.log(np.log((1+eps)*t+2)/delta)
den = 2*t
return (1+np.sqrt(eps))*np.sqrt(num/den)
def _check_convergence_ucb(alpha, bandit_samples, n):
num_samples = [len(bandit_samples[arm]) for arm in range(n)]
m = np.max(num_samples)
if m > alpha*(np.sum(num_samples) - m):
return True
else:
return False
def _check_convergence_lucb(first_arm, second_arm, bandit_estimates,
bandit_bounds):
if (bandit_estimates[first_arm] - bandit_bounds[first_arm] >
bandit_estimates[second_arm] + bandit_bounds[second_arm]):
return True
else:
return False