Simulation code for models 4 and 5.