Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Online ml River Bandit RandomPolicy

From Leeroopedia


Knowledge Sources
Domains Online_Learning, Multi_Armed_Bandits, Baseline
Last Updated 2026-02-08 16:00 GMT

Overview

A baseline bandit policy that randomly selects arms with uniform probability, useful for establishing performance baselines.

Description

RandomPolicy implements the simplest possible bandit strategy by selecting arms uniformly at random at each time step. Despite its simplicity, it still tracks reward statistics for each arm, making it useful for comparison purposes. The policy does not exploit learned information about arm performance but provides a lower bound on expected performance. It's primarily used as a baseline to demonstrate that other algorithms are learning effectively.

Usage

Use RandomPolicy as a baseline when evaluating other bandit algorithms. If a sophisticated algorithm performs worse than random selection, it indicates a problem with the algorithm or its configuration. Also useful for ablation studies to quantify the value of intelligent arm selection.

Code Reference

Source Location

Signature

class RandomPolicy(bandit.base.Policy):
    def __init__(self, reward_obj=None, burn_in=0, seed: int | None = None):
        ...

    def _pull(self, arm_ids):
        return self._rng.choice(arm_ids)

Import

from river import bandit

I/O Contract

Parameter Type Description
reward_obj RewardObj (optional) Reward statistic (defaults to stats.Mean())
burn_in int (default: 0) Minimum pulls per arm (not meaningful for random)
seed int (optional) Random seed for reproducibility

Usage Examples

import gymnasium as gym
from river import bandit
from river import stats

env = gym.make('river_bandits/CandyCaneContest-v0')
_ = env.reset(seed=42)
_ = env.action_space.seed(123)

policy = bandit.RandomPolicy(seed=123)

metric = stats.Sum()
while True:
    action = policy.pull(range(env.action_space.n))
    observation, reward, terminated, truncated, info = env.step(action)
    policy.update(action, reward)
    metric.update(reward)
    if terminated or truncated:
        break

print(metric)  # Sum: 755.

# Use for offline evaluation as baseline
news = bandit.datasets.NewsArticles()
total_reward, n_samples = bandit.evaluate_offline(
    policy=bandit.RandomPolicy(seed=42),
    history=news,
)
print(f"Random baseline: {total_reward} over {n_samples} samples")

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment