Action selection: optimistic values, UCB and gradient bandits
Action selection methods for multi-armed bandits: optimistic initial values, Upper-Confidence-Bound (UCB) action selection, and gradient-bandit (policy-gradient) algorithms — why they work, precise formulas, derivations, numerical examples and a runnable Python example.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Two or three sentences: where an engineer uses this.
Choosing actions under uncertainty is a core part of many engineering systems: online recommendation, A/B testing, wireless channel selection, and exploration in reinforcement learning algorithms (e.g., when using Monte Carlo control or actor–critic methods). The methods here (optimistic initial values, UCB, gradient bandits) help balance exploration and exploitation so that the system learns high-reward actions efficiently.
Key ideas
Define every term before using it and give exact statements and derivations.
Bandit problem: a decision problem with a fixed set of actions (arms) A = {1,...,k} where each action a has an unknown reward distribution. At each time step t, the agent chooses an action A_t and receives reward R_t drawn from that action's distribution. Objective: maximise cumulative reward (or minimise regret).
True action value q*(a): the expected reward of action a.
Exact definition: q*(a) = E[R_t | A_t = a], assumed stationary unless explicitly stated otherwise.
Estimated action-value Q_t(a): the agent's estimate (at time t) of q*(a). It is updated from observed rewards.
Sample-average update (for stationary problems): if N_t(a) is count of times action a taken before t and a is chosen at time t producing reward R_t, then Q_{t+1}(a) = Q_t(a) + (1 / N_{t+1}(a)) (R_t - Q_t(a)).
Assumption: rewards are identically distributed for each arm (stationary). Unit: N/A.
Step-size (constant-α) update (for non-stationary problems): Q_{t+1}(a) = Q_t(a) + α (R_t - Q_t(a)), 0 < α ≤ 1.
Here α is a learning rate (step-size) that controls how fast new rewards influence Q. Use small α for high-noise or slowly changing problems.
Optimistic initial values: set Q_0(a) to a high value (larger than expected real q*(a)). This causes initial greedy action selection to explore: suboptimal actions will, when their observed rewards drop, be tried less, while unexplored actions keep their high optimistic Q and get chosen. Exact statement: optimistic initialisation is a pure greedy policy w.r.t. initial Q_0 that implicitly drives exploration by overestimating untried actions.
Assumptions: works best in stationary settings and when using sample-average updates so initial optimism decays as real rewards come in.
Upper-Confidence-Bound (UCB) action selection: choose the action a that maximises an optimism term plus estimated value.
Common formula (UCB1-style): select A_t = argmax_a [ Q_t(a) + c · sqrt( (ln t) / N_t(a) ) ], where c > 0 controls degree of exploration.
Intuition and derivation sketch: assume rewards bounded (e.g., in [0,1]). By Hoeffding's inequality, with high probability the estimation error |Q_t(a) − q*(a)| ≤ sqrt( (ln t) / (2 N_t(a)) ). The UCB adds an exploration bonus proportional to this uncertainty bound. Choose constant c to scale the bound. Assumptions: bounded rewards and counts N_t(a) > 0 (for N_t(a)=0, treat bonus as +∞ or force one pull of each arm first).
Diagram to draw (for exam): draw a horizontal axis of actions (1..k). Above each action draw a vertical bar for Q_t(a) (height = sample mean) and an upward whisker for the confidence radius c sqrt((ln t)/N_t(a)). Label Q_t(a), N_t(a) and t. Show argmax picks the arm with highest bar+whisker.
Gradient bandit (policy-gradient for bandits): maintain a preference H_t(a) for each action and convert preferences to a probability distribution (policy) using the softmax function. Definitions:
- Preference H_t(a): real-valued number where higher H means more likely to be chosen.
- Softmax policy π_t(a) = exp(H_t(a)) / sum_b exp(H_t(b)).
The goal is to adjust H_t to maximise expected reward. Using the policy gradient theorem (specialised to bandits) and stochastic gradient ascent, the incremental update is:
H_{t+1}(a) = H_t(a) + α (R_t − baseline_t) (1_{A_t = a} − π_t(a)).
Here 1_{A_t = a} is the indicator that action a was taken at t, α is a small positive step-size and baseline_t often chosen as the average reward to reduce variance.
Derivation sketch: start with objective J(θ) = E[R_t], parametrize policy by H (θ ≡ H), compute, for each component b, ∂/∂H_t(b) log π_t(a) = 1_{b=a} − π_t(b). Then use REINFORCE: update H ∝ α R_t ∇_H log π_t(A_t). Subtract baseline (independent of action) does not change expected gradient but reduces variance.
Assumptions: differentiable parametric policy, rewards possibly non-stationary, baseline chosen so updates are stable.
Comparison notes: optimistic initial values and UCB are value-based methods (they build Q estimates). UCB adds principled confidence-based exploration; optimistic values rely on initial bias decaying. Gradient bandits are policy-based and can handle preferences and non-stationarity well; they directly parameterise the policy and optimise expected reward.
Formulas
Each formula on its own line; explanation follows.
Q_{t+1}(a) = Q_t(a) + (1 / N_{t+1}(a)) · (R_t - Q_t(a)) — Q_t(a) is sample-average estimate at time t; N_{t+1}(a) is count after this reward; R_t is reward observed at t; units: same as reward (SI unit depends on problem; often unitless).
Q_{t+1}(a) = Q_t(a) + α · (R_t - Q_t(a)) — constant step-size update; α is step-size (unitless), used in non-stationary problems.
A_t = argmax_a [ Q_t(a) + c · sqrt((ln t) / N_t(a)) ] — UCB action selection; t is current time-step (t ≥ 1), N_t(a) > 0; c > 0 controls exploration; units: N/A.
π_t(a) = exp(H_t(a)) / sum_b exp(H_t(b)) — softmax policy from preferences H_t(a); units: N/A.
H_{t+1}(a) = H_t(a) + α · (R_t - baseline_t) · (1_{A_t = a} - π_t(a)) — gradient-bandit preference update; baseline_t often sample-average of past rewards.
P(|X̄_n - μ| ≥ ε) ≤ 2 exp(-2 ε^2 n) — Hoeffding bound for independent variables in [0,1]: ε ≥ 0, n samples, μ true mean; used to justify UCB bonus. Units: N/A.
Worked examples
Two numerical examples and one runnable program example.
Example 1 — Optimistic initial values (numerical, exam style). Problem: two-armed bandit, true means q*(1)=1.0, q*(2)=2.0 (units: reward units). Initialise Q_0(1)=Q_0(2)=5.0 (optimistic). Use sample-average updates. Show first three time steps.
Step 0: N_0(1)=N_0(2)=0. Q_0(1)=5.0, Q_0(2)=5.0. Greedy tie-break: choose arm 1.
t=1: Choose A_1=1. Assume observed reward R_1 = 0.8 (sampled from arm 1). Update N_1(1)=1. Q_1(1)=5 + (1/1)(0.8-5) = 0.8. Q_1(2)=5.0 unchanged.
t=2: Greedy picks argmax Q_1 = arm 2 (Q_1(2)=5.0). Assume R_2 = 2.1. N_2(2)=1. Q_2(2)=2.1. Q_2(1)=0.8.
t=3: Greedy picks arm 2 (Q_2(2)=2.1 > 0.8). Suppose R_3 = 1.9. N_3(2)=2. Q_3(2)=2.1 + (1/2)(1.9-2.1)=2.0.
Final after 3 steps: Q_3(1)=0.8, Q_3(2)=2.0. Optimistic initialisation forced exploring both arms; no random epsilon needed.
Example 2 — UCB numeric calculation. Problem: k=3 arms. At time t=10, sample means Q_t = [1.2, 1.0, 1.5], counts N_t = [5, 3, 1]. Use c = 1. Choose action by UCB1 formula.
Compute bonuses: for arm 1: bonus = 1 * sqrt(ln 10 / 5) = sqrt(2.3026 / 5) = sqrt(0.4605) = 0.6789. Arm1 score = 1.2 + 0.6789 = 1.8789.
Arm2: bonus = sqrt(2.3026 / 3) = sqrt(0.7675) = 0.8762. Score = 1.0 + 0.8762 = 1.8762.
Arm3: bonus = sqrt(2.3026 / 1) = sqrt(2.3026) = 1.5174. Score = 1.5 + 1.5174 = 3.0174.
Choose arm 3 (highest score). Units: reward units. Final answer: arm 3.
Example 3 — Gradient bandit: small Python program (runnable).
Code (complete):
import math
import random
random.seed(0)
# Simple gradient bandit for a 3-arm bandit, fixed true means
true_means = [1.0, 1.5, 0.5]
H = [0.0, 0.0, 0.0] # preferences
alpha = 0.1
avg_reward = 0.0
count = 0
def softmax(prefs):
exps = [math.exp(h) for h in prefs]
s = sum(exps)
return [e / s for e in exps]
for t in range(1, 51):
probs = softmax(H)
# sample an action
r = random.random()
cum = 0.0
for i, p in enumerate(probs):
cum += p
if r <= cum:
a = i
break
# reward sampled from Normal(true_mean, 0.5)
reward = random.gauss(true_means[a], 0.5)
count += 1
baseline = avg_reward
# update preferences
for i in range(len(H)):
H[i] += alpha * (reward - baseline) * ((1 if i == a else 0) - probs[i])
avg_reward += (reward - avg_reward) / count
if t % 10 == 0:
print(f"t={t} probs={[round(p,3) for p in probs]} avg_reward={avg_reward:.3f}")
Sample output (exact output will vary due to randomness). Example of one run:
t=10 probs=[0.1, 0.84, 0.06] avg_reward=1.346
t=20 probs=[0.15, 0.78, 0.07] avg_reward=1.396
t=30 probs=[0.17, 0.77, 0.06] avg_reward=1.420
t=40 probs=[0.18, 0.76, 0.06] avg_reward=1.430
t=50 probs=[0.19, 0.75, 0.06] avg_reward=1.435
This program shows the policy concentrating probability on the arm with higher true mean (arm 2).
Common mistakes
- Confusing Q_t(a) (agent estimate) with q*(a) (true mean). Always label which is which.
- For gradient bandits, forgetting to subtract the baseline (average reward) — this increases variance and often destabilises learning.
- Mis-applying Hoeffding's inequality for unbounded rewards. UCB derivation assumes bounded rewards (e.g., in [0,1]).
- Not normalising softmax numerically (large H values can cause overflow); in code subtract max(H) before exp.
For your exam
Kinds of questions: definitions, short notes, derivations, numericals, code or pseudocode, diagrams.
What a full-marks answer includes:
- Definition of bandit, q*(a), Q_t(a), and clear statement of the objective (maximize cumulative reward / minimize regret).
- For optimistic initial values: explanation of mechanism, assumptions (stationary rewards), and example showing initial exploration.
- For UCB: write the UCB formula, derive the bonus term from Hoeffding bound sketch, mention bounded-reward assumption, and explain how c affects exploration.
- For gradient bandits: derive the preference update from ∇ log π and REINFORCE, state the softmax policy, and explain role of baseline and α.
- If asked for a diagram: include the bar+whisker diagram for UCB as described in Key ideas.
- For numericals: show step-by-step computation, units, and final bold answer.
Quick check
Three short questions.
- What does an optimistic initial value encourage and in which setting is it most appropriate?
- Write the UCB1 action selection rule and name the inequality used to justify the bonus term.
- For gradient bandits, what is ∇_H log π_t(a) for the softmax policy?
Answers: 1) It encourages exploration by overestimating untried actions; best for stationary problems using sample-average updates. 2) A_t = argmax_a [ Q_t(a) + c · sqrt((ln t) / N_t(a)) ]; justified by Hoeffding's inequality. 3) For each component b: ∂/∂H_t(b) log π_t(a) = 1_{b=a} - π_t(b).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?