Taken from https://code.google.com/p/scipy-sim/