Bandits and Dynamic Programming

Results — agents learning

Stationary 10-armed bandit

Each curve averages 200 independent runs on freshly sampled testbeds. ε-greedy explores at a fixed rate; optimistic initialization forces early exploration then coasts; UCB steers exploration toward uncertain arms; the gradient bandit learns action preferences by stochastic gradient ascent.

Parameter study

Average reward over the first 1000 steps as a function of each method's single hyperparameter (100 runs per point), swept over powers of two. This is the Sutton & Barto Figure 2.6 summary — every algorithm has a sweet spot.

Nonstationary bandit — why a constant step size wins

Here the arm values drift by a random walk every step. The sample-average estimator gives every reward equal weight and lags the moving target; a constant step size (α = 0.1) keeps chasing it. Averaged over 200 runs.

Gridworld — dynamic programming

The 4×4 gridworld (corners are terminal, −1 reward per step, γ = 0.9). Policy iteration and value iteration converge to the same optimal value function and greedy policy. Each cell shows the optimal move and its state value.

Policy iteration
Value iteration

Gambler's problem

Value iteration on the capital-betting MDP for three coin biases. The value function is the probability of reaching the $100 goal; the policy is the optimal stake at each capital level — producing the famous jagged staircase when the coin is unfavorable.

How it was built

Part 1: Stationary k-Armed Bandits

Build a seeded bandit testbed, implement incremental sample-average updates and epsilon-greedy selection, then average reward and optimal-action curves over many runs.

Part 2: Nonstationary and Advanced Bandits

Add random-walk nonstationarity, constant step-size updates, optimistic initialization, UCB action selection and gradient bandits, then run a parameter study comparing all methods.

Part 3: Gridworld Dynamic Programming

Encode the book gridworld as an MDP, perform iterative policy evaluation and greedy improvement, run full policy iteration, and solve the same MDP directly with value iteration.

Part 4: Gambler's Problem

Model the capital-betting MDP, compute optimal state values with value iteration, and extract the optimal stake for every capital level.