-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathcoefficient_drifter.py
240 lines (205 loc) · 8.91 KB
/
coefficient_drifter.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
from collections import deque
from dataclasses import dataclass
from typing import Optional, Tuple, List
import numpy as np
from sklearn.utils import check_random_state
from obp.dataset.synthetic import sample_random_uniform_coefficients
@dataclass
class CoefficientDrifter:
"""Class for synthesizing bandit data.
Note
-----
By calling the `obtain_batch_bandit_feedback` method several times,
we can resample logged bandit data from the same data generating distribution.
This can be used to estimate confidence intervals of the performances of OPE estimators.
If None is given as `behavior_policy_function`, the behavior policy will be generated from the true expected reward function. See the description of the `beta` argument, which controls the behavior policy.
Parameters
-----------
drift_interval: int
Controls interval of steps at which the coefficients are updated.
transition_period: int, default=0
Controls the period in which the coefficients are transitioning between new and old. The transition period
always happened before the drift interval. Meaning, that if the drift interval is 5000 and the transition period
500, the transition will be between step 4500 and step 5000.
transition_type: str, default="linear"
The type of transition (linear or weighted_sampled) to be applied while transitioning between two sets of
coefficients.
seasonal: bool, default=False
When True, the coefficients will shift between two sets of coefficients representing a seasonal shift.
base_coefficient_weight: float, default=0.0
A floating point between 0.0 and 1.0 that represents a base coefficient weight that is consistent regardless of
any drift. This can be used to ensure the severity of the drift over time.
effective_dim_action_context: (optional) int, default=None
Specifies the dimensions of the action context coefficients.
effective_dim_context: (optional) int, default=None
Specifies the dimensions of the context coefficients.
random_state: int, default=12345
Controls the random seed
References
------------
Emanuele Cavenaghi, Gabriele Sottocornola, Fabio Stella, and Markus Zanker.
"Non stationary multi-armed bandit: Empirical evaluation of a new concept drift-aware algorithm.", 2021.
"""
drift_interval: int
transition_period: int = 0
transition_type: str = "linear" # linear or weighted_sampled
seasonal: bool = False
base_coefficient_weight: float = 0.0
effective_dim_action_context: Optional[int] = None
effective_dim_context: Optional[int] = None
random_state: int = 12345
played_rounds: int = 0
context_coefs: Optional[deque] = None
action_coefs: Optional[deque] = None
context_action_coefs: Optional[deque] = None
base_context_coef: Optional[np.ndarray] = None
base_action_coef: Optional[np.ndarray] = None
base_context_action_coef: Optional[np.ndarray] = None
def __post_init__(self) -> None:
if self.random_state is None:
raise ValueError("`random_state` must be given")
self.random_ = check_random_state(self.random_state)
self.available_rounds = self.drift_interval
self.context_coefs = deque(maxlen=2)
self.action_coefs = deque(maxlen=2)
self.context_action_coefs = deque(maxlen=2)
if self.effective_dim_action_context and self.effective_dim_context:
self.update_coef()
def update_coef(self) -> None:
if self.base_context_coef is None:
(
self.base_context_coef,
self.base_action_coef,
self.base_context_action_coef,
) = sample_random_uniform_coefficients(
self.effective_dim_action_context,
self.effective_dim_context,
self.random_,
)
if len(self.context_coefs) == 0:
self.context_coefs.append(self.base_context_coef)
self.action_coefs.append(self.base_action_coef)
self.context_action_coefs.append(self.base_context_action_coef)
if self.seasonal and len(self.context_coefs) == 2:
self.context_coefs.rotate()
self.action_coefs.rotate()
self.context_action_coefs.rotate()
else:
(
tmp_context_coef,
tmp_action_coef,
tmp_action_context_coef,
) = sample_random_uniform_coefficients(
self.effective_dim_action_context,
self.effective_dim_context,
self.random_,
)
self.context_coefs.append(tmp_context_coef)
self.action_coefs.append(tmp_action_coef)
self.context_action_coefs.append(tmp_action_context_coef)
def get_coefficients(
self,
n_rounds: int,
effective_dim_context: int = None,
effective_dim_action_context: int = None,
**kwargs,
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
if effective_dim_action_context and effective_dim_context:
eff_dim_not_set = (
not self.effective_dim_action_context and not self.effective_dim_context
)
eff_dim_equal = (
self.effective_dim_action_context == effective_dim_action_context
and self.effective_dim_context == effective_dim_context
)
if eff_dim_not_set or eff_dim_equal:
self.effective_dim_action_context = effective_dim_action_context
self.effective_dim_context = effective_dim_context
else:
raise RuntimeError("Trying to change the effective dimensions")
if len(self.context_coefs) == 0:
self.update_coef()
required_rounds = n_rounds
context_coefs = []
action_coefs = []
context_action_coefs = []
while required_rounds > 0:
if required_rounds >= self.available_rounds:
self.append_current_coefs(
context_coefs,
action_coefs,
context_action_coefs,
rounds=self.available_rounds,
)
required_rounds -= self.available_rounds
self.update_coef()
self.available_rounds = self.drift_interval
else:
self.append_current_coefs(
context_coefs,
action_coefs,
context_action_coefs,
rounds=required_rounds,
)
self.available_rounds -= required_rounds
required_rounds = 0
return (
np.vstack(context_coefs),
np.vstack(action_coefs),
np.vstack(context_action_coefs),
)
def append_current_coefs(
self,
context_coefs: List[np.ndarray],
action_coefs: List[np.ndarray],
context_action_coefs: List[np.ndarray],
rounds: int,
) -> None:
shift_start = self.available_rounds - self.transition_period
transition_steps = np.arange(start=1, stop=self.transition_period + 1)
if shift_start >= 0:
transition_steps = np.pad(transition_steps, pad_width=[(shift_start, 0)])
if shift_start < 0:
transition_steps = transition_steps[-shift_start:]
shift_remainder = self.available_rounds - rounds
if shift_remainder > 0:
transition_steps = transition_steps[shift_remainder:]
weights = transition_steps / (self.transition_period + 1)
if self.transition_type == "weighted_sampled":
weights = self.random_.binomial(n=1, p=weights)
context_coefs.append(
self.compute_weighted_coefs(
self.context_coefs, self.base_context_coef, rounds, weights
)
)
action_coefs.append(
self.compute_weighted_coefs(
self.action_coefs, self.base_action_coef, rounds, weights
)
)
context_action_coefs.append(
self.compute_weighted_coefs(
self.context_action_coefs,
self.base_context_action_coef,
rounds,
weights,
)
)
def compute_weighted_coefs(self, coefs, base_coef, rounds, weights):
base_coef = self.base_coefficient_weight * base_coef
A = np.tile(coefs[0], [rounds] + [1 for _ in coefs[0].shape])
B = np.tile(coefs[1], [rounds] + [1 for _ in coefs[1].shape])
coefs = (
base_coef
+ A
* np.expand_dims(
(1 - self.base_coefficient_weight) * (1 - weights),
list(range(1, len(A.shape))),
)
+ B
* np.expand_dims(
(1 - self.base_coefficient_weight) * weights,
list(range(1, len(B.shape))),
)
)
return coefs