AI & Machine Learning Master Course — 170. Sequential-decision lab: evaluate a toy policy
Smartyfy AI & Machine Learning Master Course
Course ID: smartyfy-ai-ml-master-200
Lesson 170: Sequential-decision lab: evaluate a toy policy
Prerequisites: Lesson 169. Learning outcome: Test a simple policy against baselines and explain why simulated success may not transfer.
Define the evaluation before simulating outcomes
This lab compares three fixed policies in the same fictional MDP: take the immediate-safe action; invest and then choose the safe action; or invest and then choose the risky action. The policies are specified before sampling evaluation episodes. No evaluation reward is used to refit or tune them.
Every episode begins at state zero and ends at terminal state two. The discount is 0.9. We report mean discounted return, Monte Carlo standard error of that mean, and the fraction of episodes with negative return. The final measure exposes a downside that a single average can hide.
The words safe and risky are names for this toy model’s reward patterns. They do not certify real-world safety. The environment has no hidden state, resource constraints, delayed harm, or changing dynamics beyond what its helper file specifies.
Compute an exact reference before Monte Carlo
The immediate-safe policy always returns one. The planned-safe policy always returns 0.9 times three, or 2.7. The risky policy returns 5.4 with probability one half and -1.8 with probability one half, giving exact mean 1.8.
Its negative-return probability is one half. Its return variance is 0.5 times (5.4-1.8) squared plus 0.5 times (-1.8-1.8) squared, or 12.96. For five thousand independent episodes under this fixed model, the theoretical standard error of its sample mean is 3.6/sqrt(5000), approximately 0.05091.
These quantities provide an independent reference for the simulator. They are known because the transition model is known. In an unfamiliar environment, sampled evaluation may be necessary precisely because such exact values are unavailable.
CPU lab: evaluate five thousand episodes per policy
Save mdp167.py from Lesson 167 beside the script. Each policy receives a separately restarted pseudorandom generator with seed 170. The same seed makes the comparison reproducible, but different trajectory lengths can consume random draws differently; the code does not claim a special paired variance reduction.
Immediate-safe has sample mean one, zero Monte Carlo standard error, and no negative episodes. Planned-safe has mean 2.7, zero Monte Carlo standard error, and no negative episodes. Their returns are deterministic in this model.
Risky has sample mean approximately 1.687680, Monte Carlo standard error approximately 0.050892, and negative-return fraction 0.5156. Its sample mean differs from the exact value 1.8 because the realized episodes contain more negative outcomes than half. The code reports that deviation rather than changing the seed to make the result look closer.
This is simulated evaluation of fixed policies, not new reinforcement-learning training. It does not demonstrate transfer to physical systems, users, or an external service.
import numpy as np
from mdp167 import sample_step
policies={'immediate_safe':[0,0],'planned_safe':[1,0],'risky':[1,1]}
results={}
for name,policy in policies.items():
rng=np.random.default_rng(170) # Common seeded stream, separately restarted per fixed policy.
returns=[]
for _ in range(5000):
state=0;discount=1.;total=0.
while state!=2:
action=policy[state];state,reward,done=sample_step(state,action,rng)
total+=discount*reward;discount*=.9
returns.append(total)
r=np.array(returns)
results[name]=(r.mean(),r.std(ddof=1)/np.sqrt(len(r)),np.mean(r<0))
for name,values in results.items():
print(name,'mean / Monte Carlo SE / negative fraction:',np.round(values,6).tolist())
print('Exact model means:',{'immediate_safe':1.,'planned_safe':2.7,'risky':1.8})
assert np.isclose(results['planned_safe'][0],2.7)
assert results['risky'][2]>0 and results['immediate_safe'][2]==0
Compare expected value and downside
| Policy | Expected discounted return | Negative-return probability |
|---|---|---|
| Immediate-safe | 1.0 | 0 |
| Planned-safe | 2.7 | 0 |
| Risky | 1.8 | 0.5 |
Separate Monte Carlo uncertainty from model uncertainty
Monte Carlo standard error quantifies sampling variation in the estimated mean under the fixed simulator. Zero standard error for a deterministic policy does not mean its real-world value is known without uncertainty. The transition and reward assumptions may still be wrong.
The risky sample mean is about 2.2 estimated standard errors below its exact expectation. Such a realization is possible. A normal-approximation interval from one run need not contain the true model mean, just as the earlier experimental lesson emphasized. Do not assert truth coverage as a mandatory unit test.
Model sensitivity asks a different question. If the risky action’s probability of reward six changes from 0.5 to p, its expected start return becomes 0.9 times (8p-2). It exceeds the planned-safe return 2.7 only when p is greater than 5/8. Whether such a model change is plausible requires evidence outside this simulator.
The objective can also change. A decision maker may care about constraints, quantiles, downside probability, or costs in addition to expected reward. State those requirements before choosing a policy rather than selecting whichever summary favors a preferred result.
Write the final project conclusion with boundaries
The planned-safe policy is best among the three fixed candidates for expected return in the supplied model. The exact dynamic-programming result also identifies it as optimal for that model. Simulation confirms the implementation’s behavior while retaining ordinary finite-sample noise for the stochastic policy.
A reproducible report should include the helper file, policy definitions, initial state, discount, number of episodes, seeds, reward timing, termination rules, exact reference values, and sampled outputs. Keep the policies frozen while producing final evaluation results.
For a larger study, use separate development and final evaluation scenarios. Examine multiple environment seeds, initial conditions, and plausible model variations. Do not tune on final scenarios and then present their performance as an untouched test.
The project’s strongest conclusion is narrow but useful: the local implementation agrees with a transparent decision model, and its uncertainty calculations are interpretable. Real deployment would require validated dynamics, appropriate constraints, and evidence that the reward captures the intended outcome.
Four practice questions with worked answers
Practice 1
Compute the risky policy’s exact expected return.
Answer: The two discounted returns are 5.4 and -1.8 with equal probability. Their average is (5.4-1.8)/2 = 1.8.
Practice 2
Why do deterministic policies have zero Monte Carlo standard error here?
Answer: Every episode produces the same return under the fixed model, so sample variance is zero. This says nothing about uncertainty in whether the model correctly represents another environment.
Practice 3
At what risky success probability does its expected return equal 2.7?
Answer: Solve 0.9 times (8p-2) = 2.7. Dividing by 0.9 gives 8p-2=3, so p=5/8=0.625. Higher probabilities favor risky on expected return alone.
Practice 4
Why not change the seed until the risky sample mean is nearer 1.8?
Answer: That selects an evaluation realization based on its outcome and hides sampling variation. The chosen seed should remain part of the reproducible protocol; unexpected but plausible noise should be reported and investigated honestly.
Five multiple-choice questions
Question 1
What is held fixed during this lab’s evaluation?
- A. Only the final policy with the highest sampled reward after every episode.
- B. The three policies and the supplied environment model.
- C. A policy that is retuned after observing each evaluation reward.
Correct: B. The candidate policies are prespecified and receive no evaluation-time learning.
Question 2
What does the risky policy’s negative-return fraction measure?
- A. The share of immediate transition rewards below zero across all transitions.
- B. The probability that the model’s transition table is incorrect.
- C. The share of evaluated episodes whose discounted return is below zero.
Correct: C. Its denominator is episodes. Counting individual transition rewards uses a different unit, and neither measure establishes model validity.
Question 3
What does zero Monte Carlo error for planned-safe imply?
- A. Its simulated return is deterministic under the fixed model.
- B. No uncertainty remains about reward design or transfer.
- C. Its value in every possible real environment is known exactly.
Correct: A. Sampling error vanishes for a constant return, while model and task uncertainty remain separate.
Question 4
Which probability makes risky better than planned-safe on expected return?
- A. Any positive probability of reward six.
- B. A reward-six probability below 0.5.
- C. A reward-six probability greater than 0.625.
Correct: C. The threshold follows from 0.9(8p-2)>2.7. A large best-case payoff alone is insufficient.
Question 5
What conclusion is supported by the project?
- A. Planned-safe has the best expected return in the specified model.
- B. One seed establishes robust performance under all initial-state distributions.
- C. A successful simulator policy can be deployed without validating its dynamics.
Correct: A. The model and initial state bound the result. Transfer and robustness require additional evidence.
Common pitfalls
Avoid tuning policies on evaluation episodes, replacing unfavorable seeds, confusing Monte Carlo error with model uncertainty, or interpreting toy reward names as safety guarantees. Keep exact and sampled results distinct.
Summary
A policy evaluation should report its objective, protocol, uncertainty, and limitations. The toy project combines exact references with sampled episodes and shows why simulator success remains a bounded result.