Skip to content
svgregret-bands

Cumulative regret

Which bandit policy loses the least reward compared with always playing the best arm?

Demo data · seed 20261101 · 204 rows · default settings · 640 × 360SVG, lightSVG, darkDemo CSV

How to read it

  1. Read each line as how much reward a policy loses compared with always picking the best arm.
  2. A curve that flattens has stopped losing: it found the best arm.
  3. Read the band for replication spread.
  1. Callout 1: Thompson sampling flattens early.
  2. Callout 2: A fixed split keeps losing at the same rate.
  3. Callout 3: Band: the spread across runs.

Watch out

  • Lower regret may trade against slower learning: decide what you need the result for.

Defaults

No settings: the chart draws one way.

Pinned in the code

  • Draws the run's numbers as given (computed by the Python/R engine); the chart computes nothing statistical.
  • Regret_t = Σ (μ* − μ of the arm played) up to round t; the band spans the replication quantiles the engine sends. Replications share the random stream, so Python and R simulations match.
# Cumulative regret — Codesplash Chart Lab (recipe "regret-bands")
# controls: defaults
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

try:
    import codesplash as cs  # optional: the app's chart theme
except ImportError:
    cs = None
if cs:
    cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else plt.rcParams["axes.prop_cycle"].by_key()["color"]
# non-colour encodings per series (same table as the app: series are never told apart by colour alone)
SERIES_DASHES = ["-", (0, (6, 3)), (0, (1.5, 2.5)), (0, (8, 3, 2, 3)), (0, (12, 4)), (0, (3, 3)), (0, (8, 2, 2, 2, 2, 2)), (0, (1.5, 5))]

df = pd.read_csv("regret-bands-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV

def levels_of(s):
    """Declared level order (categoricals), else order of first appearance."""
    if isinstance(s.dtype, pd.CategoricalDtype):
        return [v for v in s.cat.categories if (s == v).any()]
    return list(dict.fromkeys(s.dropna().astype(str)))
X = "round"
Y = "cumulative regret"
LO = "p10"
HI = "p90"
G = "policy"
groups = levels_of(df[G]) if G else [None]
series = []
for name in groups:
    d = df if name is None else df[df[G].astype(str) == str(name)]
    series.append((name if name is not None else "", d.dropna(subset=[X]).sort_values(X)))
fig, ax = plt.subplots(figsize=(8, 4.5))
for i, (name, d) in enumerate(series):
    col = PALETTE[i % len(PALETTE)]
    if LO and HI:
        ax.fill_between(d[X], d[LO], d[HI], color=col, alpha=0.15, lw=0)
    ax.plot(d[X], d[Y], color=col, linewidth=2, label=name or Y, linestyle=SERIES_DASHES[i % 8])  # dash per group, as in the app
ax.set_xlabel(X)
ax.set_ylabel(Y)
if G: ax.legend(fontsize=8)
plt.show()

Needs pandas, numpy, matplotlib · reads regret-bands-demo.csv · app theme from the optional codesplash helper