Dopamine neurons fire to an unexpected reward, shift their response to the earliest cue that predicts reward once it is learned, and dip below baseline when an expected reward is omitted. Sutton and Barto show that the TD error of tabular TD(0) behaves the same way. Simulate it.
Implement dopamine_td(trial_types, cue_len, reward, alpha, gamma=1.0). The learned states are a trial-start state 0 and cue states 1, ..., cue_len (a CSC-style sequence: one state per time step after the cue). Their values start at zero. Two further states have value fixed at zero and are never updated: the inter-trial interval that precedes every trial and the terminal state that ends it.
Every trial begins with a transition from the inter-trial interval into state 0 with reward 0. Its TD error is ; record it as the trial's first TD error, but make no update for it. The trial then continues according to its type:
"rewarded": cue_len terminal. Every transition has reward 0 except the last, which pays reward."omitted": the same path, but the last transition pays 0."blank": terminal with reward 0 (no cue appears on this trial).On each of these transitions with reward , compute , record it, and immediately update .
Return (deltas, V): deltas is a list with one list per trial holding that trial's TD errors in order (the entry error first), and V is the final value array of length cue_len + 1.