The Rescorla–Wagner model explains why an animal fails to learn about a new cue that is added to an already-predictive one. Each conditioned stimulus (CS) has an associative strength . On a trial, the stimuli present are encoded as a binary feature vector , the animal's prediction of the unconditioned stimulus (US) is the aggregate strength , and learning is driven by the surprise
where is the magnitude of the US on that trial (0 if there is none). Rescorla and Wagner allowed a different step size per stimulus, so may be a vector with one entry per CS, applied elementwise.
Implement rescorla_wagner(trials, n_cs, alpha, w0=None):
trials: list of (present, us) pairs in order, where present is a list of CS indices shown on that trial and us is the US magnitude .alpha: a number or an array of length n_cs.w0: initial strengths (zeros if None).Return (w, deltas): the final strengths as a float array and the list of prediction errors , one per trial.