Bandits
rlbook.bandits.algorithms.Bandit
Base Bandit class
Attributes:
Name | Type | Description |
---|---|---|
testbed |
TestBed class object
|
Testbed object that returns a Reward value for a given Action |
columns |
list of strings
|
List of numpy column names to use when outputting results as a pandas dataframe. |
action_values |
numpy array
|
Stores results of the actions values method. Contains Run, Step, Action, and Reward Initialized as None, and created with the run method. |
n |
int
|
Current step in a run |
Q_init |
numpy array
|
Numpy array of initial Q values with size n matching n actions available in testbed |
Q |
numpy array
|
Numpy array of Q values with size n matching n actions available in testbed |
Qn |
int
|
Length of Q array |
Na |
numpy array
|
Numpy array with count of how many times an action has been chosen |
At |
int
|
Action that corresponds to the index of the selected testbed arm |
random_argmax |
bool
|
Boolean configuring whether to use argmax implementation that will choose randomly between tied Q values for tiebreakers rather than first occurence. Defaults to false. |
Source code in src/rlbook/bandits/algorithms.py
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 |
|
output_av()
Output action_values numpy array reshaped from 3D to 2D and columns names
Source code in src/rlbook/bandits/algorithms.py
150 151 152 153 154 155 156 |
|
rargmax(a)
Argmax implementation that chooses randomly between multiple tied max values rather than first occurence
Source code in src/rlbook/bandits/algorithms.py
76 77 78 79 80 |
|
run(testbed, steps, n_runs=1, n_jobs=4, serial=False)
Run bandit for specified number of steps and optionally multiple runs
Parameters:
Name | Type | Description | Default |
---|---|---|---|
testbed
|
Testbed class object providing a reward distribution |
required | |
steps
|
int
|
Number of steps in a single run |
required |
n_runs
|
int
|
Number of indepedent runs |
1
|
n_jobs
|
int
|
Number of process pools for executing runs in parallel |
4
|
serial
|
bool
|
Disables parallel runs if set to True |
False
|
Source code in src/rlbook/bandits/algorithms.py
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 |
|
select_action(testbed)
abstractmethod
Select action logic
Source code in src/rlbook/bandits/algorithms.py
71 72 73 74 |
|
rlbook.bandits.algorithms.EpsilonGreedy
Bases: Bandit
Epsilon greedy bandit Choose the 'greedy' option that maximizes reward but 'explore' a random action for a certain percentage of steps according to the epsilon value
Attributes:
Name | Type | Description |
---|---|---|
epsilon |
float
|
epsilon coefficient configuring the probability to explore non-optimal actions, ranging from 0.0 to 1.0 |
alpha |
float or sample_average
|
Constant step size ranging from 0.0 to 1.0, resulting in Q being the weighted average of past rewards and initial estimate of Q Note on varying step sizes such as using 1/n "sample_average": self.Q[self.At] = self.Q[self.At] + 1/self.Na[self.At]*(R-self.Q[self.At]) Theoretically guaranteed to converge, however in practice, slow to converge compared to constant alpha |
Source code in src/rlbook/bandits/algorithms.py
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 |
|
output_av()
Output action_values numpy array reshaped from 3D to 2D and columns names
Source code in src/rlbook/bandits/algorithms.py
214 215 216 217 218 219 220 221 |
|
select_action(testbed)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
testbed
|
Testbed class object providing a reward distribution |
required |
Source code in src/rlbook/bandits/algorithms.py
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 |
|
rlbook.bandits.algorithms.UCB
Bases: Bandit
Upper Confidence Bound bandit Estimate an upper bound for a given action that includes a measure of uncertainty based on how often the action has been chosen in the past
At = argmax( Qt(a) + c * sqrt(ln(t)/Nt(a)))
Sqrt term is a measure of variance of an action's Upper Bound The more often an action is selected, the uncertainty decreases (denominator increases) When another action is selected, the uncertainty increases (the numerator since time increase, but in smaller increments due to the ln)
Attributes:
Name | Type | Description |
---|---|---|
c |
float
|
c > 0 controls the degree of exploration, specifically the confidence level of a UCB for a given action |
U |
dict
|
Action-value uncertainty estimate in format {action: uncertainty (float), ...} |
alpha |
float or sample_average
|
Constant step size ranging from 0.0 to 1.0, resulting in Q being the weighted average of past rewards and initial estimate of Q Note on varying step sizes such as using 1/n "sample_average": self.Q[self.At] = self.Q[self.At] + 1/self.Na[self.At]*(R-self.Q[self.At]) Theoretically guaranteed to converge, however in practice, slow to converge compared to constant alpha |
Source code in src/rlbook/bandits/algorithms.py
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 |
|
__init__(Q_init, c=0.1, alpha=0.1)
Source code in src/rlbook/bandits/algorithms.py
250 251 252 253 254 255 256 257 |
|
output_av()
Output action_values numpy array reshaped from 3D to 2D and columns names
Source code in src/rlbook/bandits/algorithms.py
298 299 300 301 302 303 304 305 |
|
select_action(testbed)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
testbed
|
Testbed class object providing a reward distribution |
required |
Source code in src/rlbook/bandits/algorithms.py
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 |
|
rlbook.bandits.algorithms.Gradient
Bases: Bandit
Gradient bandit Learn a set of numerical preferences "H" rather than estimate a set of action values "Q" H preferences are all relative to each other, no correlation to a potential reward
Update H using: Ht+1(At) = Ht(At) + lr * (Rt - Q[At]) * (1 - softmax(At)) for At Ht+1(a) = Ht(a) + lr * (Rt - Q[At]) * softmax(a) for all a != At where At is action chosen
Attributes:
Name | Type | Description |
---|---|---|
H |
dict
|
Action-value uncertainty estimate in format {action: uncertainty (float), ...} |
lr |
float between 0.0-1.0
|
learning rate, step size to update H |
alpha |
float or sample_average
|
Constant step size ranging from 0.0 to 1.0, resulting in Q being the weighted average of past rewards and initial estimate of Q Note on varying step sizes such as using 1/n "sample_average": self.Q[self.At] = self.Q[self.At] + 1/self.Na[self.At]*(R-self.Q[self.At]) Theoretically guaranteed to converge, however in practice, slow to converge compared to constant alpha |
disable_baseline |
bool
|
Disable rewards baseline when calculating H, note that Q[At] is substituted for Pi. |
Source code in src/rlbook/bandits/algorithms.py
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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
|
__init__(Q_init, lr=0.1, alpha=0.1, disable_baseline=False)
Source code in src/rlbook/bandits/algorithms.py
334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
|
output_av()
Output action_values numpy array reshaped from 3D to 2D and columns names
Source code in src/rlbook/bandits/algorithms.py
404 405 406 407 408 409 410 411 |
|
select_action(testbed)
Select At based on H prob
Then update H via: Ht+1(At) = Ht(At) + lr * (Rt - Q[At]) * (1 - softmax(At)) for At Ht+1(a) = Ht(a) + lr * (Rt - Q[At]) * softmax(a) for all a != At where At is action chosen
Parameters:
Name | Type | Description | Default |
---|---|---|---|
testbed
|
Testbed class object providing a reward distribution |
required |
Source code in src/rlbook/bandits/algorithms.py
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
|
rlbook.bandits.testbeds.Testbed
Base Testbed class
Attributes:
Name | Type | Description |
---|---|---|
expected_values |
dict
|
Dict of parameters describing the Testbed distribution |
Source code in src/rlbook/bandits/testbeds.py
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 |
|
action_value(action, shape=None)
abstractmethod
Return reward value given action
Source code in src/rlbook/bandits/testbeds.py
40 41 42 43 |
|
best_action()
Return true best action that should have been taken based on EV state
Source code in src/rlbook/bandits/testbeds.py
33 34 35 36 37 38 |
|
rlbook.bandits.testbeds.NormalTestbed
Bases: Testbed
Return random value from a Normal Distribution according to expected value config
Attributes:
Name | Type | Description |
---|---|---|
expected_values |
dict
|
Dict of means and variances describing Normal Distribution of each arm in the testbed Example: expected_values = {1: {'mean': 0.5, 'var': 1}, 2: {'mean': 1, 'var': 1}} |
p_drift |
float
|
Probability for underlying reward to change ranging from 0.0 to 1.0, defaults to 0 |
drift_mag |
float
|
Magnitude of reward change when drifting, defaults to 1.0 |
Source code in src/rlbook/bandits/testbeds.py
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 |
|
action_value(action, shape=None)
Return reward value given action
Source code in src/rlbook/bandits/testbeds.py
67 68 69 70 71 72 73 74 75 76 77 78 79 |
|