-
Notifications
You must be signed in to change notification settings - Fork 87
/
test_dr_estimators_continuous.py
369 lines (339 loc) · 10.7 KB
/
test_dr_estimators_continuous.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import pytest
import numpy as np
from obp.ope import KernelizedDoublyRobust
from obp.dataset import (
SyntheticContinuousBanditDataset,
linear_reward_funcion_continuous,
linear_behavior_policy_continuous,
linear_synthetic_policy_continuous,
)
def test_synthetic_init():
# kernel
with pytest.raises(ValueError):
KernelizedDoublyRobust(kernel="a", bandwidth=0.1)
with pytest.raises(ValueError):
KernelizedDoublyRobust(kernel=None, bandwidth=0.1)
# bandwidth
with pytest.raises(TypeError):
KernelizedDoublyRobust(kernel="gaussian", bandwidth="a")
with pytest.raises(TypeError):
KernelizedDoublyRobust(kernel="gaussian", bandwidth=None)
with pytest.raises(ValueError):
KernelizedDoublyRobust(kernel="gaussian", bandwidth=-1.0)
# prepare dr instances
dr = KernelizedDoublyRobust(kernel="cosine", bandwidth=0.1)
# --- invalid inputs (all kernelized estimators) ---
# action_by_evaluation_policy, estimated_rewards_by_reg_model, action_by_behavior_policy, reward, pscore, description
invalid_input_of_dr = [
(
None, #
np.ones(5),
np.ones(5),
np.ones(5),
np.random.uniform(size=5),
"action_by_evaluation_policy must be 1-dimensional ndarray",
),
(
np.ones((5, 1)), #
np.ones(5),
np.ones(5),
np.ones(5),
np.random.uniform(size=5),
"action_by_evaluation_policy must be 1-dimensional ndarray",
),
(
np.ones(5),
None, #
np.ones(5),
np.ones(5),
np.random.uniform(size=5),
"estimated_rewards_by_reg_model must be ndarray",
),
(
np.ones(5),
np.ones((5, 1)), #
np.ones(5),
np.ones(5),
np.random.uniform(size=5),
"estimated_rewards_by_reg_model must be 1-dimensional ndarray",
),
(
np.ones(5), #
np.ones(4), #
np.ones(5),
np.ones(5),
np.random.uniform(size=5),
"estimated_rewards_by_reg_model and action_by_evaluation_policy must be the same size",
),
(
np.ones(5),
np.ones(5),
None, #
np.ones(5),
np.random.uniform(size=5),
"action_by_behavior_policy must be ndarray",
),
(
np.ones(5),
np.ones(5),
np.ones((5, 1)), #
np.ones(5),
np.random.uniform(size=5),
"action_by_behavior_policy must be 1-dimensional ndarray",
),
(
np.ones(5),
np.ones(5),
np.ones(5),
None, #
np.random.uniform(size=5),
"reward must be ndarray",
),
(
np.ones(5),
np.ones(5),
np.ones(5),
np.ones((5, 1)), #
np.random.uniform(size=5),
"reward must be 1-dimensional ndarray",
),
(
np.ones(5),
np.ones(5),
np.ones(4), #
np.ones(3), #
np.random.uniform(size=5),
"action_by_behavior_policy and reward must be the same size",
),
(
np.ones(5), #
np.ones(5),
np.ones(4), #
np.ones(4),
np.random.uniform(size=5),
"action_by_behavior_policy and action_by_evaluation_policy must be the same size",
),
(
np.ones(5),
np.ones(5),
np.ones(5),
np.ones(5),
None, #
"pscore must be ndarray",
),
(
np.ones(5),
np.ones(5),
np.ones(5),
np.ones(5),
np.random.uniform(size=(5, 1)), #
"pscore must be 1-dimensional ndarray",
),
(
np.ones(5),
np.ones(5),
np.ones(5),
np.ones(5),
np.random.uniform(size=4), #
"action_by_behavior_policy, reward, and pscore must be the same size",
),
(
np.ones(5),
np.ones(5),
np.ones(5),
np.ones(5),
np.arange(5), #
"pscore must be positive",
),
]
@pytest.mark.parametrize(
"action_by_evaluation_policy, estimated_rewards_by_reg_model, action_by_behavior_policy, reward, pscore, description",
invalid_input_of_dr,
)
def test_dr_continuous_using_invalid_input_data(
action_by_evaluation_policy,
estimated_rewards_by_reg_model,
action_by_behavior_policy,
reward,
pscore,
description: str,
) -> None:
with pytest.raises(ValueError, match=f"{description}*"):
_ = dr.estimate_policy_value(
action_by_evaluation_policy=action_by_evaluation_policy,
estimated_rewards_by_reg_model=estimated_rewards_by_reg_model,
action_by_behavior_policy=action_by_behavior_policy,
reward=reward,
pscore=pscore,
)
with pytest.raises(ValueError, match=f"{description}*"):
_ = dr.estimate_interval(
action_by_evaluation_policy=action_by_evaluation_policy,
estimated_rewards_by_reg_model=estimated_rewards_by_reg_model,
action_by_behavior_policy=action_by_behavior_policy,
reward=reward,
pscore=pscore,
)
# --- valid inputs (all kernelized estimators) ---
# action_by_evaluation_policy, estimated_rewards_by_reg_model, action_by_behavior_policy, reward, pscore
valid_input_of_dr = [
(
np.ones(5),
np.ones(5),
np.ones(5),
np.ones(5),
np.random.uniform(size=5),
),
]
@pytest.mark.parametrize(
"action_by_evaluation_policy, estimated_rewards_by_reg_model, action_by_behavior_policy, reward, pscore",
valid_input_of_dr,
)
def test_dr_continuous_using_valid_input_data(
action_by_evaluation_policy,
estimated_rewards_by_reg_model,
action_by_behavior_policy,
reward,
pscore,
) -> None:
_ = dr.estimate_policy_value(
action_by_evaluation_policy=action_by_evaluation_policy,
estimated_rewards_by_reg_model=estimated_rewards_by_reg_model,
action_by_behavior_policy=action_by_behavior_policy,
reward=reward,
pscore=pscore,
)
# --- confidence intervals ---
# alpha, n_bootstrap_samples, random_state, description
invalid_input_of_estimate_intervals = [
(0.05, 100, "s", "random_state must be an integer"),
(0.05, -1, 1, "n_bootstrap_samples must be a positive integer"),
(0.05, "s", 1, "n_bootstrap_samples must be a positive integer"),
(0.0, 1, 1, "alpha must be a positive float (< 1)"),
(1.0, 1, 1, "alpha must be a positive float (< 1)"),
("0", 1, 1, "alpha must be a positive float (< 1)"),
]
valid_input_of_estimate_intervals = [
(0.05, 100, 1, "random_state is 1"),
(0.05, 1, 1, "n_bootstrap_samples is 1"),
]
@pytest.mark.parametrize(
"action_by_evaluation_policy, estimated_rewards_by_reg_model, action_by_behavior_policy, reward, pscore",
valid_input_of_dr,
)
@pytest.mark.parametrize(
"alpha, n_bootstrap_samples, random_state, description",
invalid_input_of_estimate_intervals,
)
def test_estimate_intervals_of_all_estimators_using_invalid_input_data(
action_by_evaluation_policy,
estimated_rewards_by_reg_model,
action_by_behavior_policy,
reward,
pscore,
alpha,
n_bootstrap_samples,
random_state,
description,
) -> None:
with pytest.raises(ValueError, match=f"{description}*"):
_ = dr.estimate_interval(
action_by_evaluation_policy=action_by_evaluation_policy,
estimated_rewards_by_reg_model=estimated_rewards_by_reg_model,
action_by_behavior_policy=action_by_behavior_policy,
reward=reward,
pscore=pscore,
alpha=alpha,
n_bootstrap_samples=n_bootstrap_samples,
random_state=random_state,
)
@pytest.mark.parametrize(
"action_by_evaluation_policy, estimated_rewards_by_reg_model, action_by_behavior_policy, reward, pscore",
valid_input_of_dr,
)
@pytest.mark.parametrize(
"alpha, n_bootstrap_samples, random_state, description",
valid_input_of_estimate_intervals,
)
def test_estimate_intervals_of_all_estimators_using_valid_input_data(
action_by_evaluation_policy,
estimated_rewards_by_reg_model,
action_by_behavior_policy,
reward,
pscore,
alpha,
n_bootstrap_samples,
random_state,
description,
) -> None:
_ = dr.estimate_interval(
action_by_evaluation_policy=action_by_evaluation_policy,
estimated_rewards_by_reg_model=estimated_rewards_by_reg_model,
action_by_behavior_policy=action_by_behavior_policy,
reward=reward,
pscore=pscore,
alpha=alpha,
n_bootstrap_samples=n_bootstrap_samples,
random_state=random_state,
)
@pytest.mark.parametrize(
"kernel",
["triangular", "gaussian", "epanechnikov", "cosine"],
)
def test_continuous_ope_performance(kernel):
# define dr instances
dr = KernelizedDoublyRobust(kernel=kernel, bandwidth=0.1)
# set parameters
dim_context = 2
reward_noise = 0.1
random_state = 12345
n_rounds = 10000
min_action_value = -10
max_action_value = 10
behavior_policy_function = linear_behavior_policy_continuous
reward_function = linear_reward_funcion_continuous
dataset = SyntheticContinuousBanditDataset(
dim_context=dim_context,
reward_noise=reward_noise,
min_action_value=min_action_value,
max_action_value=max_action_value,
reward_function=reward_function,
behavior_policy_function=behavior_policy_function,
random_state=random_state,
)
# obtain feedback
bandit_feedback = dataset.obtain_batch_bandit_feedback(
n_rounds=n_rounds,
)
context = bandit_feedback["context"]
action_by_evaluation_policy = linear_synthetic_policy_continuous(context)
action_by_behavior_policy = bandit_feedback["action"]
reward = bandit_feedback["reward"]
pscore = bandit_feedback["pscore"]
# compute statistics of ground truth policy value
q_pi_e = linear_reward_funcion_continuous(
context=context, action=action_by_evaluation_policy, random_state=random_state
)
true_policy_value = q_pi_e.mean()
print(f"true_policy_value: {true_policy_value}")
# OPE
policy_value_estimated_by_dr = dr.estimate_policy_value(
action_by_evaluation_policy=action_by_evaluation_policy,
estimated_rewards_by_reg_model=q_pi_e,
action_by_behavior_policy=action_by_behavior_policy,
reward=reward,
pscore=pscore,
)
# check the performance of OPE
estimated_policy_value = {
"dr": policy_value_estimated_by_dr,
}
for key in estimated_policy_value:
print(
f"estimated_value: {estimated_policy_value[key]} ------ estimator: {key}, "
)
# test the performance of each estimator
assert (
np.abs(true_policy_value - estimated_policy_value[key]) / true_policy_value
<= 0.1
), f"{key} does not work well (relative estimation error is greater than 10%)"