UCB1 and UCB1-Tuned: an index that grows for the arms not tried enough¶
A desk has three execution algorithms. Instead of choosing at random to explore, it adds to the average of each one a bonus that is large for the algorithms tried few times, and plays the best sum. UCB1-Tuned shrinks the bonus for the algorithms whose rewards vary little. A robot choosing a gain does the same, in the last section.

1. A first example: five execution algorithms, one desk¶
Three execution algorithms fill an order well with probability 0.2, 0.5 and 0.8. UCB1 learns which one to use in 500 days; UCB1-Tuned does the same.
import numpy as np
from dense_armor.core.online.bandit import UCB1
rng = np.random.default_rng(0)
rate = {"twap": 0.2, "vwap": 0.5, "pov": 0.8}
desk = UCB1(arms=["twap", "vwap", "pov"], seed=0)
for _ in range(500):
arm = desk.pull()
desk.update(arm, float(rng.binomial(1, rate[arm])))
print(desk.pull())
The index of UCB1 is the mean of an arm plus sqrt(2 ln n / n_j). Theorem 1 (page 3) bounds the regret.
import numpy as np
from dense_armor.core.online.bandit import UCB1Tuned
rng = np.random.default_rng(0)
rate = {"twap": 0.2, "vwap": 0.5, "pov": 0.8}
desk = UCB1Tuned(arms=["twap", "vwap", "pov"], seed=0)
for _ in range(500):
arm = desk.pull()
desk.update(arm, float(rng.binomial(1, rate[arm])))
print(desk.pull())
UCB1-Tuned reaches the same answer. Its bonus uses the variance of each arm; on this stream its regret falls faster in the picture.

On five execution algorithms with rates 0.2, 0.35, 0.5, 0.65 and 0.8, the mean regret per day of UCB1 reads 0.189, 0.146 and 0.093 after 50, 200 and 1000 days, UCB1-Tuned reads 0.048, 0.03 and 0.014, and epsilon-greedy with epsilon 0.1 reads 0.078, 0.036 and 0.03.
The picture follows the same 1000 days for 25 gaps between the best algorithm and four equal ones, from 0.02 to 0.4. Each point of the surface is the mean regret per day so far: cyan for UCB1, pink for UCB1-Tuned. With a gap of 0.4 UCB1 reads 0.208, 0.158 and 0.094 after 50, 200 and 1000 days and UCB1-Tuned reads 0.072, 0.032 and 0.014; with a gap of 0.02 they read 0.015, 0.016 and 0.015 and 0.013, 0.015 and 0.018, about the same: when the arms are almost equal there is little to lose.
| class or function | what it does |
|---|---|
UCB1, UCB1Tuned |
the arm with the highest mean plus an exploration bonus |
2. What it does¶
Each arm is played once; after that the policy plays the arm with the highest index mean_j + sqrt(2 * ln(n) / n_j), where n is the total number of plays and n_j is the number of times arm j was played. When the rewards are in [0, 1] and mu* is the best expected reward, Theorem 1 of the paper bounds the expected regret after n plays by 8 * sum_{i: mu_i < mu*} ln(n) / Delta_i + (1 + pi^2 / 3) * sum_j Delta_j, with Delta_i = mu* - mu_i.
UCB1Tuned is the same policy, but the exploration bonus is sqrt((ln(n) / n_j) * min(1/4, V_j(n_j))) with V_j(s) = (1/s) * sum_{tau=1}^s X_{j,tau}^2 - mean_j(s)^2 + sqrt(2 ln(n) / s), the upper confidence bound on the variance of arm j. The paper reports better empirical performance than UCB1 but no proven bound.
Source: Auer, P., Cesa-Bianchi, N., Fischer, P. (2002). Finite-time analysis of the multiarmed bandit problem. Machine Learning 47(2-3), 235-256. For UCB1: Figure 1 and Theorem 1, page 3; the index sqrt(2 ln(n) / n_j) is stated explicitly on page 11. For UCB1Tuned: Section 4, "Experiments", page 11: the definition of V_j(s) and the substitution of sqrt(2 ln(n) / n_j) with sqrt((ln n / n_j) * min{1/4, V_j(n_j)}).
3. The parameters¶
| Parameter | Meaning | Default |
|---|---|---|
arms |
the sequence of arms, any hashables | none |
seed |
seed of the random generator | 0 |
UCB1 and UCB1Tuned take the same two parameters. The methods that every policy shares (pull, update, n_seen_, state_dict, clone...) are on the shared methods page.
4. The same policy on a controller gain¶
A controller has three candidate gains and a good run is a reward of 1. The gain 8.0 works with probability 0.8. The same page also runs UCB1-Tuned on the same tries.
import numpy as np
from dense_armor.core.online.bandit import UCB1
rng = np.random.default_rng(0)
rate = {0.5: 0.2, 8.0: 0.8, 30.0: 0.5}
tuner = UCB1(arms=[0.5, 8.0, 30.0], seed=0)
for _ in range(500):
kp = tuner.pull()
tuner.update(kp, float(rng.binomial(1, rate[kp])))
print(tuner.pull())
After 500 tries UCB1 picks the gain 8.0. The Tuned version is built the same way: UCB1Tuned(arms=[0.5, 8.0, 30.0], seed=0).
API reference¶
UCB1 ¶
Bases: BanditPolicy
UCB1 policy.
Each arm is played once; after that the policy plays the arm with the
highest index mean_j + sqrt(2 * ln(n) / n_j), where n is the
total number of plays and n_j is the number of times arm j
was played. When the rewards are in [0, 1] and mu* is the
best expected reward, Theorem 1 of the paper bounds the expected
regret after n plays by
8 * sum_{i: mu_i < mu*} ln(n) / Delta_i
+ (1 + pi^2 / 3) * sum_j Delta_j
with Delta_i = mu* - mu_i.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arms
|
Sequence[Any]
|
the sequence of arms. |
required |
seed
|
int
|
seed of the random generator. |
0
|
Examples:
>>> from dense_armor.core.online.bandit import UCB1
>>> p = UCB1(arms=[0, 1], seed=0)
>>> for _ in range(60):
... a = p.pull()
... _ = p.update(a, 1.0 if a == 1 else 0.0)
>>> p.pull()
1
References
Auer, P., Cesa-Bianchi, N., Fischer, P. (2002). Finite-time
analysis of the multiarmed bandit problem. Machine Learning
47(2-3), 235-256. Figure 1 and Theorem 1, page 3; the index
sqrt(2 ln(n) / n_j) is stated explicitly on page 11.
Initialise the policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arms
|
Sequence[Any]
|
the sequence of arms. |
required |
seed
|
int
|
seed of the random generator. |
0
|
Source code in dense_armor/core/online/bandit/ucb.py
pull ¶
Choose an arm.
Plays every arm once (round-robin over the untried arms), then chooses the arm with the highest index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
dict | None
|
unused. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The chosen arm. |
Source code in dense_armor/core/online/bandit/ucb.py
update ¶
Learn from the reward of arm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arm
|
Any
|
the arm that was pulled. |
required |
reward
|
float
|
the observed reward. |
required |
context
|
dict | None
|
unused. |
None
|
Returns:
| Type | Description |
|---|---|
UCB1
|
The policy itself. |
Source code in dense_armor/core/online/bandit/ucb.py
UCB1Tuned ¶
Bases: UCB1
UCB1-Tuned policy.
Same as :class:UCB1, but the exploration bonus is
sqrt((ln(n) / n_j) * min(1/4, V_j(n_j))) with
V_j(s) = (1/s) * sum_{tau=1}^s X_{j,tau}^2 - mean_j(s)^2
+ sqrt(2 ln(n) / s),
the upper confidence bound on the variance of arm j. The paper
reports better empirical performance than UCB1 but no proven bound.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arms
|
Sequence[Any]
|
the sequence of arms. |
required |
seed
|
int
|
seed of the random generator. |
0
|
Examples:
>>> from dense_armor.core.online.bandit import UCB1Tuned
>>> p = UCB1Tuned(arms=[0, 1], seed=0)
>>> for _ in range(60):
... a = p.pull()
... _ = p.update(a, 1.0 if a == 1 else 0.0)
>>> p.pull()
1
References
Auer, P., Cesa-Bianchi, N., Fischer, P. (2002). Finite-time
analysis of the multiarmed bandit problem. Machine Learning
47(2-3), 235-256. Section 4, "Experiments", page 11: the
definition of V_j(s) and the substitution of
sqrt(2 ln(n) / n_j) with
sqrt((ln n / n_j) * min{1/4, V_j(n_j)}).
Initialise the policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arms
|
Sequence[Any]
|
the sequence of arms. |
required |
seed
|
int
|
seed of the random generator. |
0
|
Source code in dense_armor/core/online/bandit/ucb.py
update ¶
Learn from the reward of arm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arm
|
Any
|
the arm that was pulled. |
required |
reward
|
float
|
the observed reward. |
required |
context
|
dict | None
|
unused. |
None
|
Returns:
| Type | Description |
|---|---|
UCB1Tuned
|
The policy itself. |
Source code in dense_armor/core/online/bandit/ucb.py
Details¶
Choices¶
- The figures are drawn with the library by
docs/assets/bandit/make_bandit_style_figures.py. Five execution algorithms with success rates 0.2, 0.35, 0.5, 0.65 and 0.8 (or the stated gap), 1000 days, the regret of a day is the rate of the best algorithm minus the rate of the one that was pulled, and the surface is the mean over the days so far.