this version must be very close to code by Briggs et al (2000), but unclear about initial state and meaning of residual