Implement a function that performs both batch TD(0) and batch every-visit Monte Carlo prediction on the same fixed dataset of episodes, demonstrating how they converge to different value estimates.
In batch training, a fixed set of experience is presented to the learning algorithm repeatedly until the value estimates converge. The key insight is that given the same batch of data, TD(0) and Monte Carlo methods can arrive at fundamentally different answers.
Your function should:
- Compute batch MC values by calculating the average return observed from each state across all episodes (every-visit).
- Compute batch TD(0) values by repeatedly sweeping through all transitions in the batch, applying TD(0) updates until convergence.
Args:
- episodes: List of episodes. Each episode is a list of (state, reward) tuples. The reward at index i is received when leaving state i. After the last tuple, the episode terminates (next state value is 0).
- n_states: Number of states (integers 0 to n_states-1)
- gamma: Discount factor
- alpha: Learning rate for TD(0) updates
- max_iterations: Maximum number of full sweeps through the batch for TD(0)
Return a tuple of two lists: (V_td, V_mc), each containing value estimates rounded to 4 decimal places.