First-Visit Monte Carlo prediction estimates the value of a state by averaging returns from episodes, counting only the first visit to that state in each episode.
The problem, from first principles
Given episodes (trajectories) from a fixed policy π, estimate V^π(s) for each state s. Unlike bootstrapping, MC uses full episode returns without a model. The "first-visit" constraint ensures independence: each episode contributes at most one return for state s.
Takes a list of episodes (each is a list of (state, reward) tuples)
Takes discount factor γ
Returns a dict {state: estimated_value}
Use first-visit semantics and unbiased averaging.
Constraints
Each episode is a list of (state, reward) pairs
Only count the first visit to each state per episode
Return a dict mapping states to float values
If a state never appears, it doesn't appear in the dict (or value 0.0)
Hints
Hint 1: Track visits
For each state, keep a list of returns from first visits. Average them at the end.
Hint 2: First-visit check
For each episode, iterate through states seen and mark first visits.
Hint 3: Compute return
For each first visit at index i, sum future rewards with discounts.
Keywords
rlmonte-carlovalue-estimationprediction
Python
Run your code to see results
No results yet
Run your code to check it against the examples, or submit to run every test.