Concepts / Value Iteration Algorithm

Value Iteration Algorithm

A value estimate for a state is therefore an estimate of the gambler's chance of eventually reaching $100 from that capital level.

  • Programming

A Capital-Based Decision

The Gambler's Problem looks simple: a gambler wants to reach a capital of $100, and each coin flip either increases or decreases the gambler's capital. The important decision is not merely whether to gamble. Before each flip, the gambler chooses how many dollars to stake. Value iteration studies these choices by estimating the probability of eventually reaching the goal from each capital level.

Current capitalstateSelected stakeactionHeadscapital increases$100winning endpointTailscapital decreases$0losing endpointchooseflipflipmay reachmay reach
How does the gambler's capital define the state, how does the selected stake define the action, and how do win or loss transitions change the capital?

The Gambler's MDP

The gambler's current capital is the state. At that state, the gambler selects an integer stake as the action. The coin flip creates the random transition: heads increases capital by the stake, while tails decreases capital by the stake. The transition behavior is determined when the probability of heads, represented by p_h, is known.

MDP elementRole in the Gambler's Problem
StateThe gambler's current capital level
ActionThe integer number of dollars selected as the stake
TransitionHeads increases capital by the stake; tails decreases capital by the stake
RewardOrdinary transitions have reward zero; reaching the goal has reward +1
Terminal outcomesReaching $100 means winning; running out of money means losing

The parts of the Gambler's Problem viewed as a finite Markov Decision Process

This is an undiscounted, episodic, finite MDP. It is episodic because each game ends at a terminal outcome. It is undiscounted because the problem does not use discounting. It is finite because the game is bounded by the losing endpoint and the $100 winning endpoint.

Capital levelnonterminal stateInteger stakechosen actionHeadscapital rises$100reward +1Tailscapital fallsNo moneygame endsselectcoin flipcoin flipreach endpointreach endpoint
What are the nonterminal capital states, available stake actions, terminal endpoints, rewards, and stopping conditions in one episode?

What the Value Means

Definition

The value function represents the probability of winning from each capital level.

A value estimate for a state is therefore an estimate of the gambler's chance of eventually reaching $100 from that capital level. The estimate depends on the available actions and on the random heads and tails transitions. A value function does not by itself name a stake; it describes how promising each state is. An action-selection step uses those state values to choose a stake intended to maximize the probability of reaching the goal.

Worked example

Reading a State Value

Interpret a value estimate attached to one nonterminal capital level in the Gambler's Problem.

  1. 1
    Identify the state

    The state is the gambler's current capital level.

  2. 2
    Interpret the value

    The value is the estimated probability of eventually reaching the $100 goal from that capital level.

  3. 3
    Connect value to action

    To choose a stake, the algorithm evaluates the possible heads and tails outcomes for available stakes and compares their expected future success.

Result

The value function describes expected success from states; policy selection uses those values to select a promising stake.

Value Iteration Sweeps

Value iteration begins with estimates of state values and repeatedly improves them. During each sweep, it considers the available stakes at each capital level and uses the possible heads and tails outcomes to refine the estimated probability of future success. The successive sweeps produce progressively improved value information.

Current valuesstate estimatesAvailable stakescandidate actionsHeads and tailspossible transitionsExpected returnsaction assessmentsNext valuesrefined estimatesBest actionpolicy choiceevaluateconsidercombineimprovemaximize
How do current value estimates flow through possible stakes to produce the next value estimate and the best action?

Once the optimal value function is available, action selection can choose a stake that gives the greatest value. The probability of heads matters because it determines how the possible outcomes of each stake should be evaluated.

Tied Optimal Stakes

The Gambler's Problem does not necessarily have one unique optimal policy. If two or more actions tie when evaluated against the optimal value function, choosing any of those tied actions preserves optimality. A policy is therefore allowed to be one member of a whole family of optimal policies.

Capital stateone stateStake Amaximum valuePolicy Achoose Stake AStake Bmaximum valuePolicy Bchoose Stake Bevaluateevaluatepreserves optimalitypreserves optimality
How can different stakes produce the same maximum expected value in a state, allowing multiple policies to be optimal?

A tie is not an error in action selection. It means that the current state has more than one action with the same maximum expected value. Different implementations may select different tied actions while still producing an optimal policy.

Policy Iteration Control

Policy iteration uses a different control structure from value iteration. It begins with arbitrary state values and an arbitrary policy. It then alternates between evaluating the current policy and improving that policy. Evaluation estimates how well the current policy performs. Improvement uses those estimates to check whether another action would produce a greater expected return.

Initial policyarbitrary policyPolicy evaluationupdate V(s)Policy improvementupdate π(s)Policy stablestopping decisionOptimal policyfinishUpdated policycontinueevaluateuse valuescheckyesnorepeat
How does the algorithm alternate between policy evaluation, policy improvement, and the stopping decision based on policy stability?

Evaluation and Convergence

During iterative policy evaluation, the algorithm updates V(s) under a fixed policy. It saves the value before an update, computes a newly improved value, and tracks the largest update through ∆. Repeated updates continue until the evaluation process has converged according to its convergence check. The resulting values are then available for policy improvement.

Saved valuebefore updateNew valueunder fixed policyLargest update∆Convergence checkevaluate updatesNext sweepcontinue evaluationEvaluated valuesready for improvementupdatetrack changeinspectnot convergedconvergedupdate again
How are state values repeatedly updated under a fixed policy, and how does the algorithm determine that the updates have converged?

Convergence of value evaluation answers one question: have the values for the current policy been sufficiently updated? Policy stability answers a later question: does policy improvement still want to change any action? These checks belong to different stages and should not be confused.

Improving the Policy

Policy improvement updates π(s) by selecting an action with the greatest expected return according to the current value function. The implementation compares the old action with the newly selected action. If the selected action differs, the policy is not stable and another evaluation-improvement cycle is required. If no action changes, the policy-stability decision allows the algorithm to stop.

  • Save the old action for the current state.
  • Evaluate candidate stakes using the current value function.
  • Use an argmax over expected returns to select a best action.
  • Update the policy action for that state.
  • Record whether the selected action differs from the old action.
  • Stop only when the policy remains stable.
Worked example

Tracing One Improvement Decision

Follow the control information needed when policy improvement examines one capital state.

  1. 1
    Save

    Save the action that the current policy already assigns to the state.

  2. 2
    Compare

    Use the current value function to compare the expected returns of the available stakes.

  3. 3
    Select

    Choose an action from the actions with the greatest expected return.

  4. 4
    Check

    Compare the selected action with the saved action. A difference means the policy is not stable.

Result

The stopping decision depends on whether policy improvement changed any action, not only on whether value evaluation completed.

Common Tracing Mistakes

  • Common mistake

    Treating the stake as the state

    What it looks like

    Describing the selected number of dollars as the current state

    Why it fails

    The gambler's capital defines the state; the selected stake defines the action.

    Do this instead

    Record the current capital as the state and the chosen stake as the action.

  • Common mistake

    Treating the coin flip as the controlled decision

    What it looks like

    Saying the algorithm chooses heads or tails

    Why it fails

    The coin flip supplies the random transition, while the stake supplies the controlled part of the process.

    Do this instead

    Separate the chosen stake from the random heads-or-tails outcome.

  • Common mistake

    Stopping after value evaluation converges

    What it looks like

    Ending policy iteration immediately after the value updates become sufficiently settled

    Why it fails

    Policy iteration stops only after the policy-stability decision.

    Do this instead

    Run policy improvement and check whether any action changed.

  • Common mistake

    Assuming one uniquely correct optimal policy

    What it looks like

    Treating a tied action as an implementation error

    Why it fails

    Multiple actions can tie under the optimal value function.

    Do this instead

    Accept any tied action that preserves the maximum expected value.

  • Common mistake

    Ignoring the update checkpoints

    What it looks like

    Looking only at the final policy after an unexpected result

    Why it fails

    The divergence may have occurred during value evaluation, action selection, or termination control.

    Do this instead

    Inspect the saved value, new value, running ∆, old action, updated action, and final stability decision.

Trace the Control Flow

MEDIUM

A policy-evaluation phase has updated the values and its convergence check reports that the updates have settled. During policy improvement, one state's old action is replaced by a different action because that action has the greatest expected return. Should the algorithm stop or continue? Explain which checkpoint determines the answer.

Hints
  • Separate evaluation convergence from policy stability.
  • Compare the old action with the updated action.
  • Ask whether any policy action changed.

What do you think happens?

If policy improvement changes one state's action after evaluation has converged, should policy iteration stop?

  • Yes, because value evaluation has converged

  • No, because the policy is not stable

  • Yes, because any changed action is automatically optimal

Reveal answer
Answer:

No, because the policy is not stable.

Policy iteration stops only after the policy-stability decision. A changed action means another evaluation-improvement cycle is required.

Key Takeaways

  1. 1
    In the Gambler's Problem, capital is the state and the selected integer stake is the action.
  2. 2
    Heads and tails create random transitions, and the game ends at the winning or losing endpoint.
  3. 3
    The value function represents the probability of reaching the $100 goal from each capital level.
  4. 4
    Value iteration repeatedly refines state-value estimates and uses them to support action selection.
  5. 5
    Policy iteration alternates between policy evaluation and policy improvement, stopping only when the policy is stable.
  6. 6
    Tied actions can produce multiple equally optimal policies.

At a glance

  • The Gambler's Problem is an undiscounted, episodic, finite MDP whose state is the gambler's capital.
  • The selected stake is the action, while the coin flip determines whether capital increases or decreases.
  • The value function estimates the probability of winning from each capital level.
  • Value iteration refines values across repeated sweeps; policy iteration alternates evaluation with improvement.
  • Policy iteration stops after the policy remains stable, and tied actions can create multiple optimal policies.

Related concepts

More concepts starting with V