Unit 6 of 13 · about 4 hours · derive, implement

Trajectory and per-decision importance sampling, ordinary and weighted estimators

Off-policy data has the wrong action probabilities. Importance ratios reweight it back, and the choice of estimator decides whether variance or bias dominates.

You'll be able to

  • Derive trajectory importance ratios and the per-decision form that drops irrelevant future factors.
  • Implement ordinary and weighted importance-sampling estimators on fixed trajectories.
  • Explain why ordinary sampling is unbiased with high variance while weighted sampling trades bias for lower variance.

Covers

Trajectory and per-decision importance samplingOrdinary and weighted importance estimators
1

Read

In this order, only the parts listed

0 of 2 done

  1. Off-policy prediction via importance samplingReinforcement Learning: An Introduction (2nd ed.) - Chapters 2, 5, 6 and 7

    Chapter 5, Section 5.5, equations (5.3) to (5.6) with Examples 5.4 and 5.5 and Figures 5.3 and 5.4 (printed pp. 103 to 109)

    Read the ordinary-versus-weighted comparison closely; leave incremental implementation to Section 5.6.

  2. Per-decision importance samplingReinforcement Learning: An Introduction (2nd ed.) - Starred Sections 5.8 and 5.9 and Chapter 10 Sections 10.3 to 10.5

    Starred Section 5.9 on per-decision importance sampling, equations (5.11) to (5.14) (printed pp. 114 to 115)

    Read only the per-decision expectation argument; skip discounting-aware Section 5.8 unless the discounted case needs it.

2

Focus on

  • The trajectory ratio corrects the whole-trajectory probability. Its expectation under is .
  • Ordinary sampling averages and is unbiased. Weighted sampling divides by the summed ratios; it is biased but its variance stays controlled and no single return takes weight above .
  • Per-decision sampling scales each reward only by ratios up to . Later factors have expectation given the reward, so dropping them keeps the expectation and cuts variance.
  • Example 5.5 shows ordinary sampling failing to settle after millions of episodes on a looping MDP while weighted sampling converges. Long products are the mechanism.
Common mistakes · 3
  • Multiplying ratios past the reward they correct. The per-decision argument shows those factors add variance with no change in expectation.
  • Normalizing the weighted estimator by the episode count instead of the summed weights. Equation (5.6) divides by the weight sum; dividing by rebuilds the ordinary estimator's scale.
  • Trusting ordinary sampling on looping trajectories. Example 5.5 is the warning: a few huge ratios dominate the average.
3

Try it yourself

Closed-book, before checking anything

Closed-book, take two trajectories with returns and and ratios and . Compute the ordinary estimate as and the weighted estimate as .
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.What is when ?Answer

    Every factor is 1, so the product is 1 and both estimators reduce to plain Monte Carlo.

  2. 2.Why does weighted sampling have lower variance than ordinary sampling?Answer

    It divides by the observed weight sum, so the largest weight on any return is 1. Ordinary sampling lets rare huge ratios scale returns without bound.

  3. 3.A reward at is scaled by ratios up to . Which factors can go?Answer

    All factors after . Their conditional expectation is , so per-decision estimation drops them.

  4. 4.The behavior policy never takes an action the target takes. What happens to the ratio?Answer

    It divides by zero. Coverage fails before any estimator runs; see the previous unit.

You're done when

You derive both estimators and the per-decision reduction without notes and reproduce the 2 versus 1.6 hand case.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 5 more in the section
  • Reinforcement Learning: An Introduction (2nd ed.) - Chapters 2, 5, 6 and 7Richard Sutton and Andrew Barto · primary

    The authors post the full second edition free at incompleteideas.net; cite the printed page numbers. Study Sections 2.4 and 2.5 (incremental and constant step-size estimates), Chapter 5 without the starred sections (Monte Carlo prediction and control, ordinary and weighted importance sampling), Chapter 6 Sections 6.1 to 6.7 (TD(0), Sarsa, Q-learning, Expected Sarsa, maximization bias) and Chapter 7 Sections 7.1 to 7.3 (n-step methods). Reproduce Example 5.4 (off-policy blackjack), Example 6.2 (random walk) and Example 6.6 (cliff walking), and implement each update from fixed hand-calculated trajectories before training an agent.

  • Reinforcement Learning: An Introduction (2nd ed.) - Starred Sections 5.8 and 5.9 and Chapter 10 Sections 10.3 to 10.5Richard Sutton and Andrew Barto · support

    Study starred Section 5.9 on per-decision importance sampling only for the per-decision estimator, and Chapter 10 Sections 10.3 to 10.5 on average reward and differential values only for the continuing-task unit. Cite printed pages; the authors post the full text free at incompleteideas.net.

Also in this section

  • Stanford CS234 Winter 2026 - Policy Evaluation and Q-LearningStanford University · required

    Study Lecture 3 (tabular policy evaluation) and Lecture 4 (Q-learning) slides and the relevant independent Assignment 1 and 2 written questions. Use the course treatment to state model-access, convergence, coverage and function-approximation assumptions for each update.

  • Hands-on Modern RL - Dynamic Programming, Monte Carlo and TDWalking Labs · support

    Use the DP, MC and TD page and the bandit script as a code companion. Rebuild the algorithms in the shared track schema, and keep the bandit comparison as a seed-sensitivity case: which method wins changes with the seed. Pin commit 9231bab of walkinglabs/hands-on-modern-rl; its code/chapterNN folders do not follow the site chapter numbers.

  • Gymnasium Core APIFarama Foundation · reference

    Use the reset, step, seeding and wrapper documentation as the behavior authority for the installed environment version: reset(seed=...) seeds once, step returns terminated and truncated, and vector environments autoreset on the next step by default. Test observation, reward, terminated, truncated and info fields at the boundary.

  • Deep-ML - Tabular Prediction and Control ProblemsDeep-ML · support

    Use named problems as closed-book checks after your own implementation passes: 272 (first-visit Monte Carlo), 257 (TD error), 273 (n-step TD), 175 (SARSA), 476 (Expected SARSA), 133 (Q-learning) and 518 (cliff walking, SARSA versus Q-learning). Read each problem's tests before trusting its statement.

  • Reinforcement Learning Control Laboratory - Tabular ControlRoadmap-Final · required

    Complete the MC, SARSA and Q-learning requirements with exact trajectory updates, stochastic and exploration-sensitive environments, visitation logs, matched interaction budgets and seeded alignment and bootstrap defects.