Skip to content

The shared methods of every policy

← Bandits

Every bandit policy of the library comes from one base class, BanditPolicy, and inherits a set of methods that no page showed. They decide what a policy counts as a reward, how it is saved and copied, how its settings are changed, and one of them, fit, refuses a table and says how to learn. A robot that tunes a controller gain uses the same methods, in the last section.

The mean regret per day of UCB1 over 1000 days, for 2 to 12 arms.

1. A first example: what a policy counts

A desk with three execution algorithms learns from rewards one at a time. A reward for an algorithm that does not exist, or a reward that is not a number, is not learned: it is counted apart.

from dense_armor.core.online.bandit import EpsilonGreedy

desk = EpsilonGreedy(arms=["twap", "vwap", "pov"], epsilon=0.1, seed=0)
for arm, reward in (("pov", 1.0), ("pov", 1.0), ("vwap", 0.0)):
    desk.update(arm, reward)
desk.update("unknown", 1.0)
desk.update("pov", float("nan"))
print(desk.n_seen_, desk.n_missing_)
3 2

The three good rewards are learned: n_seen_ is 3. The reward of an arm that is not in the set and the NaN reward are skipped and counted in n_missing_: 2. update also returns the policy itself, so calls can be chained. pull returns one of the arms and takes an optional context that the non-contextual policies ignore.

UCB1 on five execution algorithms: how many times each arm was pulled over 1000 days.

On five execution algorithms with rates 0.2, 0.35, 0.5, 0.65 and 0.8, UCB1 pulled the arms 25, 56, 84, 183 and 652 times in 1000 days: the best arm gets most of the pulls and the worst one the fewest, which is what state_dict()["counts_"] holds.

The picture follows the same 1000 days for UCB1 with 2 to 12 execution algorithms of rates between 0.2 and 0.8. Each point of the surface is the mean regret per day so far. After 50, 200 and 1000 days it reads 0.072, 0.03 and 0.014 with 2 arms, 0.189, 0.146 and 0.093 with 5 arms and 0.252, 0.223 and 0.168 with 12 arms: the more arms, the more there is to try before the best one is found.

class what it does
BanditPolicy the base class of every policy: pull, update, the counters and the methods of Root

2. Save, restore and copy a policy

state_dict() returns what the policy has learned, the attributes that end in an underscore. clone() builds a fresh policy with the same settings and nothing learned. load_state_dict() puts a saved state in a policy.

from dense_armor.core.online.bandit import EpsilonGreedy

desk = EpsilonGreedy(arms=["twap", "vwap", "pov"], epsilon=0.1, seed=0)
for arm, reward in (("pov", 1.0), ("pov", 1.0), ("vwap", 0.0)):
    desk.update(arm, reward)
state = desk.state_dict()
copy = desk.clone()
print(copy.n_seen_, sorted(state))
copy.load_state_dict(state)
print(copy.n_seen_, copy.get_params()["epsilon"])
0 ['counts_', 'means_', 'n_missing_', 'n_seen_']
3 0.1

The clone starts with n_seen_ equal to 0 and the same epsilon; after load_state_dict it has the 3 rewards of the saved policy.

3. Change a setting: mutate or set_params

get_params() returns the settings (the constructor parameters). set_params() changes them in place and returns the policy, and it checks the settings that mutate may change in the same way (a value that does not pass raises ValueError and nothing changes). mutate() changes, in place and after the same check as the constructor, the settings that the policy reads at every decision: epsilon of EpsilonGreedy, alpha of LinUCBDisjoint, c, horizon and sigma of BayesUCB and sigma of ThompsonSampling. A value that the constructor would refuse raises ValueError and changes nothing.

from dense_armor.core.online.bandit import EpsilonGreedy

desk = EpsilonGreedy(arms=["twap", "vwap", "pov"], epsilon=0.1, seed=0)
desk.mutate({"epsilon": 0.2})
print(desk.epsilon)
try:
    desk.mutate({"epsilon": 5})
except ValueError as e:
    print(e, desk.epsilon)
desk.set_params(epsilon=0.3)
print(desk.epsilon)
0.2
epsilon must be in [0, 1] 0.2
0.3

mutate({"epsilon": 0.2}) changes epsilon to 0.2. mutate({"epsilon": 5}) is refused, as the constructor refuses it, and epsilon stays at 0.2. The names that a policy does not list as mutable, such as seed, arms or what it has learned, are left alone and nothing raises. UCB1, UCB1Tuned and Exp3 list no mutable setting, so mutate leaves them as they are.

4. Describe the policy, and the window

describe() returns a JSON-serialisable description of the policy, and window_samples(seconds) converts a window in seconds to a number of samples.

from dense_armor.core.online.bandit import EpsilonGreedy

desk = EpsilonGreedy(arms=["twap", "vwap", "pov"], epsilon=0.1, seed=0)
d = desk.describe()
print(d["name"], sorted(d["parameters"]), d["methods"])
print(desk.window_samples(2.0), desk.budget_s, desk.memory_class)
EpsilonGreedy ['arms', 'epsilon', 'seed'] ['update']
2 0.001 O(n_arms)

The description lists the parameters (arms, epsilon, seed) and the learning method, update. A window of 2 seconds is 2 samples: when the policy has no sample period (dt), window_samples counts one sample per second. budget_s is the time budget of one call, 0.001 s, and memory_class says that the memory grows with the number of arms.

5. fit refuses a table and says how to learn

Every estimator of the library has fit, which feeds a whole table to learn_one row by row. A bandit policy learns one reward at a time, with pull and update, so fit stops with an error that says so.

from dense_armor.core.online.bandit import EpsilonGreedy

desk = EpsilonGreedy(arms=["twap", "vwap", "pov"], epsilon=0.1, seed=0)
try:
    desk.fit([{"arm": "pov"}], [1.0])
except TypeError as e:
    print(e)
EpsilonGreedy is not fitted on a table: learn with pull() and update(arm, reward), or judge a policy on a log with offline_replay

Use a loop of pull and update, as on the other pages, or offline_replay on a log. RobotKpSelector gives the same error and names its own way to learn, run_round().

6. The same methods on a controller gain

A controller has three candidate gains. The same counters, the same saved state and the same description apply to them.

from dense_armor.core.online.bandit import UCB1

tuner = UCB1(arms=[0.5, 8.0, 30.0], seed=0)
for gain, reward in ((8.0, 1.0), (8.0, 1.0), (0.5, 0.0)):
    tuner.update(gain, reward)
tuner.update(99.0, 1.0)
print(tuner.n_seen_, tuner.n_missing_, tuner.clone().n_seen_, sorted(tuner.state_dict()))
3 1 0 ['counts_', 'means_', 'n_missing_', 'n_seen_']

The gain 99.0 is not one of the arms, so it is counted in n_missing_: 1. The clone has learned nothing.

API reference

BanditPolicy

BanditPolicy(arms: Sequence[Any], seed: int = 0)

Bases: _PullPolicy

Base class for bandit policies.

A bandit policy chooses an arm at each step (pull) and learns from the reward of the chosen arm (update). Arms are any hashables. The set of arms is fixed at construction time; a pull returns one of those arms, and an update with an arm that is not in the set is counted in n_missing and ignored.

Parameters:

Name Type Description Default
arms Sequence[Any]

the sequence of arms, any hashables.

required
seed int

seed of the random generator.

0

Raises:

Type Description
ValueError

if arms is empty.

Attributes:

Name Type Description
arms

the arms as a tuple.

n_seen_

number of rewards learned so far.

n_missing_

number of samples skipped because of NaN, missing arm, or non-numeric reward (n_missing reads it).

Initialise the policy.

Parameters:

Name Type Description Default
arms Sequence[Any]

the sequence of arms.

required
seed int

seed of the random generator.

0

Raises:

Type Description
ValueError

if arms is empty.

Source code in dense_armor/core/online/bandit/base.py
def __init__(self, arms: Sequence[Any], seed: int = 0) -> None:
    """Initialise the policy.

    Args:
        arms: the sequence of arms.
        seed: seed of the random generator.

    Raises:
        ValueError: if ``arms`` is empty.
    """
    super().__init__()
    if not arms:
        raise ValueError("arms must be non-empty")
    self.arms = tuple(arms)
    self.seed = int(seed)
    self._rng = np.random.default_rng(self.seed)
    self.n_seen_ = 0
    self.n_missing_ = 0

pull

pull(context: dict | None = None) -> Any

Choose an arm.

Parameters:

Name Type Description Default
context dict | None

optional dict of features, ignored by non-contextual policies.

None

Returns:

Type Description
Any

The chosen arm.

Raises:

Type Description
NotImplementedError

in the base class.

Source code in dense_armor/core/online/bandit/base.py
def pull(self, context: dict | None = None) -> Any:
    """Choose an arm.

    Args:
        context: optional dict of features, ignored by non-contextual
            policies.

    Returns:
        The chosen arm.

    Raises:
        NotImplementedError: in the base class.
    """
    raise NotImplementedError

update

update(
    arm: Any, reward: float, context: dict | None = None
) -> BanditPolicy

Learn from the reward of arm.

Parameters:

Name Type Description Default
arm Any

the arm that was pulled.

required
reward float

the observed reward, a finite float.

required
context dict | None

optional dict of features, ignored by non-contextual policies.

None

Returns:

Type Description
BanditPolicy

The policy itself.

Raises:

Type Description
NotImplementedError

in the base class.

Source code in dense_armor/core/online/bandit/base.py
def update(
    self, arm: Any, reward: float, context: dict | None = None
) -> "BanditPolicy":
    """Learn from the reward of ``arm``.

    Args:
        arm: the arm that was pulled.
        reward: the observed reward, a finite float.
        context: optional dict of features, ignored by non-contextual
            policies.

    Returns:
        The policy itself.

    Raises:
        NotImplementedError: in the base class.
    """
    raise NotImplementedError

Details

Choices

  • What is shown here is the behaviour of the code: the outputs above are printed by the examples, not stated from the docstrings.
  • fit and mutate are shared with two classes that are not BanditPolicy: LinUCBDisjoint and RobotKpSelector come from the same base, _PullPolicy, so they refuse a table in the same way and check a mutable setting (alpha of LinUCBDisjoint) in the same way.
  • History: before this change fit failed with EpsilonGreedy has no learn_one, an accident of inheriting Root.fit, and mutate changed nothing on any policy because none declared a mutable setting.
  • The figures are drawn with the library by docs/assets/bandit/make_bandit_style_figures.py.