Simulation and inference code